CONTAINMENT

18
CONTAINMENT Class Relationships

description

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 - PowerPoint PPT Presentation

Transcript of CONTAINMENT

Page 1: CONTAINMENT

CONTAINMENT

Class Relationships

Page 2: CONTAINMENT

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

Page 3: CONTAINMENT

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.

Page 4: CONTAINMENT

Possible implementations

Two possibilities exist for the implementation of containment :

1. class Vehicle{

Car mini; //contained object

Vehicle() {;}

};

Page 5: CONTAINMENT

Part of

2. pointer member:

class Vehicle{

Car * mini;

Vehicle(Car* acar): mini(acar){;}

};

Page 6: CONTAINMENT

Further Example

class X{

int a;

public:

X(int val){a = val;}

int getVal(){ return a;}

};

Page 7: CONTAINMENT

Example - contained membersclass 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;}

Page 8: CONTAINMENT

An instance of the class C

main(){

C myC(5,10);cout << myC.getA();cout<< myC.getP();

}

Page 9: CONTAINMENT

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

Page 10: CONTAINMENT

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];

}; };

Page 11: CONTAINMENT

Rectangle class uses association to link with Point

Rectangle Point

Page 12: CONTAINMENT

Two methods of resolving

•Pointers to members•Member variables

Page 13: CONTAINMENT

Tutorial Exercise

Set up a class called Point which contains two Members x and y which represent a point on theScreen. Write functions to return the values associatedwith 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 suitableConstructors. Experiment with the pointer and non pointer members solutions

Page 14: CONTAINMENT

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();

}

Page 15: CONTAINMENT

Constructors

Rectangle(int x, int y, int p, int q): p1(x,y), p2(p,q){

}

Rectangle(){;} or none

Page 16: CONTAINMENT

Using Pointer member variablesClass Rectangle{

Point* p1;Point* p2;public:

Rectangle(int x, int y, int p, int q);Point* getp1();Point* getp2();

}

Page 17: CONTAINMENT

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);

}

Page 18: CONTAINMENT

Copy constructorRectangle(const Rectangle& toCopy){

p1 = new Point(toCopy.p1->getx(), toCopy.p1->gety(); p2 = …………….}

~Rectangle(){ if (p1 != 0){ delete p1;}if ( p2 != 0) {delete p2;}

}