0% found this document useful (0 votes)
10 views25 pages

OOP Lecture 6 09112022 071704pm

This document discusses object-oriented programming concepts like composition, aggregation, and the 'this' pointer in C++. It provides examples of composition using classes like Date and Employee that have composition relationships. It distinguishes between composition and aggregation, providing examples of each. It also demonstrates how to use the 'this' pointer to access members of an object from within its own member functions.

Uploaded by

Ali Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views25 pages

OOP Lecture 6 09112022 071704pm

This document discusses object-oriented programming concepts like composition, aggregation, and the 'this' pointer in C++. It provides examples of composition using classes like Date and Employee that have composition relationships. It distinguishes between composition and aggregation, providing examples of each. It also demonstrates how to use the 'this' pointer to access members of an object from within its own member functions.

Uploaded by

Ali Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

OBJECT ORIENTED

PROGRAMMING

Muhammad Zeeshan Javed


Bahria University Islamabad
[email protected]
• Composition
• Aggregation
• Composition vs Aggregation
• Examples
• This pointer
COMPOSITION

2
COMPOSITION
In real-life, complex objects are often built from smaller, simpler objects.
For example

A car is built using a metal frame, an engine, some tires, a transmission,


a steering wheel, and many other parts.

A personal computer is built from a CPU, a motherboard, some memory,


etc.

3
COMPONENTS OF A CAR

4
COMPOSITION

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

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

void Date::print() const


{
cout << month << '/' << day << '/' << year;
}

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( int eID, Date dateOfBirth, Date dateOfHire )


: ID(eID),birthDate( dateOfBirth ), hireDate( dateOfHire )
{}
10
COMPOSITION
void Employee::print() const
{
cout << Emp ID << ", " << ID << " Hired: ";
hireDate.print();
cout << " Birthday: ";
birthDate.print();
cout << endl;
}

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

Player Date of Birth

A Team has Player


A Player has a Date of Birth

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

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

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

Point tl(2, 8);


Point br(10, 2);

Rectangle r(tl, br);

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

void Test::print() const


{
cout << " x is equal to " << x ;
cout << "\n this->x is equal to "<<this->x;
cout << "\n (*this).x is equal to "<< (*this).x <<endl;
}

int main (void)


{
Test testobject(12);
testobject.print();
return 0;}
24
ACCESSING MEMBERS WITH ‘THIS’ POINTER
class Point{
private:
int x;
int y;
public:
Point(int x, int y){
this->x = x;
this->y = y;
}
25 };

You might also like