Created
June 20, 2020 16:48
-
-
Save javedbaloch4/4a908be3096ebfbbaa52805ca384f080 to your computer and use it in GitHub Desktop.
The C++ using virtual destructor to avoid memory leaks.
This file contains hidden or 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
| // Written by Javed Ahmed - F2019266402 | |
| #include <iostream> | |
| using namespace std; | |
| class Base { | |
| public: | |
| virtual void func() { | |
| cout << "Base Fun is called\n"; | |
| } | |
| ~ Base () { | |
| cout << "Base Destructor is called\n"; | |
| } | |
| }; | |
| class Derived : public Base { | |
| public: | |
| virtual void func() { | |
| cout << "Dervied class func is called\n"; | |
| } | |
| ~ Derived () { | |
| cout << "Dervied class destructor is callled.\n"; | |
| } | |
| }; | |
| int main() | |
| { | |
| Base *b1 = new Base(); | |
| Base *b2 = new Derived(); | |
| b1->func(); | |
| b2->func(); | |
| delete b1; | |
| delete b2; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment