×

C++ Aggregation

C++ Aggregation Definition: In C++, aggregation is a process in which one class (as an entity reference) defines another class. It provides another way to reuse the class. It represents a HAS-A relationship association, or it has class and relationship.

Note: HAS-A relation simply means dynamic or run-time binding.

To qualify as an aggregation, an object must define the following relationships:

  1. The member must be a part of a class.
  • A member can belong to or more classes at a time.
  • The member does not have any existence managed by the object.
  • The member is unknown about the object's existence.
  • The relationship is uni-directional.

Collectively, aggregation is a part-whole relationship, where the parts (member) are contained within the entire uni-directional relationship. However, unlike composition, members can belong to one object at a time. The entire object doesn't need to be responsible for the existence and lifespan of the members. In simple and sober language, aggregation is not responsible for creating or destroying the members or parts.

For instance, consider a relationship between a person and their home address. However, the same address may belong to more than one person at a time in some significant order. Although, it is not managed by the person; the address existed even before the man and will tend to exist even after it. Additionally, a person knows where he/she lives, but the address does not have any information about what person lives. Therefore, it is an example of an aggregate relationship.

Let us dive into the syntax of aggregation.

Syntax:

 Class PartClass
 {
 //instance variables
 //instance methods
 }
 class Whole
 {
 PartClass* partclass;
 } 

In the above syntax, the whole class represents the class that is a container class. The container class is considered as the class for the other part class, which is again contained in the entire class's object. Here, each object of class as the whole can hold the reference pointer of the part class's object.

Implementing Aggregations

In the previous example, we came across how address and person serve the aggregation relation. Similarly, consider the points which help in understanding how aggregations are implemented:

Compositional Nature: Aggregation resembles compositions because they represent the part-whole relationship. They are implemented identically in the same fashion with a slight difference in the semantics.

Member Constraints: In aggregation, we also add parts as member variables. However, these members are references or pointers that point to the object created outside the scope of the class. Thus, aggregation takes objects which point as constructor parameters. The objects and sub-objects are later added by functions and operations.

Existence: Since the parts may exist outside the scope, class destruction may lead to member variable destruction (but not deletion). The parts will exist consequently.

Let us now look at a programming example to understand aggregation better.

 #include<bits/stdc++.h>
 using namespace std;
 class Address
 {
     public:
         int houseNo;
         string colony,city, state;
         Address(int hno, string colony, string city, string state)
 {
         this->houseNo = hno;
         this->colony=colony;
         this->city = city;
         this->state = state;
   }
 };
 class Person
 {
     private:
         Address* address;
     public:
         string name;
         Person(string name, Address* address)
 {
         this->name = name;
         this->address = address;
     }
 void display()
 {
     cout<< name<< " "<< " "<< address->houseNo<<" "<<address-> colony<<" " <<address->city<< " "<<address->state<<endl;
 }
 };
 int main(void)
 {
 Address add1= Address(007 ,"Ampitheatre Park","China Gate","San        Fransisco, CA");
 Person p1 = Person("Andrew's Address:->",&add1);
 Person p2 = Person("Stacy's Address: ->",&add1);
 p1.display();
 p2.display();
 }
 return 0; 

Output:

C++ Aggregation

Explanation:

In the above code, Here Person has an instance variable name that tells the name of the person and a pointer variable to address the class object. Address class object has variables such as House, street, city, and state. Here we have two persons, Raj and Seema, living at the same address, thus share the same address object add1.

The class Person is defined with an instance variable name which tells the name of the person, and the pointer associated with it tells the address of the class object. We have initially assigned two persons, namely Andrew and Stacy, who share the same address as defined in the class with the address as a1. The output address for both Andrew and Stacy will thus be the same.

Advantages of C++ Aggregates:

  1. The aggregation process shows the bi-directional relationship between objects of different classes.
  • The HAS-A relationship provides dependencies that are easy to understand and co-relate.
  • Aggregation provides code reusability and readability. Once the whole class is created, the reference provides the proper access to part classes.
  • It defines a unidirectional relationship that to correctly associates the whole class to the part class.

Related Topics

Memset in C++

Memset () is a function in the C++ programming language that fills memory blocks. The value of "ch" is first converted to an unsigned character. In this case, "ch" denotes the...

3 minutes read.

Returning Multiple Values from a Function using Tuple and Pair in C++

We may come across many situations where after the driver code's execution is performed in a code block, the return should be either multiple values or a single value possibly...

4 minutes read.

C++ Nested if

C++'s nested if statements enable more complex decision-making when a section of code needs to execute only after a set of conditions is met. The nested if control statement refers...

4 minutes read.

Diamond Pattern Using Do-While loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

5 minutes read.

Floating Point Operations and Associativity in C, C++ and Java

In this tutorial, we are going to compare Floating-point operations and the concept of associativity. Before we apply the concept of associativity in the floating-point operations in all three programming...

3 minutes read.

Boost split in C++ library

Boost::split in C++ library Boost offers strong tools for adding mature, well-tested libraries to the C++ standard library. The boost: split function, which is a component of the Boost string algorithm...

2 minutes read.

Multiple Inheritance

C++ Multiple Inheritance Multiple inheritance is such an inheritance in which a derived class inherits properties of more than one base class.     C++ Multiple Inheritance Example In this example, two base classes Square and...

2 minutes read.

Pure Virtual Function in C++ With Example Program

What is a Virtual Function? A virtual function is created inside a class with the keyword virtual. A virtual function does not have any value to be returned. Once a virtual...

3 minutes read.

C++ Object Class

C++ Object Class Overview: C++ is a high-level programming language and an object-oriented programming language. An object-oriented language always has some properties of classes and objects. In this article, we...

4 minutes read.

For Loop Examples in C++

For Loop A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently. The syntax of for loop In C++, a...

6 minutes read.

Parameterize Constructor

C++ Parameterized Constructor A constructor having parameters is known as parameterize constructor. Parameterize constructor is used to assign different values. Syntax: className(data-type argument){   // Constructor definition   }   className(data-type argument, data-type argument){   // Constructor definition   } A parameterized constructor can be passed values to constructor function in two ways: 1)...

1 minute read.

C++ Bidirectional Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

3 minutes read.

10 Best C and C++ Books for Beginners & Advanced Programmers

If you want to become a skilled software developer, you should never stop learning, whether you're a working professional or a student. Why, therefore, only C or C++? The fundamental...

6 minutes read.

External merge sort in C++

External merge sort in C++ External sorting is a concept for a group of sorting algorithms capable of handling large data volumes. External sorting is needed if the information getting sorted...

9 minutes read.

Factorial of a Number in C++ using while Loop

What is a factorial? The factorial of a number is the product of all the positive numbers less than or equal to n, indicated by n! According to the standard for an...

6 minutes read.

Palindrome using Do-while loop in C++

What is Palindrome? A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++...

5 minutes read.

Difference between Exit and Return

Define Exit() At the point when a client needs to leave a program from this capability is utilized. A void return type capability calls all capabilities enrolled at the exit and ends...

3 minutes read.

Armstrong Number Program in C++

Let's first define Armstrong number before writing the C++ program to check whether the number is Armstrong or not. The sum of the cubes of its digits is equal to the...

2 minutes read.

Lambda Expression in C++

The lambda expression was introduced in C++ 11. It is used to write the inline function in C++. The code written in lambda expression cannot be reused further. The syntax...

3 minutes read.

C++ String Concatenation

C++ String Concatenation In this section, we will learn about C ++ String Concatenation, what it does, how it works, and will also see its programs. What is the String Concatenation? The + operator...

3 minutes read.