Site icon Afzal Badshah, PhD

Associations, Aggregation & Composition in OOP (C++)

Object-Oriented Programming is not only about creating classes and objects, it is also about how objects interact with each other. In the real world, nothing exists in isolation: a teacher teaches a subject, a car has an engine, a university contains departments. OOP represent these real-world relationships using: Association, Aggregation, and Composition.

Before we begin, remember the fundamental difference:

"is-a"  → Inheritance  
"has-a" → Class relationships (Association, Aggregation, Composition)

This tutorial explains “has-a” relationships clearly using real-world analogies and simple C++ programs.

Association

Association represents a general relationship between two independent objects.
One object uses or interacts with another, but neither owns the other.
They can exist without one another.

Definition

Association is a loose relationship between two independent classes where one object uses or interacts with another object.

For example, a Driver drives a Car.
The driver does not own the car; the interaction exists only while driving.
Both can exist independently.

Complete C++ Example — Association

#include <iostream>
#include <string>
using namespace std;

class car{
    private:
    string name;
    
    public:
    car(string c):name(c){}
    
    string getname(){
        return name;
    }
};

class driver{
    private:
    string na;
    
    public:
    driver(string d): na(d){}
    
    void drive(car C){
        cout<<na<<" is driving "<<C.getname();
    }
    
};


int main() {
car c("Toyoto");
driver d("Rehan");

d.drive(c);

    return 0;
}

Explanation

The driver and car exist independently.
The driver only uses the car through a function.
No object owns the other.
This is Association.

Aggregation

Aggregation is a whole–part relationship, but with weak ownership.
The container holds objects, but does not own their lifetime.

Definition

Aggregation is a whole–part relationship where the container holds objects that can exist independently outside the container.

For example,  a Classroom has Students, but the students can exist without the classroom.
If the classroom object is deleted, students still exist.

Complete C++ Example — Aggregation

#include <iostream>
#include <string>
using namespace std;
#include <vector>

class student{
    private:
    string name;
    
    public:
    student(string n):name(n){}
    
    string getname(){
        return name;
    }
};

class room{
    public:
    vector<student*> students;
    
    void add(student *s){
        students.push_back(s);
    }
    
    void show(){
        for(auto s : students){
            cout<<"students are: "<<s->getname();
        }
    }
};


int main() {
student s("ABC");
room r;
r.add(&s);
r.show();



    return 0;
}

Explanation

Students are created outside the classroom.
The classroom only stores pointers to them.
If the classroom is destroyed, students remain alive.
This represents Aggregation.

Composition

Composition is the strongest form of object relationship.
The container object owns the inner object and controls its lifetime.
If the outer object is destroyed, the part also gets destroyed.

Definition

Composition is a strong whole–part relationship where the inner object’s lifetime is dependent on the container object.

For example, a House has Rooms.
If the house is destroyed, the rooms cannot exist alone.
This is strict ownership.

Complete C++ Example — Composition

#include <iostream>
#include <string>
using namespace std;

class engine{
    public:
    string name;
    
    engine(){
        cout<<"enginer starts"<<endl;
    }  
};

class car{
    public:
    engine E;
    
    car(){
        cout<<"Car starts";
    }
};


int main() {
    car c;
    return 0;
}

Explanation

The engine object is created inside car.
When car is destroyed, engine is also destroyed.
The part cannot exist independently.
This is Composition.

Comparison Table

FeatureAssociationAggregationComposition
RelationshipInteraction onlyWhole–Part (weak)Whole–Part (strong)
OwnershipNo ownershipPartial ownershipFull ownership
Lifetime dependencyIndependentIndependentDependent
UML SymbolSimple lineHollow diamondFilled diamond
ExampleDriver → CarClassroom → StudentsHuman → Heart
Code MeaningTemporary usageStores external objectsOwns and creates internal objects

Exit mobile version