Object Delegation in C++ Last Updated : 18 Aug, 2021 Comments Improve Suggest changes Like Article Like Report 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. Comment More infoAdvertise with us Next Article Object Delegation in C++ Z zack_aayush Follow Improve Article Tags : C++ Programs C++ CPP-Basics C++-Class and Object Practice Tags : CPP Similar Reads Serialize and Deserialize an Object in C++ In C++, serialization is the process of converting an object into a sequence of bytes so that it can be stored in memory or transmitted across a network and deserialization is the reverse process, where the byte stream is used to reconstruct the original object. In this article, we will learn how we 4 min read Dynamic initialization of object in C++ In this article, we will discuss the Dynamic initialization of objects using Dynamic Constructors. Dynamic initialization of object refers to initializing the objects at a run time i.e., the initial value of an object is provided during run time.It can be achieved by using constructors and by passin 4 min read Life cycle of Objects in C++ with Example In Object Oriented Programming, Objects are the instances of a class which has its own state(variables) and behavior(methods). Every class has two special methods related with creation and destruction of object of a class- constructors and destructors. C++ Object Life Cycle: There are various steps 6 min read Reflection in C++ Reflection in C++ is defined as the ability of a program to examine and modify its structure and behavior at runtime. This powerful feature enables dynamic code generation, introspection, and metaprogramming. While C++ does not have built-in reflection capabilities like other languages, it offers te 5 min read How to Use Deleted Functions to Prevent Object Copying in C++? In C++, the class always has a copy constructor and assignment operator, whether it is default or user-defined which allows the program to create copies of the objects of that class. But sometimes, we need to create a class whose object should not be copied. In this article, we will learn how to use 2 min read Like