Open In App

Object Composition-Delegation in C++ with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

Object Compositions

An object is a basic unit of Object-Oriented Programming and represents real-life entities. Complex objects are 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. This process of building complex objects from simpler ones is called object composition.
In object-oriented programming languages, object composition is used for objects that have a “has-a” relationship with each other. Therefore, the complex object is called the whole or a parent object whereas a simpler object is often referred to as a child object.

Syntax:

class A
{
    // body of a class
};

class B
{
    A objA;
    public:
   B(arg-list) : objA(arg-list1);
};

In the classes given above, B uses objects of class A as its data members. Hence, B is a complex class that uses a simple class A. Let’s have a look at the program that makes use of the composition.

Below is the implementation of the composite class:

C++




#include <iostream>
using namespace std;
  
// Simple class
class A {
public:
    int x;
  
    // COnstructor initializing
    // the data members
    A() { x = 0; }
  
    A(int a)
    {
        cout << "Constructor A(int a) is invoked" << endl;
        x = a;
    }
};
  
// Complex class
class B {
    int data;
    A objA;
  
public:
    // COnstructor initializing the
    // data members
    B(int a)
        : objA(a)
    {
        data = a;
    }
  
    // Function to print values
    // of data members in class
    // A and B
    void display()
    {
        cout << "Data in object of class B = " << data
             << endl;
        cout << "Data in member object of "
             << "class A in class B = " << objA.x;
    }
};
  
// Driver code
int main()
{
    // Creating object of class B
    B objb(25);
  
    // Invoking display function
    objb.display();
    return 0;
}


Output:

Constructor A(int a) is invoked
Data in object of class B = 25
Data in member object of class A in class B = 25

Types of Object Compositions:

There are two basic subtypes of object composition:

1. Composition: The composition relationships are part-whole relationships where a part can only be a part of one object at a time. This means that the part is created when the object is created and destroyed when the object is destroyed. To qualify as a composition, the object and a part must have the following relationship-

  1. The part (member) is part of the object (class).
  2. The part (member) can only belong to one object (class).
  3. The part (member) has its existence managed by the object (class).
  4. The part (member) does not know about the existence of the object (class).

There are some variations on the rule of creating and destroying parts: 

  1. A composition may avoid creating some parts until they are needed.
  2. A composition may opt to use a part that has been given to it as input rather than creates the part itself.
  3. A composition may delegate the destruction of its parts to some other object.

2. Aggregation: The aggregation is also a part-whole relationship but here in aggregation, the parts can belong to more than one object at a time, and the whole object is not responsible for the existence of the parts. To qualify as aggregation, a whole object and its part must have the following relationships:

  1. The part (member) is part of the object (class).
  2. The part (member) can belong to more than one object (class) at a time.
  3. The part (member) does not have its existence managed by the object (class).
  4. The part (member) does not know about the existence of the object (class).

Benefits of Object Composition:

Using object composition can provide the following benefits:

  1. Reuse existing codes: Object composition allows to reuse of the existing code without a need to model an is-a relationship as usually done in inheritance.
  2. Clean design APIs: Using object composition can help to design clean and composed APIs. This is because when the class is composed it is easier to decide if the referenced class will become part of the API or will be hidden.
  3. Change in implementation of the class used in the composition without requiring external clients: Composition also allows to make code easier to change and adapt if necessary. The internal classes can be changed without any side effects and changes can be handled internally.

Object Delegation

Object Delegation means using the object of another class as a class member of another class. It is known as object delegation. Delegation can be an alternative to inheritance, but in an inheritance, there is an i-s a relationship, but in the delegation, there is no inheritance relationship between the classes.

C++




// C++ program to illustrate the
// Object Delegation
#include <iostream>
using namespace std;
class First {
public:
    void print() { cout << "The Delegate"; }
};
class Second {
    // Creating instance of the class
    First ob;
  
public:
    void print() { ob.print(); }
};
  
// Driver Code
int main()
{
    Second ob1;
    ob1.print();
    return 0;
}


Output:

The Delegate

Benefits of object delegation:

  1. The reuse class can be changed without changing the reusing class.
  2. Multiple reusing classes can share reused class variables simultaneously.
  3. Variables and methods of reusing and the reused classes can be on separate computers.
  4. It works in single inheritance languages.

Object composition vs Object Delegation

S No. Object Composition Object Delegation
1 It is about relationships between objects. It is about passing work from one object to another.
2 In composition,  the methods of the inner object may be used only privately and not re-exposed. Delegation involves re-exporting methods.
3 The composition has some implications for object lifecycle, the parent object owns the child and the child doesn’t have much reason to exist on its own. The delegation does not have this implication.


Last Updated : 21 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads