In the C++ programming language, aggregation refers to the scenario where one class incorporates another class as a reference to any entity. This methodology serves as an alternative approach to class reuse, embodying a form of association that signifies a HAS-A relationship. The concept of aggregation is employed to illustrate the relationship of "HAS-A" between two objects, essentially establishing an association between them.
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 serves as the container that encapsulates various instance variables and methods. A Has-A relationship is established between the Test class and class A, where instances of class A contain reference pointers to objects of the Test class.
Example Implementation of C++ Aggregation
Let's consider an example to demonstrate 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 illustration, we showcase the idea of aggregation, where the Address class includes a reference to a Test object that holds address specifics such as the house number, city, and state. Within the main method, we instantiate two Test objects and assign them to respective Address objects. Following this, the display method outputs the consolidated details for each individual. This scenario exemplifies how one class can refer to another class without being responsible for managing its lifespan.
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 summary, C++ aggregation defines a connection between instances of distinct classes, illustrating a HAS-A relationship in which one class serves as the component class while the other acts as the composite class. This involves having a reference variable within the composite class object that points to the component class object, allowing the component object to exist independently of the composite object's lifespan.
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.
b) In C++, aggregation represents a relationship where one class is contained within 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