In C++, aggregation is a process in which one class defines another class as any entity reference. It is another way to reuse the class. It is a form of association that represents HAS-A relationship. We use the concept of aggregation to represent the "HAS-A" relationship between two objects, i.e., to associate two objects.
Syntax of C++ Aggregation
It has the following syntax:
class Test
{
//instance variables
//instance methods
}
For example,
class A
{
Test* obj;
}
Here, the Test class is the container holding some instance variables and instance methods. There exists a Has-A relationship between class Test and class A. The object of class A holds the reference pointer for the objects of class Test.
Example Implementation of C++ Aggregation
Let us take an example to illustrate the aggregation in C++.
Example
#include <iostream>
#include <string.h>
using namespace std; //using standard namespace
class Test{
public:
int hno;
string city, state;
Test( int hno, string city, string state )
{ this->hno = hno;
this->city = city;
this->state = state;
}
};
class Address
{
private:
Test* addr;
public:
string name;
Address( string name, Test* addr )
{
this->name = name;
this->addr = addr;
}
void display( )
{
cout<< " Name : " << name << " \n " << " Hno : " << addr->hno << " \n " << " City : " << addr->city << " \n " << " State : " << addr->state << endl << " ------------------------- " << endl;
}
};
int main( ) {
Test obj1= Test( 45 ,"Los Angeles","USA" );
Test obj2 = Test( 65, " London","UK" );
Address a1 = Address( "Joy",&obj1 );
Address a2 = Address( "Jack",&obj2 );
cout << " Below are the details : " << endl << " ------------------------------ " << endl;
a1.display( );
a2.display( );
return 0;
}
Output:
Below are the details :
------------------------------
Name : Joy
Hno : 45
City : Los Angeles
State : USA
-------------------------
Name : Jack
Hno : 65
City : London
State : UK
-------------------------
Explanation:
In this example, we demonstrate the concept of aggregation, where the Address class contains a pointer to a Test object representing address details like house number, city, and state. In the main function, we create two Test objects and passes them to corresponding Address objects. After that, the display function prints the combined information for each person. It shows how one class can reference another without owning its lifetime.
Eligibility for an Object to Qualify Aggregation
In C++ , there are some relationships an object needs to define:
- An object must be a part of a class.
- The relationship should have no direction, i.e., it should be unidirectional.
- The members of the class can be part of one or more classes at a time.
- The members of the class should not be aware of the existence of the class.
Advantages of Using C++ Aggregation
Below are the advantages of the C++ Aggregation:
- It shows a unidirectional HAS-A relationship between objects of two classes.
- It consists of more restrictions as compared to the association.
- It makes the code more readable and understandable to represent the relationship.
- It also helps to enhance the code reusability.
Conclusion
In conclusion, C++ aggregation is a type of relation between objects of two separate classes. It represents the HAS-A type of relation where one class is the part class and the other is the whole class, where the 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.
C++ Aggregation MCQs
1) Choose the correct option for C++ Aggregation
- C++ Aggregation is a relationship where one class inherits from the other class.
- C++ Aggregation is a relationship where one class is part of another class.
- C++ Aggregation is a relationship where one class uses another class.
- C++ Aggregation is a relationship where one class is a friend of another class.
2) Choose the best option that describes the relationship in C++ aggregation.
- Inheritance
- Strong Ownership
- Weak Ownership
- No Ownership
3) Select the example of C++ Aggregation from the following options.
- A class 'Dog' has a method called bark
- A class 'Library' has many objects of 'book'.
- A class 'Car' that inherits from another class 'Vehicle'.
- A class 'Person' having a friend class 'Address'.
4) Choose the correct option from below.
- C++ Aggregation is the same as composition
- C++ Aggregation cannot be implemented in C++
- The lifetime of the C++ Aggregation part is dependent on the lifetime of the whole.
- The part of the C++ Aggregation can exist independently of the whole.
5) Choose the commonly used keyword used for implementing C++ Aggregation.
- Protected
- Public
- Private
- Not Applicable