#include "stdafx.h"
#include <iostream>
using namespace std;
/* 情形一
// 输出为:
// Output from the constructor of class ClxBase!
// Output from the constructor of class ClxDerived!
// Do something in class ClxDerived!
// Output from the destructor of class ClxDerived!
// Output from the destructor of class ClxBase!
*/
class ClxBase
{
//本例父类中析构函数是否为virtual对输出结果没有影响
public:
ClxBase() { cout << "Output from the constructor of class ClxBase!" << endl; };
virtual ~ClxBase() { cout << "Output from the destructor of class ClxBase!" << endl; };
virtual void DoSomething() { cout << "Do something in class ClxBase!" << endl; };
};
class ClxDerived : public ClxBase
{
public:
ClxDerived() { cout << "Output from the constructor of class ClxDerived!" << endl; };
~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; };
void DoSomething() { cout << "Do something in class ClxDerived!" << endl; };
};
int _tmain(int argc, _TCHAR* argv[])
{
ClxDerived *pTest = new ClxDerived;
pTest->DoSomething();
delete pTest;
return 0;
}
/* 情形二
// 输出为:
// Output from the constructor of class ClxBase!
// Output from the constructor of class ClxDerived!
// Do something in class ClxDerived!
// Output from the destructor of class ClxDerived!
// Output from the destructor of class ClxBase!
*/
class ClxBase
{
//本例父类中析构函数是否为virtual对输出结果没有影响
public:
ClxBase() { cout << "Output from the constructor of class ClxBase!" << endl; };
virtual ~ClxBase() { cout << "Output from the destructor of class ClxBase!" << endl; };
void DoSomething() { cout << "Do something in class ClxBase!" << endl; };
};
class ClxDerived : public ClxBase
{
public:
ClxDerived() { cout << "Output from the constructor of class ClxDerived!" << endl; };
~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; };
void DoSomething() { cout << "Do something in class ClxDerived!" << endl; };
};
int _tmain(int argc, _TCHAR* argv[])
{
ClxDerived *pTest = new ClxDerived;
pTest->DoSomething();
delete pTest;
return 0;
}
/* 情形三
// 输出为:
// Output from the constructor of class ClxBase!
// Output from the constructor of class ClxDerived!
// Do something in class ClxBase!
// Output from the destructor of class ClxDerived!
// Output from the destructor of class ClxBase!
*/
class ClxBase
{
//本例父类中析构函数和DoSomething()函数是否为virtual对输出结果没有影响
public:
ClxBase() { cout << "Output from the constructor of class ClxBase!" << endl; };
virtual ~ClxBase() { cout << "Output from the destructor of class ClxBase!" << endl; };
virtual void DoSomething() { cout << "Do something in class ClxBase!" << endl; };
};
class ClxDerived : public ClxBase
{
public:
ClxDerived() { cout << "Output from the constructor of class ClxDerived!" << endl; };
~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; };
};
int _tmain(int argc, _TCHAR* argv[])
{
ClxDerived *pTest = new ClxDerived;
pTest->DoSomething();
delete pTest;
return 0;
}