1 / 18

CONTAINMENT

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

guido
Download Presentation

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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