C++ Aggregation
C++ Aggregation Definition: In C++, aggregation is a process in which one class (as an entity reference) defines another class. It provides another way to reuse the class. It represents a HAS-A relationship association, or it has class and relationship.
Note: HAS-A relation simply means dynamic or run-time binding.
To qualify as an aggregation, an object must define the following relationships:
- The member must be a part of a class.
- A member can belong to or more classes at a time.
- The member does not have any existence managed by the object.
- The member is unknown about the object's existence.
- The relationship is uni-directional.
Collectively, aggregation is a part-whole relationship, where the parts (member) are contained within the entire uni-directional relationship. However, unlike composition, members can belong to one object at a time. The entire object doesn't need to be responsible for the existence and lifespan of the members. In simple and sober language, aggregation is not responsible for creating or destroying the members or parts.
For instance, consider a relationship between a person and their home address. However, the same address may belong to more than one person at a time in some significant order. Although, it is not managed by the person; the address existed even before the man and will tend to exist even after it. Additionally, a person knows where he/she lives, but the address does not have any information about what person lives. Therefore, it is an example of an aggregate relationship.
Let us dive into the syntax of aggregation.
Syntax:
Class PartClass { //instance variables //instance methods } class Whole { PartClass* partclass; }
In the above syntax, the whole class represents the class that is a container class. The container class is considered as the class for the other part class, which is again contained in the entire class's object. Here, each object of class as the whole can hold the reference pointer of the part class's object.
Implementing Aggregations
In the previous example, we came across how address and person serve the aggregation relation. Similarly, consider the points which help in understanding how aggregations are implemented:
Compositional Nature: Aggregation resembles compositions because they represent the part-whole relationship. They are implemented identically in the same fashion with a slight difference in the semantics.
Member Constraints: In aggregation, we also add parts as member variables. However, these members are references or pointers that point to the object created outside the scope of the class. Thus, aggregation takes objects which point as constructor parameters. The objects and sub-objects are later added by functions and operations.
Existence: Since the parts may exist outside the scope, class destruction may lead to member variable destruction (but not deletion). The parts will exist consequently.
Let us now look at a programming example to understand aggregation better.
#include<bits/stdc++.h> using namespace std; class Address { public: int houseNo; string colony,city, state; Address(int hno, 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(007 ,"Ampitheatre Park","China Gate","San Fransisco, CA"); Person p1 = Person("Andrew's Address:->",&add1); Person p2 = Person("Stacy's Address: ->",&add1); p1.display(); p2.display(); } return 0;
Output:

Explanation:
In the above code, Here Person has an instance variable name that tells the name of the person and a pointer variable to address the class object. Address class object has variables such as House, street, city, and state. Here we have two persons, Raj and Seema, living at the same address, thus share the same address object add1.
The class Person is defined with an instance variable name which tells the name of the person, and the pointer associated with it tells the address of the class object. We have initially assigned two persons, namely Andrew and Stacy, who share the same address as defined in the class with the address as a1. The output address for both Andrew and Stacy will thus be the same.
Advantages of C++ Aggregates:
- The aggregation process shows the bi-directional relationship between objects of different classes.
- The HAS-A relationship provides dependencies that are easy to understand and co-relate.
- Aggregation provides code reusability and readability. Once the whole class is created, the reference provides the proper access to part classes.
- It defines a unidirectional relationship that to correctly associates the whole class to the part class.