Here’s a C++ program that shows how a Multiple Inheritance actually works with the help of Perfomer,Singer and Dancer
If you found this post useful:
#include <iostream> using namespace std; // Base class 1 class Singer { public: void sing() { cout << "Singing..." << endl; } }; // Base class 2 class Dancer { public: void dance() { cout << "Dancing..." << endl; } }; // Derived class (inherits from both Singer and Dancer) class Performer : public Singer, public Dancer { }; int main() { Performer p; p.sing(); // Inherited from Singer p.dance(); // Inherited from Dancer return 0; }
Output:
Singing...
Dancing...
Explanation:
-
Singer and Dancer are two independent base classes.
-
Performer inherits from both, using multiple inheritance.
-
This means
Performer
automatically gets the ability tosing()
anddance()
without writing those methods again. -
When we create a
Performer
object inmain()
, it can call bothsing()
anddance()
.
Real-life analogy: A Performer is both a Singer and a Dancer, so they naturally have both abilities.
Thanks for reading 💗!
⮕ Share it with others who might benefit.
⮕ Leave a comment with your thoughts or questions—I’d love to hear from you.
⮕ Follow/Subscribe to the blog for more helpful guides, tips, and insights.
Comments
Post a Comment