Skip to content
Related Articles

Related Articles

Improve Article

Introduction to Complex Objects and Composition

  • Last Updated : 29 Jul, 2021

An object is a basic unit of Object-Oriented Programming and represents the real-life entities. Complex objects are the objects that are built from smaller or a collection of objects. For example, a mobile phone is made up of various objects like a camera, battery, screen, sensors, etc. In this article, we will understand the use and implementation of a complex object. 
In object-oriented programming languages, object composition is used for objects that have a “has-a” relationship with each other. For example, a mobile has-a battery, has-a sensor, has-a screen, etc. Therefore, the complex object is called the whole or a parent object whereas a simpler object is often referred to as a child object. In this case, all the objects or components are the child objects which together make up the complex object(mobile). 

Classes that have data members of built-in type work really well for simple classes. However, in real-world programming, the product or software comprises of many different smaller objects and classes. In the above example, the mobile phone consisted of various different objects that on a whole made up a complete mobile phone. Since each of those objects performs a different task, they all are maintained in different classes. Therefore, this concept of complex objects is being used in most of the real-world scenarios. The advantages of using this concept are: 

  • Each individual class can be simple and straightforward.
  • One class can focus on performing one specific task and obtain one behavior.
  • The class is easier to write, debug, understand, and usable by other programmers.
  • While simpler classes can perform all the operations, the complex class can be designed to coordinate the data flow between the simpler classes.
  • It lowers the overall complexity of the complex object because the main, task of the complex object would then be to delegate tasks to the sub-objects, who already know how to do them.

The most important advantage is if any changes have to be made in a child class, only the child class can be changed rather than changing the entire parent class.

  • For example, if we want to change the battery class in the mobile object, with the help of composition, the changes are only made in the battery class and the entire mobile object works fine with the updated changes.

Scope of Use: Although there are no well-defined rules to state when a programmer must use the composition, as a rule of thumb, each class should be built to accomplish a single task. The task should be to either perform some part of manipulation or be responsible for coordinating other classes but cannot perform both tasks. This immensely increases the maintenance of the code and future updations because, when we wish to update a feature or an object, only the class pertaining to that specific functionality needs to be updated. And also, it’s very easy to keep track of the errors in the program. For example:


// C++ program to implement a
// composition
// A point class
class Point {
    int x;
    int y;
// Every location has a point.
// Every point has two coordinates.
// The above class's functionality
// is only to store the coordinates
// in two variables. Any functionality
// using the points is implemented
// here
class Location {
    Private : Point Source;
    Point Destination

In the classes given here, Location uses objects of class Point as its data members. Hence, Location is a complex class which uses a simple class Point. Let us have a look at the program that makes use of the composition.

Below is the implementation of a composite class:


// C++ program to implement a
// composite class
using namespace std;
#include <iostream>
// Class with a private parameter
// and the getters and setters
class One {
    // Private parameter
    int num;
    // Public setter and getter
    void set(int i)
        num = i;
    int get()
        return num;
// Another class
class Two {
    // Public method and the object
    // of the previous class
    One O;
    void show()
        cout << "\n Number = "
             << O.get();
// Driver code
int main()
    // Creating the object of
    // class Two
    Two T;
    // Perform some operation using
    // the object of One in this class
Number = 100


Explanation: Note that in the program, class Two has an object of class One. To access a member of One, we must use the object of Two as in T.O.set(100). Moreover, since num is a private member of One, we must use a public function to access its value.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :