code 1.
#include <iostream>
using namespace std;
class Base {
public:
Base() {cout<<"Base constructor"<<endl;}
~Base() {cout<<"Base destructor"<<endl;}
};
class Derived:public Base{
public:
Derived() {cout<<"Derived constructor"<<endl;}
~Derived() {cout<<"Derived destructor"<<endl;}
};
int main()
{
Derived *p = new Derived;
delete p;
return 0;
}
---------------------
[tli]$ ./a.out
Base constructor
Derived constructor
Derived destructor
Base destructor
code 2.
#include <iostream>
using namespace std;
class Base {
public:
Base() {cout<<"Base constructor"<<endl;}
~Base() {cout<<"Base destructor"<<endl;}
};
class Derived:public Base{
public:
Derived() {cout<<"Derived constructor"<<endl;}
~Derived() {cout<<"Derived destructor"<<endl;}
};
int main()
{
Base *p = new Derived;
delete p;
return 0;
}
-----
[tli]$ ./a.out
Base constructor
Derived constructor
Base destructor
code 3.
#include <iostream>
using namespace std;
class Base {
public:
Base() {cout<<"Base constructor"<<endl;}
virtual ~Base() {cout<<"Base destructor"<<endl;}
};
class Derived:public Base{
public:
Derived() {cout<<"Derived constructor"<<endl;}
~Derived() {cout<<"Derived destructor"<<endl;}
};
int main()
{
Base *p = new Derived;
delete p;
return 0;
}
[tli]$ ./a.out
Base constructor
Derived constructor
Derived destructor
Base destructor