C++ Composition with programming example

(Last Updated On: April 4, 2021)

C++ Composition, Overview:

C++ Composition– Even in day to day life the very complex objects and machines are often built from bigger, smaller, and simpler objects. Just look around and you will find hundreds of things built using different objects. Even the cell phone or the computer system you are using is built using different things. The composition is also fundamental to every object-oriented language. It is normal to consider things in terms parts and components. It is hard to break down complex problems into solvable chunks without composition.  For example, a car is built using a metal frame, an engine some tires, a transmission system, a steering wheel, and 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 effortless and straightforward process of building the 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 object of another existing class becomes a member of the new class. This interconnection  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 sub object. When a C++ Composition is destroyed, then all of its sub objects are destroyed as well. Such as when car is destroyed, then its motor, frame, and other part are also destroyed with it. It has do and die relationship.

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Program example: how to use c++ composition

C++ Composition

 



In this program, the 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 value of parameter with the value of ‘d’ and displays 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, show_sum() member function is called by using two dot operators. The value 100 is also passed  as parameter. The member function print_result of object ‘b’ of class Y is also called for execution. In the body of this function, show_sum() function of object ‘a’ of class X is called for execution by passing value 5.

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply