210 likes | 354 Views
CONTAINMENT. Class Relationships. 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
E N D
CONTAINMENT Class Relationships
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]; }; };
multiple inheritance C is made up of parts of A and B A B C C can be made to inherit from A and B
multiple inheritance class tools{ public: tools(char*); ~tools(); }; class labour{ public: labour(); ~labour();
plans inherits from tools and labour class plans:public tools,public labour{ public: plans(int m):tools(“lathe”), labour(“fred”){;} ~plans(); };
Tutorial • Set up a new TIME_ACCT class where inheritance is not used. Replace the inheritance by containment. Do any necessary adjustments that are required throughout the account class hierarchy. • Additional : set up an array of pointers to ACCOUNT type objects then place instances of any accounts within the array. Display the contents polymorphically
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;} }