Introduction to Complex Objects and Composition

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 behaviour.
  • 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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement a
// composition
  
// A point class
class Point {
private:
    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 fuctionality
// using the points is implemented
// here
class Location {
    Private : Point Source;
    Point Destination
};

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
private:
    int num;
  
    // Public setter and getter
public:
    void set(int i)
    {
        num = i;
    }
    int get()
    {
        return num;
    }
};
  
// Another class
class Two {
  
    // Public method and the object
    // of the previous class
public:
    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
    T.O.set(100);
    T.show();
}

chevron_right


Output:

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.