Open In App

Object Delegation in C++

Introduction:

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:



Below is the C++ program illustrating Object Delegation:




// 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:

Article Tags :