0% found this document useful (0 votes)
11 views23 pages

Composition 27032024 090045am 26072024 100343am

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 23

Compositi

on
Object Oriented
Programming
Spring
2020
Imran
Siddiqi
Introductio
Innreal-life, complex objects are often
built from smaller, simpler objects. For
example

oA car is built using a metal frame, an engine,


some tires, a transmission, a steering wheel,
and a large number of other parts.

oA personal computer is built from a


CPU, a motherboard, some memory,
etc.
2
The process of building complex
objects from simpler ones is called
composition (also known as
object composition).

3
Compositi
on
More specifically, composition is used for
objects that have a has-a relationship to each
other.

A car has-a metal frame, has-an engine, and


has-a
transmission.

A personal computer has-a CPU, a


motherboard, and other components.
4
Composition -
Example

5
Composition -
Example

6
class Date Composition -
Example
{
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;
7
day = dy;}
void Date::print() const
{
cout << month << '/' << day << '/' << year;
}

Date::~Date()
{
cout << "Date object destructor for date ";
cout << endl;
}

8
class Employee
{
public:
Employee( int ,Date, Date );
void print() const;
~Employee();

private:
int ID;
Date birthDate;
Date hireDate;
};

Employee::Employee( i Date dateOfBirth, Date dateOfHire )


: ID(eID),birthDate(
nt eID, dateOfBirth ), hireDate( dateOfHire )
{
}

9
void Employee::print() const
{
cout << Emp ID << ", " << ID << " Hired: ";
hireDate.print();
cout << " Birthday:
"; birthDate.print();
cout << endl;
}

Employee::~Employee()
{
}

10
int main()
{
Date birth( 7, 24, 1949 );
Date hire( 3, 12, 1989 );
Employee manager( 123, birth, hire );

cout << endl;


manager.print();

return 0;
}

11
Composition
and
Aggregati
on
Composition vs.
Aggregation
► Both represent ‘has-a’
relationship
of the "has
► Composition is a stronger a“
variant 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 vs.
Aggregation
► 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
UML
Notation

Composition

Aggregation
Exampl
► Team has players
esAggregation: 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
The ‘this’
Pointer
► Member functions – Access to a pointer named
‘this’
► Points to the object itself
► A member function can find the address of the
object of which it is member
The 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;
}
Accessing Member Data with
‘this’
class What
{
private:
int alpha;
public:
void tester()
{
this->alpha = 10;
cout << this->alpha;
}
};
int main()
{ What w;
w.tester();
return 0;
}
Exampl
e #include <iostream.h>
using namespace std;

class Test{

public:
Test(int xx =0);
voidprint () const;

private:
int x;
};
Exampl
e
Test::Test (int a ) // Constructor
{ x = a;}
void Test::print() const
{
cout << " x is equal to " << x ;
<< "\n this->x is equal to "<<this->x;
cout << "\n (*this).x is equal to "<< (*this).x <<endl;
}
int
cout
main (void)
{ Test testobject(12);
testobject.print();
return 0;
}
Exampl
elass Point{
c
private:
int x;
int
y;
public: x, int y){
Point(this->x = x;
int
this->y =
y;
}
};
The Composite
Rectangle Class
class
Rectangle
{ private:
Poin upperLeft,
t lowerRight;
public:
Rectangle() {}
Rectangle (Point ul, Point
lr);
Poin GetUpperLeft() const { return upperLeft;
t } GetLowerRight() const { return
Poin SetUpperLeft(Point
void lowerRight; }
Location)
t
{ upperLeft = Location; }
void SetLowerRight(Point
Location)
{ lowerRight = Location;
} int GetArea() const; 23

};

You might also like