Topic 13

Object Relationships: Aggregation & Association (HAS-A vs. IS-A)

3 min read3 questions to test yourself

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 Car and its Driver. The car has a driver, but if the car is scrapped, the driver continues to live independently.
  • Composition: A Human and their Heart. If the human organism ceases to exist, the heart ceases to function independently.

4Diagram / Visual Representation

HAS-A: Object relationship

has an

uses / refers to

Car

Engine

Driver

IS-A: Inheritance

Vehicle

Car

Bike

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.

C++
#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:

Output
Department Professor: Dr. Smith
Professor outside Dept: Dr. Smith

8Advantages, 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

  1. 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.
  2. MCQ: What relationship is modeled when a Student class inherits from a Person class?
    • A) HAS-A
    • B) IS-A
    • C) USES-A
    • D) FRIEND-A
    Show answer
    B) IS-A
  3. Application Question: Identify whether a Library and its Books represent 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).

Finished this topic?

0 of 14 topics read in Object-Oriented Programming

Handwritten notes for the exam

Unit-wise SPPU notes and PYQs for this subject.

Helpful? Share it with your class

Free for everyone, no login needed.

Share topic