Here’s a C++ program that shows how a MultiLevel Inheritance actually works with the help of Electronic Device, Phone, and Smartphone
If you found this post useful:
#include <iostream> using namespace std; // Base class class ElectronicDevice { public: void powerOn() { cout << "Device ON" << endl; } }; // Derived class (inherits from ElectronicDevice) class Phone : public ElectronicDevice { public: void call() { cout << "Making a call" << endl; } }; // Further derived class (inherits from Phone) class Smartphone : public Phone { public: void browse() { cout << "Browsing internet" << endl; } }; // Main function int main() { Smartphone myPhone; // Accessing methods from all levels myPhone.powerOn(); // From ElectronicDevice myPhone.call(); // From Phone myPhone.browse(); // From Smartphone return 0; }
Output:
Device ON
Making a call
Browsing internet
Short Explanation
-
ElectronicDevice
→ Base class withpowerOn()
method. -
Phone
→ Inherits fromElectronicDevice
, addscall()
. -
Smartphone
→ Inherits fromPhone
, addsbrowse()
. -
In
main()
, objectmyPhone
can use all methods from parent, child, and grandchild classes.
So we can say ,this is Multilevel Inheritance in C++:
ElectronicDevice → Phone → Smartphone
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