#include <iostream.h>
class Point
{
public:
Point (int x, int y):myX(x), myY(y) {}
Point (const Point & rhs):
myX(rhs.myX),
myY(rhs.myY)
{
cout << "In Point's copy constructor\n";
}
~Point(){}
int GetX() const { return myX; }
void SetX(int x) { myX = x; }
int GetY() const { return myY; }
void SetY(int y) { myY = y; }
const Point & operator++();
const Point & operator++(int);
const Point & operator--();
const Point & operator--(int);
private:
int myX;
int myY;
};
const Point & Point::operator++()
{
++myX;
++myY;
return *this;
}
const Point & Point::operator++(int)
{
Point temp(*this); // hold the current value
++myX;
++myY;
return temp;
}
const Point & Point::operator--()
{
--myX;
--myY;
return *this;
}
const Point & Point::operator--(int)
{
Point temp(*this); // hold the current value
--myX;
--myY;
return temp;
}
class Shape
{
public:
Shape(){}
~Shape(){}
virtual Shape * Clone() const { return new Shape(*this); }
};
class Rectangle : public Shape
{
public:
Rectangle(Point upperLeft, Point lowerRight):
myUpperLeft ( new Point(upperLeft)),
myLowerRight(new Point(lowerRight))
{}
Rectangle(int upperLeftX, int upperLeftY, int lowerRightX, int lowerRightY):
myUpperLeft(new Point(upperLeftX,upperLeftY)),
myLowerRight(new Point(lowerRightX,lowerRightY))
{}
Rectangle( const Rectangle & rhs ):
myUpperLeft(new Point(*myUpperLeft)),
myLowerRight(new Point(*myLowerRight))
{
cout << "\nIn Rectangle's copy constructor...\n";
}
Shape * Clone() const { return new Rectangle(*this); }
~Rectangle(){ cout << "\nIn destructor..." << endl; delete myUpperLeft; delete myLowerRight; }
Rectangle & operator=(const Rectangle & rhs);
void Expand() { --(*myUpperLeft); ++(*myLowerRight); }
int GetWidth() { return myLowerRight->GetX() - myUpperLeft->GetX(); }
int GetHeight() { return myLowerRight->GetY() - myUpperLeft->GetY(); }
private:
Point * myUpperLeft;
Point * myLowerRight;
};
Rectangle & Rectangle::operator=(const Rectangle & rhs)
{
if ( this == &rhs ) // protect against a = a
return *this;
delete myUpperLeft;
delete myLowerRight;
myUpperLeft = new Point(*rhs.myUpperLeft);
myLowerRight= new Point(*rhs.myLowerRight);
return *this;
}
int main()
{
Point ul(10,10);
Point lw(20,30);
Rectangle myRect(ul,lw);
cout << "\nmyRect measures ";
cout << myRect.GetWidth();
cout << " by " << myRect.GetHeight() << endl;
myRect.Expand();
cout << "\nmyRect measures ";
cout << myRect.GetWidth();
cout << " by " << myRect.GetHeight() << endl;
return 0;
}