-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from rpandox/feat/mult-levl-inheritance-q7
Feat/mult-levl-inheritance-q7
- Loading branch information
Showing
2 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include <iostream> | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters