CPP Tutorial

CPP examples programs

CPP Aggregation

The process of defining a class by some other class as any entity reference is termed as aggregation in CPP programming. In other words, aggregation can be defined as the process of reusing a class in a form of association. In CPP, the term aggregation means “HAS-A relationship.”

Example:

#include <iostream.h>
using namespace std;
 
class Student
{
public:  
string Name;  
float ID;
Student(string Name, float ID) 	 
{    
this-> Name = Name;    
this-> ID = ID;   
}
};
class Details   
{    
private:  
Student* std;  
public:  
int stdclass;    
string section;    
Details(int stdclass, string section, Student* std)    
{    
this->stdclass = stdclass;    
this->section = section;    
this->std = std;    
}    
 
void detail()
{
cout << "Student Class : " << stdclass << endl;
cout << "Student Section : " << section << endl;
cout << "Student Name : " << std->Name<< endl;
cout << "Student ID : "<< std->ID << endl;  
}
};
int main()
{
Student s1 = Student("Khush", 1);    
Details d1 = Details(6, "A", &s1);  
d1.detail(); 	 
return 0;
}

Output

Student Class : 6    
Student Section : A  
Student Name : Khush  
Student ID : 1