C++ Composition- In real-life complex objects are often built from smaller and simpler objects. For example, a car is built using a metal frame, an engine some tires, a transmission system, a steering wheel, and a large number of other parts. A personal computer is built from a CPU, a motherboard, memory unit, input and output units, etc. even human beings are building from smaller parts such as a head, a body, legs, arms, and so on. This process of building complex objects from simpler ones is called c++ composition. It is also known as object composition.
So far, all of the classes that we have used had member variables that are built-in data type (e.g. int, float, double, char). While this is generally sufficient for designing and implementing small, simple classes, but it gradually becomes difficult to carry out from more complex classes, especially for those built from many sub-parts. In order to facilitate the building of complex classes from simpler ones, C++ allows us to do object C++ Composition in a very simple way by using classes as member variables in other classes.
A class can have one or more objects of other classes as members. A class is written in such a way that the object of another existing class becomes a member of the new class. this relationship between classes is known as C++ Composition. It is also known as containment, part-whole, or has-a relationship. A common form of software reusability is C++ Composition.
In C++ Composition, an object is a part of another object. The object that is a part of another object is known as a sub-object. When a C++ Composition is destroyed, then all of its subobjects are destroyed as well. Such as when a car is destroyed, then its motor, frame, and other parts are also destroyed with it. It has a do and die relationship.
Program example: how to use C++ Composition in programming:
using namespace std;
void set_value(int k)
void show_sum(int n)
cout<<"sum of "<<d<<" and "<<n<<" = "<<d+n<<endl;
In this program, class X has one data member ‘d’ and two member functions ‘set_value()’ and ‘show_sum()’. The set_value() function is used to assign value to ‘d’. the show_sum() function uses an integer type parameter. It adds the value of parameter with the value of ‘d’ and displays the result o the screen.
Another class Y is defined after the class x. the class Y has an object of class x that is the C++ Composition relationship between classes x and y. this class has its own member function print_result().
In the main() function, an object ‘b’ of class y is created. The member function set_value() of object ‘a’ that is the sub-object of object ‘b’ is called by using tw dot operators. One dot operator is used to access the member of the object ‘b’ that is object ‘a’, and second is used to access the member function set_value() of sub-object ‘a’ and ‘d’ is assigned a value 20.
In the same way, the show_sum() member function is called by using two dot operators. The value 100 is also passed as a parameter. The member function print_result of object ‘b’ of class Y is also called for execution. In the body of this function, the show_sum() function of object ‘a’ of class X is called for execution by passing value 5.
If you enjoyed this article, be sure to check out my other pieces for more great content!