1 / 21

CONTAINMENT

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

faith
Télécharger la présentation

CONTAINMENT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CONTAINMENT Class Relationships

  2. 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.

  3. Possible implementations Two possibilities exist for the implementation of containment : 1. class Vehicle{ Car mini; //contained object Vehicle() {;} };

  4. Part of 2. pointer member: class Vehicle{ Car * mini; Vehicle(Car* acar): mini(acar){;} };

  5. Further Example class X{ int a; public: X(int val){a = val;} int getVal(){ return a;} };

  6. 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;}

  7. An instance of the class C main() { C myC(5,10); cout << myC.getA(); cout<< myC.getP(); }

  8. 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

  9. 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]; }; };

  10. 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

  11. multiple inheritance class tools{ public: tools(char*); ~tools(); }; class labour{ public: labour(); ~labour();

  12. plans inherits from tools and labour class plans:public tools,public labour{ public: plans(int m):tools(“lathe”), labour(“fred”){;} ~plans(); };

  13. 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

  14. Rectangle class uses association to link with Point Rectangle Point

  15. Two methods of resolving • Pointers to members • Member variables

  16. 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

  17. 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(); }

  18. Constructors Rectangle(int x, int y, int p, int q): p1(x,y), p2(p,q) { } Rectangle(){;} or none

  19. Using Pointer member variables Class Rectangle { Point* p1; Point* p2; public: Rectangle(int x, int y, int p, int q); Point* getp1(); Point* getp2(); }

  20. 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); }

  21. 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;} }

More Related