#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 << "\nIn Point's copy constructor";
}
~Point(){}
int GetX() const { return myX; }
void SetX(int x) { myX = x; }
int GetY() const { return myY; }
void SetY(int y) { myY = y; }
private:
int myX;
int myY;
};
class Rectangle
{
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";
}
~Rectangle(){ cout << "\nIn destructor..." << endl; delete myUpperLeft; delete myLowerRight; }
int GetWidth() { return myLowerRight->GetX() - myUpperLeft->GetX(); }
int GetHeight() { return myLowerRight->GetY() - myUpperLeft->GetY(); }
// private:
Point * myUpperLeft;
Point * myLowerRight;
};
int main()
{
Point ul(0,0);
Point lw(20,30);
Rectangle myRect(ul,lw);
Rectangle otherRect(0,30,50,50);
cout << "\nmyRect measures ";
cout << myRect.GetWidth();
cout << " by " << myRect.GetHeight() << endl;
cout << "myRect address: " << &myRect << endl;
cout << "myRect->myUpperLeft: " << myRect.myUpperLeft << endl;
cout << "&myRect.myUpperLeft: " << &myRect.myUpperLeft << endl;
cout << "\notherRect measures ";
cout << otherRect.GetWidth();
cout << " by " << otherRect.GetHeight() << endl;
cout << "otherRect address: " << &otherRect << endl;
cout << "otherRect->myUpperLeft: " << otherRect.myUpperLeft << endl;
cout << "&otherRect.myUpperLeft: " << &otherRect.myUpperLeft << endl;
cout << "\nAssigning myRect = otherRect...\n";
myRect = otherRect;
cout << "\notherRect measures ";
cout << otherRect.GetWidth();
cout << " by " << otherRect.GetHeight() << endl;
cout << "otherRect address: " << &otherRect << endl;
cout << "otherRect->myUpperLeft: " << otherRect.myUpperLeft << endl;
cout << "&otherRect.myUpperLeft: " << &otherRect.myUpperLeft << endl;
return 0;
}