tranvanduong
Gà con
#include <iostream>
using namespace std;
class SimpleCircle
{
private:
float *itsRadius;
public:
SimpleCircle();
SimpleCircle(float Radius);
SimpleCircle(const SimpleCircle&);
~SimpleCircle();
float GetItsRad() const { return *itsRadius; }
void SetItsRad(float x) { *itsRadius = x; }
const SimpleCircle& operator++ ();
const SimpleCircle operator++ (int);
const SimpleCircle& operator=(const SimpleCircle&);
};
SimpleCircle::SimpleCircle()
{
itsRadius = new float;
*itsRadius = 5;
}
SimpleCircle::SimpleCircle(const SimpleCircle& rhs)
{
itsRadius = new float;
*itsRadius = rhs.GetItsRad();
}
SimpleCircle::~SimpleCircle()
{
delete itsRadius;
itsRadius = 0;
}
SimpleCircle::SimpleCircle( float Radius )
{
itsRadius = new float;
*itsRadius = Radius;
}
const SimpleCircle& SimpleCircle :: operator++ ()
{
++itsRadius;
return *this;
}
const SimpleCircle SimpleCircle :: operator++ (int flag)
{
SimpleCircle temp(*this);
++itsRadius;
return temp;
}
const SimpleCircle& SimpleCircle :: operator= (const SimpleCircle& rhs)
{
if(this == &rhs)
return *this;
*itsRadius = rhs.GetItsRad();
return *this;
}
int main()
{
SimpleCircle Circle1;
cout << "Radius of Circle1: " << Circle1.GetItsRad() << endl;
SimpleCircle Circle2(9);
cout << "Radius of Circle2: " << Circle2.GetItsRad() << endl;
++Circle1;
cout << "Radius of Circle1: " << Circle1.GetItsRad() << endl;
Circle2++;
cout << "Radius of Circle2: " << Circle2.GetItsRad() << endl;
Circle2 = Circle1;
cout << "Radius of Circle2: " << Circle2.GetItsRad() << endl;
return 0;
}
OUTPUT:
Radius of Circle1: 5
Radius of Circle2: 9
Radius of Circle1: -1.21979e-12
Radius of Circle2: -1.21979e-12
Radius of Circle2: -1.21979e-12
using namespace std;
class SimpleCircle
{
private:
float *itsRadius;
public:
SimpleCircle();
SimpleCircle(float Radius);
SimpleCircle(const SimpleCircle&);
~SimpleCircle();
float GetItsRad() const { return *itsRadius; }
void SetItsRad(float x) { *itsRadius = x; }
const SimpleCircle& operator++ ();
const SimpleCircle operator++ (int);
const SimpleCircle& operator=(const SimpleCircle&);
};
SimpleCircle::SimpleCircle()
{
itsRadius = new float;
*itsRadius = 5;
}
SimpleCircle::SimpleCircle(const SimpleCircle& rhs)
{
itsRadius = new float;
*itsRadius = rhs.GetItsRad();
}
SimpleCircle::~SimpleCircle()
{
delete itsRadius;
itsRadius = 0;
}
SimpleCircle::SimpleCircle( float Radius )
{
itsRadius = new float;
*itsRadius = Radius;
}
const SimpleCircle& SimpleCircle :: operator++ ()
{
++itsRadius;
return *this;
}
const SimpleCircle SimpleCircle :: operator++ (int flag)
{
SimpleCircle temp(*this);
++itsRadius;
return temp;
}
const SimpleCircle& SimpleCircle :: operator= (const SimpleCircle& rhs)
{
if(this == &rhs)
return *this;
*itsRadius = rhs.GetItsRad();
return *this;
}
int main()
{
SimpleCircle Circle1;
cout << "Radius of Circle1: " << Circle1.GetItsRad() << endl;
SimpleCircle Circle2(9);
cout << "Radius of Circle2: " << Circle2.GetItsRad() << endl;
++Circle1;
cout << "Radius of Circle1: " << Circle1.GetItsRad() << endl;
Circle2++;
cout << "Radius of Circle2: " << Circle2.GetItsRad() << endl;
Circle2 = Circle1;
cout << "Radius of Circle2: " << Circle2.GetItsRad() << endl;
return 0;
}
OUTPUT:
Radius of Circle1: 5
Radius of Circle2: 9
Radius of Circle1: -1.21979e-12
Radius of Circle2: -1.21979e-12
Radius of Circle2: -1.21979e-12