Skip to content
Related Articles

Related Articles

Improve Article

Object Delegation in C++

  • Difficulty Level : Expert
  • Last Updated : 18 Aug, 2021
Geek Week

Introduction:

  • Every programming language that is based on an object-oriented concept tries to connect everything to the real world.
  • Similarly, C++ languages use classes, Inheritance, Polymorphism to connect the concept with the real-world concept.
  • In this article, the topic of discussion will be what object delegation is in C++ and the use of object delegation in C++.

Object Delegation in C++: Object Delegation means using the object of another class as a class member of another class. It is known as object delegation. Below are some properties of the 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.
  • The Delegation allows us to use the properties of the particular class that is required in the class.
  • Delegation can be viewed as a relationship between objects where one object forwards a certain method calls to another object, called its delegate.
  • The primary advantage of delegation is run-time flexibility – the delegate can easily be changed during run-time.
  • But unlike inheritance, delegation is not directly supported by most popular object-oriented languages, and it doesn’t facilitate dynamic polymorphism.

Below is the C++ program illustrating Object Delegation:

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

 

When to use what?

Here are some examples of when inheritance or delegation are being used:

  • Assume class is called B and the derived/delegated to class is called A.
  • If users want to express a relationship (is-a), then use inheritance.
  • Users want to be able to pass the class to an existing API expecting A’s, then use inheritance.
  • Users want to enhance A, but A is final and can no further be sub-classed than use composition and delegation.
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 :