{"id":20669,"date":"2025-02-26T09:05:00","date_gmt":"2025-02-26T04:05:00","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=20669"},"modified":"2025-11-30T20:00:48","modified_gmt":"2025-11-30T15:00:48","slug":"associations-aggregation-composition-in-oop-c","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/02\/26\/associations-aggregation-composition-in-oop-c\/","title":{"rendered":"Associations, Aggregation &#038; Composition in OOP (C++)"},"content":{"rendered":"\n<p>Object-Oriented Programming is not only about creating classes and objects, it is also about <strong>how objects interact<\/strong> 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: <strong>Association, Aggregation, and Composition<\/strong>.<\/p>\n\n\n\n<p>Before we begin, remember the fundamental difference:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"is-a\"  \u2192 Inheritance  \n\"has-a\" \u2192 Class relationships (Association, Aggregation, Composition)\n<\/code><\/pre>\n\n\n\n<p>This tutorial explains \u201chas-a\u201d relationships clearly using real-world analogies and simple C++ programs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Association<\/strong><\/h2>\n\n\n\n<p>Association represents a <strong>general relationship<\/strong> between two independent objects.<br>One object uses or interacts with another, but neither owns the other.<br>They can exist without one another.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Definition<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Association is a loose relationship between two independent classes where one object uses or interacts with another object.\n<\/code><\/pre>\n\n\n\n<p>For example, a&nbsp;<strong>Driver drives a Car<\/strong>.<br>The driver does not own the car; the interaction exists only while driving.<br>Both can exist independently.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Complete C++ Example \u2014 Association<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;string&gt;\nusing namespace std;\n\nclass car{\n    private:\n    string name;\n    \n    public:\n    car(string c):name(c){}\n    \n    string getname(){\n        return name;\n    }\n};\n\nclass driver{\n    private:\n    string na;\n    \n    public:\n    driver(string d): na(d){}\n    \n    void drive(car C){\n        cout&lt;&lt;na&lt;&lt;\" is driving \"&lt;&lt;C.getname();\n    }\n    \n};\n\n\nint main() {\ncar c(\"Toyoto\");\ndriver d(\"Rehan\");\n\nd.drive(c);\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Explanation<\/strong><\/h3>\n\n\n\n<p>The driver and car exist independently.<br>The driver only <em>uses<\/em> the car through a function.<br>No object owns the other.<br>This is <strong>Association<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Aggregation<\/strong><\/h2>\n\n\n\n<p>Aggregation is a <strong>whole\u2013part<\/strong> relationship, but with <em>weak ownership<\/em>.<br>The container holds objects, but <strong>does not own their lifetime<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Definition<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Aggregation is a whole\u2013part relationship where the container holds objects that can exist independently outside the container.\n<\/code><\/pre>\n\n\n\n<p>For example,&nbsp; a&nbsp;<strong>Classroom has Students<\/strong>, but the students can exist without the classroom.<br>If the classroom object is deleted, students still exist.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Complete C++ Example \u2014 Aggregation<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;string&gt;\nusing namespace std;\n#include &lt;vector&gt;\n\nclass student{\n    private:\n    string name;\n    \n    public:\n    student(string n):name(n){}\n    \n    string getname(){\n        return name;\n    }\n};\n\nclass room{\n    public:\n    vector&lt;student*&gt; students;\n    \n    void add(student *s){\n        students.push_back(s);\n    }\n    \n    void show(){\n        for(auto s : students){\n            cout&lt;&lt;\"students are: \"&lt;&lt;s-&gt;getname();\n        }\n    }\n};\n\n\nint main() {\nstudent s(\"ABC\");\nroom r;\nr.add(&amp;s);\nr.show();\n\n\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Explanation<\/strong><\/h3>\n\n\n\n<p>Students are created <strong>outside<\/strong> the classroom.<br>The classroom only stores pointers to them.<br>If the classroom is destroyed, students remain alive.<br>This represents <strong>Aggregation<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Composition<\/strong><\/h2>\n\n\n\n<p>Composition is the <strong>strongest form<\/strong> of object relationship.<br>The container object <strong>owns<\/strong> the inner object and controls its lifetime.<br>If the outer object is destroyed, the part also gets destroyed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Definition<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Composition is a strong whole\u2013part relationship where the inner object\u2019s lifetime is dependent on the container object.\n<\/code><\/pre>\n\n\n\n<p>For example, a&nbsp;<strong>House has Rooms<\/strong>.<br>If the house is destroyed, the rooms cannot exist alone.<br>This is strict ownership.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Complete C++ Example \u2014 Composition<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;string&gt;\nusing namespace std;\n\nclass engine{\n    public:\n    string name;\n    \n    engine(){\n        cout&lt;&lt;\"enginer starts\"&lt;&lt;endl;\n    }  \n};\n\nclass car{\n    public:\n    engine E;\n    \n    car(){\n        cout&lt;&lt;\"Car starts\";\n    }\n};\n\n\nint main() {\n    car c;\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Explanation<\/strong><\/h3>\n\n\n\n<p>The <strong>engine<\/strong> object is created inside <strong>car<\/strong>.<br>When car is destroyed, engine is also destroyed.<br>The part <strong>cannot exist independently<\/strong>.<br>This is <strong>Composition<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Comparison Table<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>Association<\/th><th>Aggregation<\/th><th>Composition<\/th><\/tr><\/thead><tbody><tr><td>Relationship<\/td><td>Interaction only<\/td><td>Whole\u2013Part (weak)<\/td><td>Whole\u2013Part (strong)<\/td><\/tr><tr><td>Ownership<\/td><td>No ownership<\/td><td>Partial ownership<\/td><td>Full ownership<\/td><\/tr><tr><td>Lifetime dependency<\/td><td>Independent<\/td><td>Independent<\/td><td>Dependent<\/td><\/tr><tr><td>UML Symbol<\/td><td>Simple line<\/td><td>Hollow diamond<\/td><td>Filled diamond<\/td><\/tr><tr><td>Example<\/td><td>Driver \u2192 Car<\/td><td>Classroom \u2192 Students<\/td><td>Human \u2192 Heart<\/td><\/tr><tr><td>Code Meaning<\/td><td>Temporary usage<\/td><td>Stores external objects<\/td><td>Owns and creates internal objects<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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: This tutorial explains \u201chas-a\u201d relationships clearly using real-world analogies and simple C++ programs. Association Association represents a general relationship between two&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/02\/26\/associations-aggregation-composition-in-oop-c\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":20671,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[637],"tags":[563,734,704,636,603],"class_list":["post-20669","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oop-with-c","tag-aggregation","tag-association","tag-c","tag-composition","tag-oop"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/11\/OOP.jpg?fit=1920%2C1080&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-5nn","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/20669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/comments?post=20669"}],"version-history":[{"count":2,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/20669\/revisions"}],"predecessor-version":[{"id":20678,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/20669\/revisions\/20678"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/20671"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=20669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=20669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=20669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}