AGGREGATION IN C++
OBJECT ORIENTED PROGRAMMING
GROUP MEMBERS
Syed Zain Naqvi 23-CS-120
Muhazam Ali 23-CS-41
Muhammad Affan Asmat 23-CS-108
RE
CLASSICAL LITERATU
Muhammad Tayyab Anwar 23-CS-96
2
WHAT IS AGGREGATION ?
In C++, Aggregation is used to represent the ‘HAS-A’ relationship between two
objects. It is a type of association. If in a process, one class defines another class as
any entity reference then it is known as Aggregation. With the help of aggregation,
you can also reuse the class.
An object should define the following relationships to qualify as an aggregation:-
• Must be a part of the class.
RE
CLASSICAL LITERATU
• The member can belong to one or more classes at a time.
• Member is unknown about the existence of the object.
• The relationship is unidirectional. 3
WORKING OF AGGREGATION
IN C++
In the program, Aggregation helps to represent HAS-A relation between
the objects of 2 individual classes. Aggregation is more restrictive
compared to the association. Aggregation helps in making your program
code more readable and understandable to represent the relation. Using a
pointer variable, you can refer to the object of one class in the container
RE
CLASSICAL LITERATU
class object.
4
DECLARING AN AGGREGATED OBJECT
Aggregating object in C++ involves declaring an aggregated object
within a class. This allows us to compose complex objects by
combining simpler objects together.
1. Syntax.
RE
CLASSICAL LITERATU
2. Initialization.
3. Accessing Aggregated Object.
5
SYNTAX
• Declare the member variable of a class
as a Pointer.
or
Reference to aggregated object type.
• Use the appropriate datatype and
CLASSICAL LITERATURE
naming convention.
6
INITIALIZATION
• It can be Initialized in constructor of
Aggregated class.
• Use dynamic Memory Allocation (If using
Pointers)
or
CLASSICAL LITERATURE
Use Reference Assignment (If using
References)
7
ACCESSING AGGREGATED OBJECT
To access members or invoke function of
the Aggregated object, use the pointer or
reference syntax with the arrow (->) or dot
(.) operator respectively.
CLASSICAL LITERATURE
8
9
CLASSICAL LITERATURE
EXAMPLE:
10
CLASSICAL LITERATURE
COND:
11
CLASSICAL LITERATURE
COND:
DESCRIPTION OF EXAMPLE
This C++ program illustrates object aggregation via classes Tech and
Person. Tech encapsulates address details, while Person represents
individuals with associated addresses. Each Person object possesses
its own address, showcasing independence in object lifespans.
Through the display() method, the program outputs names alongside
corresponding addresses. This demonstrates the modular and flexible
CLASSICAL LITERATURE
nature of object aggregation in software design.
12
ADVANTAGES OF AGGREGATIONS IN C+
+
• Aggregation represents unidirectional relation between the objects of 2 classes. It is a
one-direction relation.
• Aggregation also helps in improving readability and reusability of the program code.
• It helps in initiating the relation between objects of 2 classes where one class is the
Whole class and other is the part of the class. It is useful in representing the HAS-A
CLASSICAL LITERATURE
relation.
• Aggregation helps in making your program code more readable and understandable to
represent the relation. 13
LIFETIME MANAGEMENT OF
AGGREGATED OBJECTS
Objects Aggregation in C++ allows for the creation and
destruction of aggregated objects independently of the
aggregating object. This provides flexibility and control
over the lifespan of individual components.
CLASSICAL LITERATURE
14
THANK YOU
OBJECT ORIENTED
PROGRAMMING