Site icon TechVidvan

Aggregation in C++

C++ Aggregation

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:-

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:-

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++

Summary

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

Exit mobile version