Understanding Aggregation in C++: A Beginner's Guide to Class Relationships

Introduction

Aggregation is a specialized form of association that represents a "has-a" relationship between two classes. It models a whole-part relationship in which one object contains or references another object without taking ownership of it.

Unlike composition, aggregation represents a weak ownership relationship. The parent object maintains references or pointers to child objects, but it does not control their lifetime. Child objects are created independently and can continue to exist even after the parent object has been destroyed.

Aggregation is commonly implemented in C++ using pointers or references to externally created objects. This design promotes flexibility and loose coupling between related classes.

This document presents two practical examples demonstrating aggregation relationships in C++.

Example 1: Department and Professor Relationship

This example demonstrates an aggregation relationship between a university department and its professors. A department contains professors, but professors can continue to exist independently even if the department is removed.

Program


Explanation

The program defines two independent classes: Professor and Department.

The Department class maintains a vector of pointers to Professor objects. The professors are created independently outside the department and are merely referenced by the department through pointers. This establishes an aggregation relationship because the department does not own the professor objects.

Inside the main() function, two professor objects are created first. A department object is then created, and both professors are added to the department using the addProfessor() function.

After displaying the department information, the department object is deleted. Despite the deletion of the parent object, both professor objects continue to exist and remain fully accessible. Finally, the professor objects are deleted separately.

This example demonstrates the key characteristic of aggregation: the lifetime of child objects is independent of the lifetime of the parent object.

Example 2: CourseSyllabus and Textbook Relationship

This example demonstrates aggregation using a pointer to a textbook object. The course syllabus references a textbook, but it does not own it.

Program 


Explanation

The Textbook class represents an independently existing object that stores information about a book.

The CourseSyllabus class stores a pointer to a Textbook object rather than creating or owning the textbook itself. This pointer simply establishes a relationship between the syllabus and the textbook.

Inside the main() function, the textbook object is created first. A CourseSyllabus object is then created inside a local scope and receives a pointer to the textbook.

When execution leaves the scope, the CourseSyllabus object is automatically destroyed. However, the textbook object continues to exist because it was created independently and is not owned by the syllabus.

This demonstrates another example of aggregation, where the child object remains alive even after the parent object has been destroyed.

Characteristics of Aggregation

PropertyDescription
Relationship TypeRepresents a weak "has-a" or whole-part relationship.
OwnershipThe parent object references the child object but does not own it.
Object LifecyclesParent and child objects have independent lifecycles.
ImplementationUsually implemented using pointers or references to externally created objects.
Memory ManagementChild objects must be managed independently by the code that creates them.

Conclusion

Aggregation is a specialized form of association that models a weak "has-a" relationship between objects. The parent object references one or more child objects without taking ownership of them, allowing both to exist independently. Because the lifetime of the child object does not depend on the lifetime of the parent, aggregation provides flexibility, loose coupling, and reusable object design. As demonstrated in the Department–Professor and CourseSyllabus–Textbook examples, aggregation is commonly implemented in C++ using pointers or references to externally created objects, making it well suited for representing independent yet related entities in Object-Oriented Programming.