Title: Rectangle class uses association to link with Point
1Rectangle class uses association to link with
Point
Rectangle
Point
2Two methods of resolving
- Pointers to members
- Member variables
3Tutorial Exercise
Set up a class called Point which contains two
Members x and y which represent a point on
the Screen. Write functions to return the values
associated with x and y. Now write a class
called Rectangle which contains two Points. The
Points represent the top left screen position and
the bottom Right screen position for the
rectangle. Write functions to return the values
associated with the points In the rectangle. You
will obviously need to provide suitable Constructo
rs. Experiment with the pointer and non pointer
members solutions
4Non Pointer Solution for Rectangle
Class Rectangle Point p1 Point
p2 public Rectangle(int x, int y, int p, int
q) Point getp1() Point getp2()
5Constructors
Rectangle(int x, int y, int p, int q) p1(x,y),
p2(p,q) Rectangle() or none
6Using Pointer member variables
Class Rectangle Point p1 Point
p2 public Rectangle(int x, int y, int p, int
q) Point getp1() Point getp2()
7Constructors
Rectangle(Point a, Point b) p1 a p2
b Rectangle(int x, int y, int p, int q) p1
new Point(x,y) p2 new Point(p,q)
8Copy constructor
Rectangle(const Rectangle toCopy) p1 new
Point(toCopy.p1-gtgetx(), toCopy.p1-gtgety()
p2 . Rectangle() if (p1
! 0) delete p1 if ( p2 ! 0) delete p2