Ich habe ein Problem mit diesen Zeilen in Haupt:Operatorüberladung =, * =. Wie funktioniert das?
* Registerkarte [1] = Test1;
* Registerkarte [4] = test2; Es fügt nur Farbe hinzu und Variablen (a, b, h) bleiben gleich. Ich war so etwas wie dies
cuboid operator=(const cuboid & base)
{
return cuboid(base.colour(), base.valueA(), base.valueB(),base.h_)
}
versucht, aber das scheint nicht zu funktionieren entweder
nächste ist dies:
* Registerkarte [4] * = 2;
Es gibt einen überladenen Operator für diese Methode, und wenn ich dies ausführe, tritt ein Fehler auf. Keine Übereinstimmung für Operator * =. Operandentypen sind Abbildung und Int.
Die letzte ist: * Registerkarte [2] = "hell" + * Registerkarte [2]; Ich denke, dass ich dafür einen neuen Konstruktor brauche, aber wo mache ich einen?
Danke für jede Antwort !!
#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;
}
Willkommen bei Stack Overflow! Sie können [die Tour] (http://stackoverflow.com/tour) zuerst und lernen [Wie man eine gute Frage stellt] (http://stackoverflow.com/help/how-to-ask) und erstellen Sie eine [ Minimal, vollständig und verifizierbar] (http://stackoverflow.com/help/mcve) Beispiel. Das erleichtert es uns, Ihnen zu helfen. –