{"id":84417,"date":"2021-09-15T09:00:07","date_gmt":"2021-09-15T03:30:07","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=84417"},"modified":"2021-09-15T09:00:07","modified_gmt":"2021-09-15T03:30:07","slug":"cpp-aggregation","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/","title":{"rendered":"Aggregation in C++"},"content":{"rendered":"<p>The C++ programming language offers various exciting and useful features to programmers. A programmer can get great benefits while using the C++ language. Aggregation is one of the exciting features of C++. Let us learn more about it.<\/p>\n<h3>What is Aggregation in C++?<\/h3>\n<p>In C++, Aggregation is used to represent the \u2018HAS-A\u2019 relationship between two objects. It is a type of association. If in a process, one class defines another class as any entity reference then it is known as Aggregation. With the help of aggregation, you can also reuse the class.<\/p>\n<p>An object should define the following relationships to qualify as an aggregation:-<\/p>\n<ul>\n<li>Must be a part of the class.<\/li>\n<li>The member can belong to one or more classes at a time.<\/li>\n<li>Member is unknown about the existence of the object.<\/li>\n<li>The relationship is unidirectional.<\/li>\n<\/ul>\n<p><strong>NOTE:-<\/strong> For creating or destroying the members or parts, Aggregation is not responsible.<\/p>\n<p>Let\u2019s understand through an example between a person and the home address. This address may belong to other persons as well. The address existed before the man and will exist for forever. The person knows where he lives but the address does not contain any information about what person lives. It is an example of an aggregate relationship.<\/p>\n<p><strong>Syntax:-<\/strong><br \/>\nTo put it short, aggregation is a \u2018HAS-A\u2019 relationship between the objects of 2 individual classes. Aggregation restricts some situations of associations.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Class ClassPart\n{\n\/\/instance variables\n\/\/instance methods\n}\nclass Tech\n{\nClassPart* partclass;\n}\n<\/pre>\n<p>In the above, the Tech class represents the class that is a container class for other ClassPart. And it is included in the object of the Tech class. The Tech class\u2019s each object holds a reference pointer to the object of the ClassPart.<\/p>\n<h3>Working of Aggregation in C++<\/h3>\n<p>In the program, Aggregation helps to represent HAS-A relation between the objects of 2 individual classes. Aggregation is more restrictive compared to the association. Aggregation helps in making your program code more readable and understandable to represent the relation. Using a pointer variable, you can refer to the object of one class in the container class object.<\/p>\n<h3>Implementing Aggregations in C++<\/h3>\n<p>Below are the points which will help you to understand the implementation of aggregations:-<\/p>\n<ul>\n<li><strong>Compositional Nature:<\/strong>&#8211; Aggregation resembles composition. With a small difference in semantics, both are implemented identically.<\/li>\n<li><strong>Member Constraints:<\/strong>&#8211; You can also add parts as member variables in aggregation. It also takes constructor parameters.<\/li>\n<li><strong>Existence:<\/strong>&#8211; The parts will exist consequently. Class destruction can lead you to member variable destruction.<\/li>\n<\/ul>\n<p><strong>Example:- C++ Aggregation<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\n#include&lt;string.h&gt;\nusing namespace std;\nclass Tech{\npublic:\nint house;\nstring city, state;\nTech(int house_no, string city, string state)\n{ this-&gt;house = house_no;\nthis-&gt;city = city;\nthis-&gt;state = state;\n}\n};\nclass Person\n{\nprivate:\nTech* address;\npublic:\nstring name;\nPerson(string name, Tech* address)\n{\nthis-&gt;name = name;\nthis-&gt;address = address;\n}\nvoid display()\n{\ncout&lt;&lt; name&lt;&lt; \" \"&lt;&lt; \" \"&lt;&lt; address-&gt;house&lt;&lt;\" \"&lt;&lt;address-&gt;city&lt;&lt; \" \"&lt;&lt;address-&gt;state&lt;&lt;endl;\n}\n};\nint main(void) {\nTech add1= Tech(112 ,\"Bandra\",\"Mumbai\");\nTech add2 = Tech(222, \"Jahanpanah\",\"New Delhi\");\nPerson p1 = Person(\"Raj\",&amp;add1);\nPerson p2 = Person(\"John\",&amp;add2);\ncout &lt;&lt; \"Name of the Person\" &lt;&lt; \" and \" &lt;&lt; \"Address\"&lt;&lt; endl&lt;&lt;endl;\np1.display();\np2.display();\nreturn 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">\n<p>Name of the Person and Address<\/p>\n<p>Raj 112 Bandra Mumbai<br \/>\nJohn 222 Jahanpanah New Delhi<\/p>\n<\/div>\n<p>In the above example, the Tech class object contains variables such as House, City, State. We have 2 people Raj and John. They are living in different addresses. The Person class has an instance variable that tells the name of the person. And it also has a pointer variable to the address class object.<\/p>\n<h3>Advantages of Aggregations in C++<\/h3>\n<ul>\n<li>Aggregation represents unidirectional relation between the objects of 2 classes. It is a one-direction relation.<\/li>\n<li>Aggregation also helps in improving readability and reusability of the program code.<\/li>\n<li>It helps in initiating the relation between objects of 2 classes where one class is the Whole class and other is the part of the class. It is useful in representing the HAS-A relation.<\/li>\n<li>Aggregation helps in making your program code more readable and understandable to represent the relation.<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>In this tutorial, we discussed aggregation in C++, its working, implementation and advantages. Hope you liked it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C++ programming language offers various exciting and useful features to programmers. A programmer can get great benefits while using the C++ language. Aggregation is one of the exciting features of C++. Let us&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":84642,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3405],"tags":[4224,4225,4226],"class_list":["post-84417","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-advantages-of-aggregations-in-c","tag-aggregation-in-c","tag-working-of-aggregation-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Aggregation in C++ - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn what is the aggregation in C++, its working, implementation and advantages with examples for better understanding.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Aggregation in C++ - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn what is the aggregation in C++, its working, implementation and advantages with examples for better understanding.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-15T03:30:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/C-Aggregation.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Aggregation in C++ - TechVidvan","description":"Learn what is the aggregation in C++, its working, implementation and advantages with examples for better understanding.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/","og_locale":"en_US","og_type":"article","og_title":"Aggregation in C++ - TechVidvan","og_description":"Learn what is the aggregation in C++, its working, implementation and advantages with examples for better understanding.","og_url":"https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-09-15T03:30:07+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/C-Aggregation.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Aggregation in C++","datePublished":"2021-09-15T03:30:07+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/"},"wordCount":579,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/C-Aggregation.jpg","keywords":["Advantages of Aggregations in C++","Aggregation in C++","Working of Aggregation in C++"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/","url":"https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/","name":"Aggregation in C++ - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/C-Aggregation.jpg","datePublished":"2021-09-15T03:30:07+00:00","description":"Learn what is the aggregation in C++, its working, implementation and advantages with examples for better understanding.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/C-Aggregation.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/C-Aggregation.jpg","width":1200,"height":628,"caption":"C++ Aggregation"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-aggregation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Aggregation in C++"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/84417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=84417"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/84417\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/84642"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=84417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=84417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=84417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}