C++

Aggregation in C++

C++ is a very performant language with outstanding memory management. It is also very efficient. Additionally, C++ offers the concepts of OOP which helps to organize and manage the development. In C++, we can apply aggregation which is the association between two classes. An association known as “aggregation” occurs when one class carries one or more occurrences of an alternative class. We can also say that aggregation symbolizes the “HAS-A” kind of association in which there is a part class and an entire class, and an instance of the entire class contains a reference variable that points to the part class object. Here, we will apply aggregation in our C++ codes.

Example 1:

Here is the C++ code in which we will do the aggregation. We begin our C++ code with the “<iostream>” header file. We must put the “#include” keyword with the header file. The header files are utilized to save us from creating the code for every scenario. It facilitates a reduction in both code complexity and lines.

Additionally, it allows us to reuse the functions that are specified in the header files in various contexts. After this, we have the “namespace std” which we add along with the “using” keyword. Now, we create a “class” named “School”. Then, we utilize the “public” keyword. Then, we declare three variables named “schoolName”, “schoolCity”, and “schoolState”. These variables’ datatype is “string”.

After this, we have a public constructor of this class which is “School” and pass the parameters which are “string schoolName, string schoolCity, string schoolSate” in it. Now, we utilize the “this” keyword to initialize the value of the “School” class member’s name as the parameter’s name that we added in our given constructor. We initialize the “schoolName”, “schoolCity”, and “schoolState” here by utilizing the “this” keyword.

After this, we create one more class named “Student”. In this “Student” class, we create the “School* std_address” as private by placing the “private” keyword. Here, in the “Student” HAS-A School, we place the “public” keyword and then declare the “std_id” with the integer data type. We declare one more variable with the “std_name” name of the “string” variable.

After this, we have a “School” constructor with three parameters. Now, we use the “this” keyword and initialize the “std_id”, “std_name”, and “std_address” with the help of “this”. Then, we put the “void display()” and type the “cout” statements inside it. We type all the information that we want to display as the output.

After this, we place the “int main()” function which is the driver code of C++. Then, we create two objects of our “School” and “Student” classes named “add_1” and “std_1”, respectively. In the first object, “add_1”, we place the public constructor which is “School” and add the values as the parameters. Also, we insert the values as the parameters to the “Student” public constructor in the second object which is “std_1”. We pass the address of “add_1” to the “Student” constructor as the third parameter. After this, we utilize the “display()” method with the “std” class object to display it on the screen.

Code 1:

#include <iostream>
using namespace std;  
class School {  
    public:  
   string schoolName, schoolCity, schoolState;    
     School(string schoolName, string schoolCity, string schoolState)    
    {    
        this->schoolName = schoolName;  
        this->schoolCity = schoolCity;  
        this->schoolState = schoolState;    
    }    
};  
class Student    
    {    
        private:  
        School* std_address;  
        public:  
        int std_id;    
        string std_name;    
        Student(int std_id, string std_name, School* std_address)    
       {    
           this->std_id = std_id;    
           this->std_name = std_name;    
           this->std_address = std_address;    
       }    
     void display()    
       {    
           cout<<std_id <<" "<<std_name<< " "<<    
             std_address->schoolName<< " "<< std_address->schoolCity<< " "<<std_address->schoolState << endl;
       }    
   };  
int main(void) {  
    School add_1= School("ABC-school, Sctr-5", "XYZ", "America");    
    Student  std_1 = Student(48,"James",&add_1);    
            std_1.display();  
   return 0;  
}

Output:

Here is the result of the aggregation that we did in our previous C++ code. It’s an extra method of reusing the class:

Example 2:

The code contains two header files – “<iostream>” and “<string.h>”. The “<iostream>” is utilized for input\output and the “<string.h>” is used here that the carries functions to operate the strings. Then, we place the “namespace std” and create a class named “SoftwareEngineer” below this. In this class, we declare some public variables which are “office” with “int” data type and “off_city” and “off_state” with “string” data type. After this, we have a constructor named “SoftwareEngineer” with three parameters. The “this” keyword is used to initialize the “office”, “off_city,” and “off_state” variables in this instance.

We then have another class called “Tester” after that. We make the ” SoftwareEngineer* T_address ” private in this “Tester” class by adding the “private” keyword. The “public” keyword is now inserted and the string data type which is “T_name” is declared. The “Tester” constructor follows two parameters: “string T_name” and “SoftwareEngineer* T_address”.

Now, with the aid of “this” keyword, we initialize the “T_name” and “T_address”. After this, we put “void display()” and use the “cout” which aids in printing in C++. Now, we put the driver code and create two objects of the “SoftwareEngineer” class inside this. These objects are named “se1” and “se2”. In “se1”, we add the values inside the “SoftwareEngineer” constructor which are “368”, “London”, and “UK”. These are the values of the parameter of this constructor.

In the same way, we add the values inside the constructor which we placed in the second object, “se2”. Here, we insert “459, New York, USA”. Now, we also create the object of the second class, “Tester”, which are “t1” and “t2”. We initialize these objects with the constructor of the “Tester” class. We insert two parameters in the “Tester” constructor, so we pass two values to this constructor. In the “t1” object, the “Tester” constructor contains the address of “se1” as the second parameter’s value. In the “t2” object, the “Tester” constructor includes the address of “se2” as the second parameter’s value. After this, we display “t1” as well as “t2” by utilizing the “display()” method.

Code 2:

#include <iostream>
#include<string.h>

using namespace std;
class SoftwareEngineer{
public:
int office;
string off_city, off_state;
SoftwareEngineer(int office_no, string off_city, string off_state)
{ this->office = office_no;
this->off_city = off_city;
this->off_state = off_state;
}
};
class Tester
{
private:
SoftwareEngineer* T_address;
public:
string T_name;
Tester(string T_name, SoftwareEngineer* T_address)
{
this->T_name = T_name;
this->T_address = T_address;
}
void display()
{
cout<< T_name<< " "<< " "<< T_address->office<<" "<<T_address->off_city<< " "<<T_address->off_state<<endl;
}
};
int main(void) {
SoftwareEngineer se1= SoftwareEngineer(368 ,"London","Uk");
SoftwareEngineer se2 = SoftwareEngineer(459, "New York","USA");
Tester t1 = Tester("Peter",&se1);
Tester t2 = Tester("William",&se2);
cout << "Name of the Tester" << " and " << "Office no, city , state"<< endl<<endl;
t1.display();
t2.display();
return 0;
}

Output:


This is another example of aggregation in C++ that shows all the data that we previously printed. We might notice that the aggregation enhances the computer code’s accessibility and reusability.

Conclusion

We explored the “aggregation” technique in this guide in detail. We went through this concept thoroughly. We illustrated two unique examples in which we applied the aggregation concept and showed the outcomes which demonstrated how the aggregation concept works in C++ programming. We explained that the method of repeating a class in a relation is known as aggregation. We also showed that aggregation facilitates the representation of an association in your program coding by making it easier to read and comprehend.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.