Skip to content

Instantly share code, notes, and snippets.

@javedbaloch4
Created June 20, 2020 16:48
Show Gist options
  • Save javedbaloch4/4a908be3096ebfbbaa52805ca384f080 to your computer and use it in GitHub Desktop.
Save javedbaloch4/4a908be3096ebfbbaa52805ca384f080 to your computer and use it in GitHub Desktop.
The C++ using virtual destructor to avoid memory leaks.
// 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