Aggregation and Its Implementation

Learn about aggregation, its importance, and its implementation using a real-life example.

What is aggregation?

In contrast to composition, an aggregation represents a part-whole relationship that is not responsible for the existence and lifespan of its parts. While the parts are contained within the whole, the relationship is unidirectional, and the parts can belong to multiple objects simultaneously. Unlike a composition, an aggregation does not create or destroy its parts when it is created or destroyed.

Real-life example

Let’s assume a university management system adheres to the following rules:

  • Departments are composed in the university, meaning they are created and destroyed by the university.
  • Teachers are also composed in the university, meaning they are created and destroyed by the university.
  • Teachers are aggregated in departments, as they can join multiple departments simultaneously.

Here’s a visual representation of the relationship that we need to create:

Press + to interact
Example of the teacher and department aggregation
Example of the teacher and department aggregation

Implementation of the university management system

Let’s demonstrate the above relationship in C++:

Press + to interact
Teacher.h
main.cpp
University.h
Department.h
#include "Teacher.h"
class Department {
private:
static const int capacity = 3;
int size;
Teacher* teachers[capacity];
string name;
public:
Department(string n) : name(n), size(0)
{
cout << "Department \"" << name << "\" has been created." << endl;
}
~Department()
{
cout << "Department \"" << name << "\" has been destroyed." << endl;
}
void addTeacher(Teacher* teacher)
{
if (size < capacity)
{
teachers[size] = teacher;
size++;
}
else
{
cout << "Couldn't add, as teachers capacity has reached."<<endl;
}
}
string getName()
{
return name;
}
};

The solution consists of three main classes: Teacher, Department, and University.

  • The Teacher class represents a faculty member with a name attribute. It handles the creation and destruction of individual teachers.

  • The Department class represents a department in the university with a name attribute and a static array to store Teacher objects. It has a capacity limit to store teachers, and new teachers can be added using the addTeacher() function. The Department is created and destroyed by the University.

  • The University class is responsible for managing departments and teachers. It maintains static arrays for departments and teachers with predefined capacities. The insertNewDepartment() function adds new departments to the university, and the insertNewTeacher() function adds new teachers. The assignTeacherToDepartment() function assigns a teacher to a department by invoking the addTeacher() function of the corresponding department. The university is responsible for creating and destroying departments and teachers.

  • The main() function demonstrates the usage of the university management system. It creates a University object, inserts departments, inserts teachers and assigns teachers to departments.

This implementation satisfies the composition relationship between the University, Department, and Teacher classes, where departments and teachers are owned and managed by the university. Additionally, it demonstrates the aggregation relationship between the Department and Teacher classes because teachers can be associated with multiple departments simultaneously.