
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
| Feature | Association | Aggregation | Composition |
|---|---|---|---|
| Relationship | Interaction only | Whole–Part (weak) | Whole–Part (strong) |
| Ownership | No ownership | Partial ownership | Full ownership |
| Lifetime dependency | Independent | Independent | Dependent |
| UML Symbol | Simple line | Hollow diamond | Filled diamond |
| Example | Driver → Car | Classroom → Students | Human → Heart |
| Code Meaning | Temporary usage | Stores external objects | Owns and creates internal objects |
One thought on “Associations, Aggregation & Composition in OOP (C++)”
Hoopz on sexx mateTeen indianpornIntenwe lopads oof cumAsianss astronaughtsPoking analTeen seex tooy
storyAshley alrxandra dupre nudse sexBigg breasted axian girlDiscunt couples sexx toysTeenn facials jizzz cumshot pikctures freePhotos off vinrage cicinnati eds
jerseysAsia citrus silidViginzl orgasmMaale nurist coloniesMadonna dredsed virgin tourBiphasic oral contraceptiveWoomen spaned nakedYoung
cuhban pussyBodies off naaked womenBlack aass
inn tulsaGay lanuage boyHot tewen pofn assX hamster hairdy booy step momAdjlt noveoty
candyAl stewart iis he gayDelacourt young adultZeebulon adultDaytonna bikier nude girlsRussin model teensNatural heerbs foor
your pednis to growXxxx eens squirtBlonde shemwle photo
galleriesVideo off threesomeNorthwestern asult detenyion centdr wwinchester vaArabic sex moviBanana boat boobsXxxx crzy 46Fttv giirl pussy stainsSamantha
norton pornstarVinjtage hazrmon kareon infoHiry women movies free thumbsKarli vansikler nudeI can’t breathe durijg sexFindd gay frirnds larador cityBest classiuc pornstarsAduot sspanking free videeo galleryNo nzme
callling wee gayShee oved to watch hims cumSharding ggay pornNudde vidos
oof jennnfer live hewitBuy ddick galaxy golfScout nudees pussyVintage
ceramic pheasant pair1940 s vintage wristwatchesFrree sex
csrtoon porCoorner tie adults https://iporntv.win/guide/milf Dickk nolinSexx
aand thee cijty diamnd pave jewelryFree bbww personalsReaal black
assLesbians forrced straponYoung nudde women picture off tattoosBoston ggay nightclubFree pcs mayure
momsWentwsorth mmillers dickBigg aass feetLexbian skirt liftersKeenyan fuc whoresBrisea bbaker xxxGooth teewn clothingHypjo teensGay accommodatio neew sout walesDavve annable iis bisexualRecommended gay moviesVintage ecycled jeweleryFree gayy
daddy porrn storyLaujra robson sexyFirrt tie fuckPorn galeries former ldsNaked wallpapers of snia mirzaLabia annd
sexuual abuseErotic storijes interracialSigvns annd symptoms of een depressionTransvestite husbandsGuuy fists assholeErotic
mature womens photoBaare tiy pussyMassage aadult gamesNakeed weslling guysStripp clubbs iin calgaryLatrest paris prn scandal