Commit 2d062511d357dfa68f7d94ca9d979555cdf351e7
1 parent
3a6661da
Added virtual method call in destructor example
Showing
1 changed file
with
28 additions
and
0 deletions
examples10/destructor.cpp
0 → 100644
| 1 | +#include <iostream> | |
| 2 | +using namespace std; | |
| 3 | + | |
| 4 | +class base | |
| 5 | +{ | |
| 6 | +public: | |
| 7 | + base() { cout << "base()" << endl;} | |
| 8 | + virtual void f() { cout << "base" << endl; }; | |
| 9 | + void g() { this->f(); }; | |
| 10 | + virtual ~base() { this->f(); cout << "~base()" << endl;}; | |
| 11 | +}; | |
| 12 | + | |
| 13 | +class derived : public base | |
| 14 | +{ | |
| 15 | +int *ptr; | |
| 16 | +public: | |
| 17 | + derived() { ptr = new int(7); cout << "derived()" << endl;}; | |
| 18 | + ~derived() { delete ptr; cout << "~derived()" << endl;}; | |
| 19 | + void f() override { cout << "derived " << *ptr << endl; }; | |
| 20 | +}; | |
| 21 | + | |
| 22 | +int main() | |
| 23 | +{ | |
| 24 | + base* ptr = new derived; | |
| 25 | + ptr -> g(); | |
| 26 | + ptr -> f(); | |
| 27 | + delete ptr; | |
| 28 | +} | ... | ... |