200 likes | 529 Views
CONTAINMENT. Class Relationships. Introduction. Last Week we introduced polymorphism explained virtual and pure virtual functions learned how to use polymorphism in C++ programs This lecture we will discuss association (containment) compare containment by instance
E N D
CONTAINMENT Class Relationships
Introduction • Last Week we • introduced polymorphism • explained virtual and pure virtual functions • learned how to use polymorphism in C++ programs • This lecture we will • discuss association (containment) • compare • containment by instance • containment by pointer • look at destructors in more detail • look at one-to-many association
The part of relationship Some class relationships are better described by the part of relationship eg part of a vehicle is the engine. Contrast this with a car is a type of vehicle This latter relationship lends itself to inheritance but the part of is containment.
Possible implementations Two possibilities exist for the implementation of containment : 1. class Vehicle{ Car mini; //contained object Vehicle() {;} };
Part of 2. pointer member: class Vehicle{ Car * mini; Vehicle(Car* acar): mini(acar){;} };
Further Example class X{ int a; public: X(int val){a = val;} int getVal(){ return a;} };
Example - contained members class C{ X a; X* p; public: C(int i, int j) : a(i), p(new X(j)) {;} int getA(){return a.getVal();} int getP(){return p->getVal();} ~C(){delete p;}
An instance of the class C main() { C myC(5,10); cout << myC.getA(); cout<< myC.getP(); }
Comparison ordinary members v pointer members • The pointer solution should be used when there is a need to alter the contained object during the lifetime of the containing object • The contained member can be supplied as an argument if the pointer solution is used • The pointer member can be of the same class as the containing class itself
containment • It is possible to have an array of objects within a class - sometimes referred to as aggregation • class A{ class A{ B shapes[5]; B* shapes[5]; }; };
Rectangle class uses association to link with Point Rectangle Point
Two methods of resolving • Pointers to members • Member variables
Tutorial 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 Constructors. Experiment with the pointer and non pointer members solutions
Non Pointer Solution for Rectangle Class Rectangle { Point p1; Point p2; public: Rectangle(int x, int y, int p, int q); Point getp1(); Point getp2(); }
Constructors Rectangle(int x, int y, int p, int q): p1(x,y), p2(p,q) { } Rectangle(){;} or none
Using Pointer member variables Class Rectangle { Point* p1; Point* p2; public: Rectangle(int x, int y, int p, int q); Point* getp1(); Point* getp2(); }
Constructors 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); }
Copy constructor Rectangle(const Rectangle& toCopy) { p1 = new Point(toCopy.p1->getx(), toCopy.p1->gety(); p2 = ……………. } ~Rectangle(){ if (p1 != 0){ delete p1;} if ( p2 != 0) {delete p2;} }