运算符重载 =, *=.如何使这项工作

Operator overloading =, *=. How to make this work?

本文关键字:何使这 工作 重载 运算符      更新时间:2023-10-16

我对main中的这些行有问题:

*选项卡[1]=测试1;
*选项卡[4]=测试2;它只是增加了颜色,变量(a,b,h)保持不变。我正在尝试这样的事情

cuboid operator=(const cuboid & base)
{
return cuboid(base.colour(), base.valueA(), base.valueB(),base.h_)
}  

但这似乎也不起作用

下一个是这个:

*制表符[4] * =2;

此方法有重载运算符,当我运行此方法时,会出现一些错误。运算符不匹配*=。操作数类型为图形和整数。

最后一个是:*选项卡[2] = "明亮" + *选项卡[2];我认为我需要一个新的构造函数,但是我在哪里制作一个?

感谢您的任何回答!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <math.h>
using namespace std;
class figure
{
string * colour_;
public:
figure(): colour_(new string("Empty")) {}
figure(const string & colour): colour_(new string(colour)) {}
figure(const figure & base)
{
    colour_=new string(*base.colour_);
}
virtual ~figure() {delete colour_;}
 string & colour () const {return *colour_;}
virtual double area () =0;
virtual void print(ostream& where) const
{
where << "Colour: " << colour() << " ";
}
friend ostream & operator <<(ostream &os, const figure & base)
{
base.print(os);
return os;
}
figure & operator=(const figure & base) 
{
if(this==&base)
    return *this;
else
{
colour_=new string(*base.colour_);
return *this;
}
}
};
class circle :public figure
{
int r_;
public:
circle() : r_(0) {}
circle(const string & colour,const int r) : figure(colour), r_(r) {}
double area()
{
    return M_PI*r_*r_;
}
const int & radius() const {return r_;}
void print(ostream& where) const
{
where << "Colour: " << colour() << " ";
where << "Radius: " << radius() << " ";
}
circle & operator=(const circle & base)
{
    r_=base.r_;
    figure::operator=(base);
    return *this;
}
};
class rectangle : public figure
{
int a_;
int b_;
public:
static int ObjectCount_;
rectangle() : a_(0), b_(0) {++ObjectCount_;}
rectangle(const string & colour, const int a, const int b) :  figure(colour),a_(a), b_(b) {++ObjectCount_;}
~rectangle() {--ObjectCount_;}
double area()
{
    return a_*b_;
}
const int & valueA () const {return a_;}
const int & valueB () const {return b_;}
int & changeA() {return a_;}
int & changeB() {return b_;}
void print(ostream& where) const
{
where << "Colour: " << colour() << " ";
where << "Value A: " << valueA() << " ";
where << "Value B: " << valueB() << " ";
}
rectangle & operator=(const rectangle & base)
{
    a_=base.a_;
    b_=base.b_;
    return *this;
}
static int & ObjectCount() {return ObjectCount_; }
};
class cuboid :public rectangle
{
int h_;
public:
cuboid() : h_(0) {}
cuboid(const string & colour, const int a, const int b, const int h) :  rectangle(colour,a,b), h_(h) {}
double area()
{
    return 2*valueA()*valueB()+2*valueB()*h_+2*valueA()*h_;
}
void print(ostream& where) const
{
where << "Colour: " << colour() << " ";
where << "Value A: " << valueA() << " ";
where << "Value B: " << valueB() << " ";
where << "Height: " << h_ << " ";
}
cuboid & operator=(const cuboid & base)
{
figure::operator=(base);
rectangle::operator=(base);
h_=base.h_;
return *this;
}
cuboid & operator*=(const int number)
{
h_*=number;
changeA()*=number;
changeB()*=number;
return *this;
}
};
int rectangle::ObjectCount_=0;
int main()
{
figure * tab[5];
const circle test1("black",100);
const cuboid test2("grey", 2,2,2);
tab[0]=new circle("red",1);
tab[1]=new circle;
tab[2]=new rectangle("blue",1,1);
tab[3]=new cuboid("green",1,1,1);
tab[4]=new cuboid;
for(unsigned i=0; i<5;++i)
cout << tab[i]->area() << endl;
for(int i=0; i<5; ++i)
cout<<*tab[i]<<tab[i]->area()<<"n";
cout << "***********************" << endl;
*tab[1]=test1;                  // it just assigns a colour, rest stays the same
*tab[4]=test2;                 // same here
/*
*tab[2] = "bright" + *tab[2];   //?????
*/
//*tab[4]*=2;                 //some error, no idea
for(int i=0; i<5; ++i)
cout<<*tab[i]<<tab[i]->area()<<"n";
cout << "$ " << rectangle::ObjectCount() << endl;
for(int i=0; i<5; i++)
delete tab[i];
cout << "$ " << rectangle::ObjectCount() << endl;
}

你正在定义数组figure* tab[5],所以你做了一个辅助,你把所有指向你的对象的指针都投射到figure*

tab[0]=new circle("red",1);
tab[1]=new circle;
tab[2]=new rectangle("blue",1,1);
tab[3]=new cuboid("green",1,1,1);
tab[4]=new cuboid;

figure 只有以下辅助运算符:

figure & operator=(const figure & base) {
    if(this==&base)
        return *this;
    else {
        colour_=new string(*base.colour_);
        return *this;
    }
}

所以,那么你正在做:

*tab[1]=test1;
*tab[4]=test2;

您正在从类图中调用该 assigment 运算符。

operator *= .class figure相同,只是没有 拥有它。这就是您遇到错误的原因。