What is Composition in C++?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Composition is referred to building a complex thing with the use of smaller and simple parts.

Composition is one of the fundamental approaches or concepts used in object-oriented programming.

Composition in C++ is defined as implementing complex objects using simpler or smaller ones. Looking around at our surroundings, we find different things built using several small components. For example, a laptop is constructed using main memory (RAM), secondary memory (Hard Drive), processor, etc. A building is created using smaller objects like bricks, sand, cement, etc. It is often good to consider complex things in terms of smaller parts and components.

Composition in C++ is achieved by using objects and classes; therefore, it is referred to as object composition.

The object composition concept work on the model of has-a relationship among two different objects. For example, A PC has a Core named CPU. Complex objects are often referred to as parent components or whole components, while simpler or smaller objects are often referred to as child components or part components.

Object composition is an important concept in terms of C++ language as it gives us the freedom to design complex classes by using simpler and smaller manageable parts. Object composition allows us to reduce the overall complexity of the program and also allows us to write code error-free and with more speed. It also allows us to reuse the program as often as we want. To facilitate the designing of complex classes from simpler and smaller parts. C++ allows us to carry out the process of object composition in an uncomplicated way by accessing the classes as a member function in other classes.

In object composition, the object created is a-part of another object which is referred to as a sub-object. A Sub-object is destroyed when an object composition is destroyed, known as a do and die relationship.

Now, after discussing the composition in C++, let's learn about the types of object composition in C++.

Types of Object Composition in C++

Object composition is basically of the following subtypes:

Composition:

Composition relationship is also called a part-whole relationship in which the part component can only be a part of a single object simultaneously. In composition relationships, the part component will be created when the object is created, and the part will be destroyed when the object is destroyed. A person's body and heart is a good example of a part-whole relationship where if a heart is part of a person's body, then it cannot be a part of someone else's body at one time.

To be qualified as a composition, the part and object must follow the relationships described below:

  1. The part or child component (referred to as a member) belongs to a single object (also called class).
  2. The part component can show its presence with the help of an object.
  3. The part(member) component is the element of the object.
  4. The part component needs to learn about the object's presence.

There are certain sets of rules for creating and destroying parts of the object:

  • There is no need to create a part (member) of the objects until it is not required.
  • The composition should use the part given to it as an input rather than completely creating a part itself.
  • The composition can assign the part's destruction to any other object.

Aggregation :

Unlike composition, in the aggregation process, the part component can simultaneously belong to more than one object. It is also a part-whole relationship. The object(class) will not be responsible for the existence or presence of the parts. To be qualified as aggregation, the part and object must follow the relationship described below:

  1. The part component or child component (also referred to as a member) simultaneously belongs to more than one object (also referred to as a class).
  2. The part component does not show its presence with the help of an object.
  3. The part(member) component is the element of the object.
  4. The part component does not know about the object's presence.

Object Delegation :

Object delegation is a process in which we use the objects of a class as a member of another class. Object delegation is the passing of work from one object to another. It is an alternative to the process of inheritance. But when the concept of inheritance is used in the program, it shows an is-a relationship between two different classes. On the contrary, in object delegation, there is no relationship between different classes.

How to Use Composition in C++?

As we have already discussed earlier in the article, composition in C++ is a way to construct a complex object with the help of a simpler one. Now, we will understand the practical usage of composition in C++ through syntax and an example:

Syntax :

Here, in the above syntax, it is shown how we can implement composition in C++ where we have used two classes, class A and class B. class B will use the objects of class A as its member variables. Therefore, class A is the simpler class accessed by the complex class class B.

Now, Let's look at an example to understand the concepts more clearly:

Code :

Output :

Explanation :

In the above example, we have two classes, class A and class B. Inside class A, we have one private data member or member variable a, which is of integer type, and two member functions of the class setValue() and getSum(). Now, in class B, we have an object of class A that is x and a member function showResult().

The setValue() method assigns the value passed in the function to the member variable a. The next member function getSum() adds the value of the parameter passed to it with the member variable a, and then the function prints the value after addition.

In class B, we have only one member function showResult() which uses the object of class A that is x which shows the composition relationship between class A and class Band invokes the member function inside the class A with a different value that is 10. Then the getSum() prints the value passed as an argument and the member variable a after addition.

Inside the main() method, we have created an object of class B that is obj1. Now, the member function setValue()and getSum() of class A having object a which is also a sub-object of object obj1 is invoked using the two dot operators with 10 and 5 as the parameter respectively. In the same way, one dot operator is used to call the member function showResult() of the same class that is class B.

This program shows how we can practically use composition in C++.

Let's take a look at some more examples :

Example 1 :

This example shows the composition relationship between classes class A and class B.

Code :

Output :

In the above program, we have first declared a class A, and in that class, we have declared two constructors; one non-parameterized and one parameterized. Both of these constructors are used to initialize the data variable of class A.

After that, we declared another class named B in which we declared the object of the class A called obj2. Apart from that, we have also declared a constructor that will use the object of class A to initialize the variables of class B. We have also declared a method named printData() to print the class variables.

Finally, in the main function, we have created an object of the B class called obj1 and passed a value to initialize the data variable of the class using the constructor (that we have discussed above). We are using the obj1 object to invoke the printData() method of class B.

Example 2 :

In this example, we have used the concept of object delegation, which is an alternative to the process of inheritance.

Code :

Output :

In the above program, we have first declared a class A, and in that class, we have declared a method called show() that will be invoked further in the program.

After that, we declared another class named B in which we declared the object of the class A called obj2. Apart from that, we have also declared a method show() that will call the show() method of the A class using the object of class A (obj2).

Finally, in the main function, we have created an object of the B class called obj1. We are using the obj1 object to invoke the show() method of class B that will eventually call the show() method of class A.

Conclusion

  • The Composition in C++ is defined as implementing complex objects using the simpler or smaller ones.
  • In real life, when we look around at our surroundings, we will find different things built using several small components. For example, a laptop is constructed using main memory (RAM), secondary memory (Hard Drive), processor, etc.
  • Composition in C++ is also referred to as object composition.
  • The object composition concept work on the model of has-a relationship among two different objects.
  • Complex objects are often referred to as parent components, while simpler or smaller objects are often referred to as child components.
  • Object composition allows us to reduce the overall complexity of the program and also allows us to write code error-free and with more speed.
  • Object composition is basically of the following subtypes:
    • Composition
    • Association
    • Delegation
  • Composition relationship is also called a part-whole relationship in which the part component can only be a part of a single object simultaneously.
  • Association is also a part-whole relationship; its part can simultaneously belong to more than one object.
  • Delegation is a process in which we use the objects of a class as a member of another class.