OOP Lecture 6 09112022 071704pm
OOP Lecture 6 09112022 071704pm
PROGRAMMING
2
COMPOSITION
In real-life, complex objects are often built from smaller, simpler objects.
For example
3
COMPONENTS OF A CAR
4
COMPOSITION
5
COMPOSITION
Composition is used for objects that have a has-a relationship to
each other.
6
COMPOSITION - EXAMPLE
7
COMPOSITION
class Date
{
public:
Date( int d= 1, int m= 1, int y= 1900 );
void print() const;
~Date();
private:
int month;
int day;
int year;
};
Date::Date( int mn, int dy, int yr )
{
month = mn;
year = yr;
day = dy;}
8
COMPOSITION
Date::~Date()
{
cout << "Date object destructor for date ";
cout << endl;
}
9
COMPOSITION
class Employee
{
public:
Employee( int ,Date, Date );
void print() const;
~Employee();
private:
int ID;
Date birthDate;
Date hireDate;
};
Employee::~Employee()
{
}
11
COMPOSITION
int main()
{
Date birth( 7, 24, 1949 );
Date hire( 3, 12, 1989 );
Employee manager( 123, birth, hire );
cout << endl;
manager.print();
return 0;
}
12
COMPOSITION AND AGGREGATION
Both represent ‘has-a’ relationship
Composition is a stronger variant of the "has a“ relationship
Aggregation can occur when a class is a collection or container of other classes, but
where the contained classes do not have a strong life cycle dependency on the
container
If the container is destroyed, its contents are not.
Composition has a strong life cycle dependency between instances of the container
class and instances of the contained class(es)
If the container is destroyed, normally every instance that it contains is destroyed as
well
13
UML NOTATION
14
UML NOTATION
Team Player
15
EXAMPLES
Team has players
Aggregation: If team dissolve, Player will still exists
Player has a date of birth
Composition: If player does not exist there will be no date of birth
Course has registered students
Aggregation: If this course does not exist, students will still be there
Circle has a Point (center)
Composition: If circle is not there, we won’t have its center
16
THIS POINTER
17
RECTANGLE CLASS
class Point{
private:
int x;
int y;
public:
Point(int x = 0, int y = 0){
this->x = x;
this->y = y;
}
void printPoint(){ cout << "(" << x << "," << y << ")" << endl; }
void setX(int x){ this->x = x;}
void setY(int y){ this->y = y; }
int getX(){ return x; }
int getY(){ return y; }
18 };
RECTANGLE CLASS
class Rectangle{
private:
Point topLeft;
Point bottomRight;
public:
Rectangle(Point tl, Point br){
this->topLeft = tl;
this->bottomRight = br;
}
void printRect() {cout << "Top Left:"; topLeft.printPoint() <<"Bottom Right:"; bottomRight.printPoint();}
int calcWidth() { return bottomRight.getX() - topLeft.getX(); }
int calcHeight() { return topLeft.getY() - bottomRight.getY();}
int calcArea() { return calcWidth() * calcHeight(); }
};
19
RECTANGLE CLASS
int main(){
r.printRect();
cout << "Area of rectangle:" << r.calcArea() << endl;
getchar();
return 0;
}
20
ACCESSING MEMBERS WITH ‘THIS’ POINTER
class Where
{
private:
int x;
public:
void reveal()
{
cout<<“My object address is : “<< this << endl;
}
};
int main()
{ Where w1,w2,w3;
w1.reveal();
w2.reveal();
w3.reveal();
return 0;
21 }
ACCESSING MEMBERS WITH ‘THIS’ POINTER
class What
{
private:
int alpha;
public:
void tester()
{
this->alpha = 10;
cout << this->alpha;
}
};
int main()
{ What w;
w.tester();
return 0;
}
22
ACCESSING MEMBERS WITH ‘THIS’ POINTER
#include <iostream.h>
using namespace std;
class Test{
public:
Test (int xx =0);
void print () const;
private:
int x;
};
23
ACCESSING MEMBERS WITH ‘THIS’ POINTER
Test::Test (int a ) { x = a;} // Constructor