Aggregation in C++

The C++ programming language offers various exciting and useful features to programmers. A programmer can get great benefits while using the C++ language. Aggregation is one of the exciting features of C++. Let us learn more about it.

What is Aggregation in C++?

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.
  • 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.

NOTE:- For creating or destroying the members or parts, Aggregation is not responsible.

Let’s understand through an example between a person and the home address. This address may belong to other persons as well. The address existed before the man and will exist for forever. The person knows where he lives but the address does not contain any information about what person lives. It is an example of an aggregate relationship.

Syntax:-
To put it short, aggregation is a ‘HAS-A’ relationship between the objects of 2 individual classes. Aggregation restricts some situations of associations.

Class ClassPart
{
//instance variables
//instance methods
}
class Tech
{
ClassPart* partclass;
}

In the above, the Tech class represents the class that is a container class for other ClassPart. And it is included in the object of the Tech class. The Tech class’s each object holds a reference pointer to the object of the ClassPart.

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 class object.

Implementing Aggregations in C++

Below are the points which will help you to understand the implementation of aggregations:-

  • Compositional Nature:– Aggregation resembles composition. With a small difference in semantics, both are implemented identically.
  • Member Constraints:– You can also add parts as member variables in aggregation. It also takes constructor parameters.
  • Existence:– The parts will exist consequently. Class destruction can lead you to member variable destruction.

Example:- C++ Aggregation

#include <iostream>
#include<string.h>
using namespace std;
class Tech{
public:
int house;
string city, state;
Tech(int house_no, string city, string state)
{ this->house = house_no;
this->city = city;
this->state = state;
}
};
class Person
{
private:
Tech* address;
public:
string name;
Person(string name, Tech* address)
{
this->name = name;
this->address = address;
}
void display()
{
cout<< name<< " "<< " "<< address->house<<" "<<address->city<< " "<<address->state<<endl;
}
};
int main(void) {
Tech add1= Tech(112 ,"Bandra","Mumbai");
Tech add2 = Tech(222, "Jahanpanah","New Delhi");
Person p1 = Person("Raj",&add1);
Person p2 = Person("John",&add2);
cout << "Name of the Person" << " and " << "Address"<< endl<<endl;
p1.display();
p2.display();
return 0;
}

Output:-

Name of the Person and Address

Raj 112 Bandra Mumbai
John 222 Jahanpanah New Delhi

In the above example, the Tech class object contains variables such as House, City, State. We have 2 people Raj and John. They are living in different addresses. The Person class has an instance variable that tells the name of the person. And it also has a pointer variable to the address class object.

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 relation.
  • Aggregation helps in making your program code more readable and understandable to represent the relation.

Summary

In this tutorial, we discussed aggregation in C++, its working, implementation and advantages. Hope you liked it.