C++ Composition with programming example

C++ Composition:

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:

C++ Composition

Programming Explanation:

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!

Related Article:

Searching in C++: Sequential Searching, Binary Searching

ESP32 Tutorial: Getting Started with ESP32 Arduino Serial Monitor

Interface MPU6050 Accelerometer & Gyroscope Sensor with Esp32

ESP32 Tutorial: Infrared Obstacle Avoidance Sensor with ESP32

Arduino Bluetooth controlling application development using Android Studio

Related Articles

One Comment

  1. This blog is an incredible resource for anyone looking to expand their knowledge on a variety of topics. The author’s writing is both informative and engaging, and their ability to present complex ideas in an accessible way is truly impressive. I appreciate the level of research that goes into each post, as well as the author’s commitment to providing balanced and unbiased perspectives. What I love most about this blog is its ability to make even the most complex subjects approachable and interesting. Whether you’re a casual reader or a serious scholar, there’s something here for everyone.

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Back to top button