0% found this document useful (0 votes)
5 views11 pages

Object Oriented Programming 9 + 10

The document covers advanced topics in Object-Oriented Programming, focusing on compile-time polymorphism, unary and binary operator overloading using friend functions, and the relationships between classes such as aggregation and composition. It explains the differences between aggregation (weak ownership) and composition (strong ownership) with examples. Key concepts include how these relationships affect object creation, lifetime dependency, ownership, and deletion impact.

Uploaded by

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

Object Oriented Programming 9 + 10

The document covers advanced topics in Object-Oriented Programming, focusing on compile-time polymorphism, unary and binary operator overloading using friend functions, and the relationships between classes such as aggregation and composition. It explains the differences between aggregation (weak ownership) and composition (strong ownership) with examples. Key concepts include how these relationships affect object creation, lifetime dependency, ownership, and deletion impact.

Uploaded by

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

OBJECT

ORIENTED
PROGRAMMING
Lecture 9 + 10
Instructor: Tayyba Khalid
Content:

■ Compile Time polymorphism (Continue)


■ Friend Unary Operator overloading
■ Friend Binary Operator overloading
■ Identification of classes and their relationships
■ Aggregation
■ Composition
Compile Time polymorphism
(Continue)
What is Compile-Time Polymorphism?
• It refers to the ability of the compiler to decide which function or
operator to invoke at compile time.
• Achieved through:
• Function Overloading
• Operator Overloading
Friend Unary Operator
Overloading
■ In C++, a unary operator operates on one operand (e.g., -, ++, --, !).
■ When you want to overload a unary operator using a friend function, the
function needs to be non-member of the class but still have access to its
private data.

Why use a Friend Function?


■ Sometimes operator overloading needs to be done outside the class.
■ A friend function can access private and protected members of the class.
■ Unary operator can be overloaded as a friend if the left-hand operand is not
the object itself (e.g., -obj or !obj).
Friend Binary Operator
Overloading
■ A binary operator operates on two operands (e.g., +, -, *, /, etc.).
■ You can overload a binary operator using a friend function when you
need access to private members of both operands and the operator is
not a member of the class.
Identification of Classes and
Their Relationships

■ In Object-Oriented Programming, classes often relate to each


other.
The two common relationships are:
■ Aggregation
■ Composition
Aggregation (Weak HAS-A
relationship)
Definition:
• Aggregation means one class has a reference to another class.
• It represents a weak ownership.
• The contained object can exist independently of the container.

• Aggregation: A university has students, but students can exist


even without the university.
■ class School {
■ public:
■ Teacher* teacher; // Aggregation: just a
reference

■ School(Teacher* t) {
int main() {
■ teacher = t; Teacher t1("Mr. Ali");
School s(&t1); // School uses existing Teacher object
■ }
s.showTeacher();

■ void showTeacher() { return 0;


■ teacher->display(); }

■ }
■ };
Composition (Strong HAS-A
relationship)
Definition:
• Composition means one class owns another class.
• It represents strong ownership.
• If the container is destroyed, the contained object is also destroyed.

■ Composition: A human has a heart – if the human dies, the heart


cannot function separately.
class Engine { public:
void startCar() {
public:
eng.start();
void start() {
}
cout << "Engine started" << endl;
};
}
};
int main() {
Car c;
class Car { c.startCar();
private: return 0;
Engine eng; // Engine is part of Car }

• Car contains an Engine object.


• When Car is destroyed, the Engine is also destroyed automatically.
• This is Composition.
Difference
Feature / Aspect 🔷 Aggregation 🔶 Composition
“Has-a” relationship with “Has-a” relationship with
Definition
weak ownership strong ownership
Associated object is Contained object is
Object Creation
created outside the class created inside the class
Contained object can Contained object cannot
Lifetime Dependency
exist independently exist independently
The class uses another The class owns another
Ownership
class class
Deleting parent does not Deleting parent also
Deletion Impact
delete the child deletes the child
Use pointers or Use direct object
Syntax (C++)
references (Teacher*) declaration (Teacher)
A School has Teachers A Human has a Heart
Real-life Example
(but teachers can leave) (heart cannot exist alone)
Harder to reuse,
Easier to reuse
Reusability components are tied to
components
parent

You might also like