
Composition and Aggregation in C++
In Object-Oriented Programming (OOP), Composition and Aggregation describe relationships between classes, specifically how objects are associated with one another. Both are forms of the “has-a” relationship, but they differ in strength and dependency. So in this article we will see the concept of Composition and Aggregation in C++ and also check the difference between Composition and Aggregation.
What is Composition?
Contents
- What is Composition?
- Key Characteristics of Composition:
- Example Code: Composition in C++
- Explanation of Composition Code:
- What is Aggregation?
- Key Characteristics of Aggregation:
- Example Code: Aggregation in C++
- Explanation of Aggregation Code:
- Benefits of Using Composition and Aggregation
- Difference in Composition and Aggregation:
- Key Differences:
- Real-World Analogies:
- Choosing Between Composition and Aggregation:
- Summary
- Key Takeaways
- Share this:
- Like this:
- Related
Composition represents a strong relationship between two classes. It is used when an object (child) is a part of another object (parent), and the child object cannot exist independently of the parent. The lifecycle of the child object is tied to the lifecycle of the parent. If the parent object is destroyed, the child object is also destroyed.
Key Characteristics of Composition:
- Strong Dependency:
- The parent object owns the child object. The child cannot exist without the parent.
- Lifecycle Management:
- The lifecycle of the child object is bound to the parent. Destroying the parent automatically destroys the child.
- Real-World Example:
- A car and its engine. The engine is a part of the car, and without the car, the engine serves no purpose.
Example Code: Composition in C++
#include <iostream>
#include <string>
using namespace std;
// Engine class representing a part of a Car
class Engine {
private:
string type;
public:
// Constructor to initialize the engine type
Engine(const string& type) : type(type) {}
void start() const {
cout << "Engine of type " << type << " is starting." << endl;
}
string getType() const {
return type;
}
};
// Car class using Composition
class Car {
private:
Engine engine; // Car "has-an" Engine (composition relationship)
public:
// Constructor to initialize the car with an engine
Car(const string& engineType) : engine(engineType) {}
void startCar() const {
cout << "Car is starting." << endl;
engine.start(); // Car depends on the engine's functionality
}
void showDetails() const {
cout << "Car with engine type: " << engine.getType() << endl;
}
};
int main() {
// Create a car with an engine
Car car("V8");
// Start the car and display its details
car.startCar();
car.showDetails();
return 0;
}
Explanation of Composition Code:
Engine
Class:
Represents an engine with:
Atype
property to store the type of engine.
MethodsgetType()
andstart()
to provide engine-specific functionalities.Car
Class:
Represents a car containing anEngine
object. It has:
A constructor to initialize theCar
object with anEngine
.
A methodstartCar()
to start the car, which also starts the engine.
A methodshowDetails()
to display the car’s engine type.- Key Points:
TheCar
class fully owns theEngine
object.
TheEngine
object is created when theCar
object is initialized and destroyed when theCar
object is destroyed.
What is Aggregation?
Aggregation represents a weaker relationship between two classes. It is used when an object (child) is associated with another object (parent), but the child object can exist independently of the parent. The lifecycle of the child object is not tied to the lifecycle of the parent.
Key Characteristics of Aggregation:
- Weak Dependency:
- The parent object holds a reference to the child object, but the child can exist without the parent.
- Independent Lifecycle:
- The child object’s lifecycle is independent of the parent. Destroying the parent does not affect the child.
- Real-World Example:
- A house and its residents. The residents can exist independently of the house.
Example Code: Aggregation in C++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Person class representing a resident
class Person {
private:
string name;
public:
// Constructor to initialize the person's name
Person(const string& name) : name(name) {}
string getName() const {
return name;
}
};
// House class containing a list of residents (Aggregation)
class House {
private:
vector<Person> residents; // A house "has-a" list of residents
public:
// Method to add a resident to the house
void addResident(const Person& person) {
residents.push_back(person);
}
// Method to display all residents
void showResidents() const {
cout << "House residents:" << endl;
for (const auto& person : residents) {
cout << person.getName() << endl;
}
}
};
int main() {
// Create Person objects
Person p1("Alice");
Person p2("Bob");
// Create a House object
House house;
// Add residents to the house
house.addResident(p1);
house.addResident(p2);
// Display residents of the house
house.showResidents();
return 0;
}
Explanation of Aggregation Code:
Person
Class:
Represents a person with aname
. It has:
A constructor to initialize the name.
A methodgetName()
to retrieve the person’s name.House
Class:
Represents a house containing multiple residents (a collection ofPerson
objects). It has:
A methodaddResident()
to add aPerson
object to the house.
A methodshowResidents()
to display the names of all residents.- Key Points:
TheHouse
class does not ownPerson
objects. It merely stores references (in this case, values) to existingPerson
objects.Person
objects (p1
,p2
) exist independently of theHouse
object.
Benefits of Using Composition and Aggregation
- Flexibility:
Both approaches provide flexibility in structuring relationships between classes, promoting modular design. - Reusability:
Classes designed with Composition or Aggregation can be reused in different contexts without modification. - Encapsulation:
Composition and Aggregation encourage encapsulating functionality in smaller classes, improving code readability. - Maintainability:
Breaking down systems into smaller, related components makes them easier to maintain and extend.
Difference in Composition and Aggregation:
Aspect | Composition | Aggregation |
---|---|---|
Ownership | The parent object owns the child object. | The parent object holds a reference to the child object. |
Lifecycle Dependency | The child object’s lifecycle is tied to the parent. | The child object’s lifecycle is independent of the parent. |
Strength of Relationship | Strong | Weak |
Example | A car “has-an” engine. | A house “has-a” resident. |
Key Differences:
- Ownership:
- In Composition, the parent object owns the child object.
- In Aggregation, the parent object does not own the child object, and the child can exist independently.
- Lifecycle Management:
- In Composition, when the parent object is destroyed, the child object is also destroyed automatically.
- In Aggregation, the child object’s lifecycle is independent, and destroying the parent object does not affect the child object.
- Relationship Strength:
- Composition is a stronger relationship, indicating that the child is an integral part of the parent.
- Aggregation is a weaker relationship, suggesting that the child can exist on its own without the parent.
Real-World Analogies:
- Composition: A heart is a part of the human body. Without the body, the heart cannot exist.
- Aggregation: A book belongs to a library, but it can be removed from the library and still exist as an independent object.
Choosing Between Composition and Aggregation:
The choice between Composition and Aggregation depends on the level of dependency between the objects. If the child object’s lifecycle depends on the parent, then Composition is the right choice. If the child object can exist independently of the parent, then Aggregation is more appropriate.
By understanding and correctly implementing Composition and Aggregation, developers can create more flexible, maintainable, and logically structured software systems. These concepts help in modeling real-world scenarios accurately and ensure better code organization and reuse.
Summary
In object-oriented programming (OOP), Composition and Aggregation are two key concepts that describe how classes relate to each other. Both represent a “has-a” relationship, but they differ in the strength of the relationship and how objects are dependent on one another.
- Composition represents a strong relationship where one object (the parent) completely owns and controls the lifecycle of another object (the child). The child object cannot exist without the parent object. For example, a Car “has-an” Engine, and the engine’s existence is tightly bound to the car.
- Aggregation, on the other hand, represents a weaker relationship. While the parent object holds a reference to the child object, the child object can exist independently of the parent. For example, a House “has-a” Resident, but the resident can live elsewhere, independent of the house’s existence.
Key Takeaways
- Composition is used when objects are tightly coupled, and their lifecycles are interdependent.
- Aggregation is used when objects are loosely coupled and can exist independently.
- Understanding the differences between Composition and Aggregation helps design better and more flexible class relationships, leading to cleaner and more maintainable code.