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

时间:2017-01-23 00:37:13

标签: c++ c++11

我在main中的那些行有问题:

*标签[1] = TEST1;
*标签[4] = TEST2; 它只是添加颜色,变量(a,b,h)保持不变。我正在尝试这样的事情

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

但这似乎无法正常工作

下一个就是:

* tab [4] * = 2;

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

最后一个是:* tab [2] =" bright" + * tab [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;
}

1 个答案:

答案 0 :(得分:0)

您正在定义数组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;

你是从班级人物那里打电话给那个分配操作员。

operator *=相同。班级figure就是没有  有它。这就是为什么你会收到错误。