diff --git a/programs/Inheritance/Multilevel Inheritance/q2.cpp b/programs/Inheritance/Multilevel Inheritance/q2.cpp new file mode 100644 index 0000000..0c0fc8a --- /dev/null +++ b/programs/Inheritance/Multilevel Inheritance/q2.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; + +// Base class +class Base +{ +public: + void displayBase() + { + cout << "This is the Base class." << endl; + } +}; + +// Intermediate class derived from Base +class Intermediate : public Base +{ +public: + void displayIntermediate() + { + cout << "This is the Intermediate class." << endl; + } +}; + +// Derived class derived from Intermediate +class Derived : public Intermediate +{ +public: + void displayDerived() + { + cout << "This is the Derived class." << endl; + } +}; + +// Main function +int main() +{ + // Creating an object of the Derived class + Derived obj; + + // Accessing methods from all levels of inheritance + obj.displayBase(); // Method from Base class + obj.displayIntermediate(); // Method from Intermediate class + obj.displayDerived(); // Method from Derived class + + return 0; +} diff --git a/programs/Inheritance/README.md b/programs/Inheritance/README.md index 5056fa4..8f87366 100644 --- a/programs/Inheritance/README.md +++ b/programs/Inheritance/README.md @@ -44,8 +44,8 @@ ## Multilevel Inheritance - [x] [q1](./Multilevel%20Inheritance/q1.cpp): WAP to show multi level inheritance. -- [ ] [q2](./Multilevel%20Inheritance/q2.cpp): WAP to show multilevel inheritance with base, intermediate, and derived class. -- [ ] [q3](./Multilevel%20Inheritance/q3.cpp): WAP to demonstrate multilevel inheritance where each class has its own method. +- [x] [q2](./Multilevel%20Inheritance/q2.cpp): WAP to show multilevel inheritance with base, intermediate, and derived class. +- [x] [q3](./Multilevel%20Inheritance/q3.cpp): WAP to demonstrate multilevel inheritance where each class has its own method. - [ ] [q4](./Multilevel%20Inheritance/q4.cpp): WAP to show multilevel inheritance where each class contains its own constructor and destructor. - [ ] [q5](./Multilevel%20Inheritance/q5.cpp): WAP to demonstrate multilevel inheritance with virtual functions. - [ ] [q6](./Multilevel%20Inheritance/q6.cpp): WAP to show multilevel inheritance where the derived class overrides methods from both intermediate and base classes.