Updated April 15, 2023
Introduction to C++ Aggregation
Aggregation is a type of association that is used to represent the “HAS-A” relationship between two objects. This is a subclass for a relation type association. Where the association is loosely coupled that exists between two classes where a relation exists, but aggregation restricts some situations of associations. This type of relation is like a whole/Part relationship type where one class owns another class thus one class object is a part of another class object. But the lifetime of a part class does not depend on the lifetime of the whole class neither whole class can exist without an object of part class.
For Example – There are two classes Address and Person, Each Person has an address.
Syntax:
Aggregation is a way to represent HAS-A relation between the objects of 2 individual classes.. It is a subtype of association type of relation but more restrictive.
Class PartClass{
//instance variables
//instance methods
}
class Whole{
PartClass* partclass;
}
Explanation: In the above syntax, the Whole class represents the class that is a container class for other Part class that is contained in the object of the whole class. Here each object of Whole class holds a reference pointer to the object of the Part class.
For example – BUS HAS-A Engine. Here Bus is a container class. Part class is a class whose object is contained within the objects of a container class. An object of the Part class can be referred in more than 1 object of Whole class and also a lifetime of a contained class object does not depend on the lifetime of the existence of the object of a container class.
How does Aggregation work in C++?
Aggregation is a relation type that helps to represent Has-A relation between objects of 2 individual classes in the program. It Is a subform of association relation and is more restrictive as compared to the association. It helps to make our code more readable and understandable to represent the relation in the programming language, in the same manner, it is explained in our day to day lives.
Here object of one class is referred to using a pointer variable present in the container class object.
Example: Person has Address is represented using two classes Person and Address. Since as we can see, Address has a Person is meaningless thus Person is container class and Address is a class whose object is contained within the container class object.
Here we can also see, the lifetime of an object of address class does not depend on the lifetime of the object of Person class. Thus objects are not tightly coupled. Also, one address can easily be associated with more than a Person since more than 1 person can live on the same address. Thus the object of address class can be associated with more than one object of Person class.
In this way, one can easily establish HAS-A relation between objects of 2 classes.
Example of C++ Aggregation
Let us try to illustrate the implementation of the aggregation type of relation between 2 classes Person and address using a C++ program.
Code:
#include <iostream>
#include<string.h>
using namespace std;
class Address {
public:
inthouseNo;
string colony,city, state;
Address(inthno, string colony, string city, string state)
{ this->houseNo = hno;
this->colony=colony;
this->city = city;
this->state = state;
}
};
class Person
{
private:
Address* address;
public:
string name;
Person(string name, Address* address)
{
this->name = name;
this->address = address;
}
void display()
{
cout<< name<< " "<< " "<< address->houseNo<<" "<<address-> colony<<" " <<address->city<< " "<<address->state<<endl;
}
};
int main(void) {
Address add1= Address(868 ,"Mahavir Colony","Jahagirpuri","New Delhi");
Person p1 = Person("Raj",&add1);
Person p2 = Person("Seema",&add1);
p1.display();
p2.display();
return 0;
}
Output:
Explanation: Here Person has instance variable name which tells the name of the person and a pointer variable to address class object. Address class object has variables such as House, street, city, and state. Here we have 2 persons Raj and Seema living on the same address thus share the same address object add1.
Advantages of C++ Aggregation
- Aggregation helps establishing a relation between objects of 2 individual classes where one is Whole class and the other is a part class. It is used to code ‘HAS-A’ relation between objects of two classes where once object of the part class can be associated with more than 1 object of Whole class and does not have existence dependency on it.
- This type of relation is a special form of relation named as association where objects does not have any dependency on each other and show bidirectional relation between objects of different classes.
- Aggregation type of relation represents uni-directional relation between objects of 2 classes like car and garage where car is a part of garage same car can be parked in any other garage as well. Thus defines its one direction relation.
- It also helps to improve the reusability of the code. Once objects have been created, any Whole class object is capable of holding a reference to the object of any of the Part class.
- It also helps to improve the readability of the code as the relation between 2 classes can be made more understandable once it is in the form of HAS-A relation the same we interpret them in our day to day life. such as Bus HAS-A Engine and Department HAS-A Teacher.
Conclusion
Aggregation is a type of relation between objects of two individual classes. It represents the ‘HAS-A’ type of relation where one is part class and the other is the whole class, where reference variable pointing to the part class object is present in the object of the whole class. Here a lifetime of a part class object is independent of the lifetime of the whole class object.
Recommended Article
This is a guide to the C++ Aggregation. Here we discuss the Introduction of C++ Aggregation and its different Advantages along with Examples and its Code Implementation. you can also go through our suggested articles to learn more –