1Definition
Object Relationships define how independent classes interact and structure software architectures. Association represents a general structural relationship. Aggregation is a specialized weak HAS-A relationship where child objects can exist independently of the parent container. Composition is a strong HAS-A relationship where child objects cannot exist without the parent. Inheritance represents an IS-A relationship.
2Simple Explanation
- IS-A Relationship (Inheritance): A Dog IS-A Mammal. If the Mammal concept vanishes, the Dog concept vanishes.
- HAS-A Relationship (Aggregation): A Department HAS-A Teacher. If the Department is closed down, the Teachers still exist and can move to another department.
- HAS-A Relationship (Composition): A House HAS-A Room. If the House is demolished, the Rooms are destroyed simultaneously.
3Real-Life Example / Analogy
- Aggregation: A
Carand itsDriver. The car has a driver, but if the car is scrapped, the driver continues to live independently. - Composition: A
Humanand theirHeart. If the human organism ceases to exist, the heart ceases to function independently.
4Diagram / Visual Representation
5Working / How It Works
- Aggregation Implementation: Storing references or pointers to external independent objects (
Teacher* teacher;). - Composition Implementation: Storing direct object member instances inside the container class (
Room room;).
6Types / Components / Characteristics
- Relationship Matrix:
| Dimension | Inheritance (IS-A) | Aggregation (HAS-A) | Composition (HAS-A) |
|---|---|---|---|
| Coupling | Very Tight | Loose | Tight |
| Lifetime Link | Shared type lifetime | Independent lifetimes | Dependent lifetime |
| Implementation | class B : public A |
Pointer member (A* obj) |
Direct object member (A obj) |
7Solved Examples / Problems
Problem Statement: Demonstrate Aggregation in C++ by modeling a Department containing a pointer reference to a Professor.
#include <iostream>
#include <string>
using namespace std;
class Professor {
public:
string name;
Professor(string n) : name(n) {}
};
// Aggregation: Department HAS-A Professor (Independent lifetime)
class Department {
private:
Professor* prof; // Pointer reference
public:
Department(Professor* p) : prof(p) {}
void display() {
cout << "Department Professor: " << prof->name << endl;
}
};
int main() {
Professor* p1 = new Professor("Dr. Smith");
{
Department dept(p1);
dept.display();
} // Department goes out of scope here
// Professor still exists independently!
cout << "Professor outside Dept: " << p1->name << endl;
delete p1;
return 0;
}Output:
Department Professor: Dr. Smith
Professor outside Dept: Dr. Smith8Advantages, Disadvantages & Applications
- Advantages:
- Aggregation offers loose coupling and maximum code reusability without class pollution.
- Disadvantages:
- Pointer aggregation requires manual lifetime management to avoid memory leaks.
- Applications: Enterprise organizational hierarchy systems, GUI container component trees.
9Important Points to Remember
- Prefer Composition/Aggregation over Inheritance when modeling dynamic runtime relationships ("Favor Composition over Inheritance").
10Test Your Understanding
- Conceptual Question: What distinguishes Aggregation from Composition regarding child object lifetime?
Show answer
In Aggregation, child objects can exist independently of the parent container; in Composition, child object lifetimes are strictly bound to the parent container. - MCQ: What relationship is modeled when a
Studentclass inherits from aPersonclass?- A) HAS-A
- B) IS-A
- C) USES-A
- D) FRIEND-A
Show answer
B) IS-A - Application Question: Identify whether a
Libraryand itsBooksrepresent Aggregation or Composition.Show answer
Aggregation (if books can be checked out or moved independently) or Composition (if books are permanently bound to the library catalog instance).
