From e14ee4976831ee63fd9766f309e1013524b6d4de Mon Sep 17 00:00:00 2001 From: Roshan Pandey <88703488+rpandox@users.noreply.github.com> Date: Wed, 23 Oct 2024 00:11:51 +0545 Subject: [PATCH] feat: add the question number 7 in multilevel inheritance and tick the corresponding part --- .../Inheritance/Multilevel Inheritance/q7.cpp | 61 +++++++++++++++++++ programs/Inheritance/README.md | 2 +- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 programs/Inheritance/Multilevel Inheritance/q7.cpp diff --git a/programs/Inheritance/Multilevel Inheritance/q7.cpp b/programs/Inheritance/Multilevel Inheritance/q7.cpp new file mode 100644 index 0000000..55df8cb --- /dev/null +++ b/programs/Inheritance/Multilevel Inheritance/q7.cpp @@ -0,0 +1,61 @@ +#include +using namespace std; + +// Base class +class Base { +public: + // Virtual function to enable dynamic dispatch + virtual void display() { + cout << "Display from Base class." << endl; + } + + virtual ~Base() { + cout << "Base class destructor called." << endl; + } +}; + +// Intermediate derived class (inherits from Base) +class Derived1 : public Base { +public: + // Overriding the display function from Base class + void display() override { + cout << "Display from Derived1 class." << endl; + } + + ~Derived1() { + cout << "Derived1 class destructor called." << endl; + } +}; + +// Final derived class (inherits from Derived1) +class Derived2 : public Derived1 { +public: + // Overriding the display function again + void display() override { + cout << "Display from Derived2 class." << endl; + } + + ~Derived2() { + cout << "Derived2 class destructor called." << endl; + } +}; + +int main() { + // Base class pointer pointing to Derived2 object + Base* basePtr = new Derived2(); + + // Dynamic dispatch happens here - Derived2's display function is called + basePtr->display(); + + // Base class pointer pointing to Derived1 object + Base* basePtr2 = new Derived1(); + + // Dynamic dispatch happens here - Derived1's display function is called + basePtr2->display(); + + // Clean up: destructors will be called in reverse order + delete basePtr; + delete basePtr2; + + return 0; +} diff --git a/programs/Inheritance/README.md b/programs/Inheritance/README.md index cbbbcb6..a91c2b6 100644 --- a/programs/Inheritance/README.md +++ b/programs/Inheritance/README.md @@ -49,7 +49,7 @@ - [x] [q4](./Multilevel%20Inheritance/q4.cpp): WAP to show multilevel inheritance where each class contains its own constructor and destructor. - [x] [q5](./Multilevel%20Inheritance/q5.cpp): WAP to demonstrate multilevel inheritance with virtual functions. - [x] [q6](./Multilevel%20Inheritance/q6.cpp): WAP to show multilevel inheritance where the derived class overrides methods from both intermediate and base classes. -- [ ] [q7](./Multilevel%20Inheritance/q7.cpp): WAP to demonstrate multilevel inheritance with method overriding and dynamic method dispatch using pointers. +- [x] [q7](./Multilevel%20Inheritance/q7.cpp): WAP to demonstrate multilevel inheritance with method overriding and dynamic method dispatch using pointers. ## Important