基类指针创建子类对象 构造顺序 面试必考

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "iostream"
using namespace std;

class A
{
public:
	A() 
	{
		cout << 1 << ends;
		func1(); 
	}
	~A() 
	{
		cout << 2 << ends;
	}

	virtual void func1()
	{
		cout << 3 << ends;
		func2();
	}

	virtual void func2()
	{
		cout << 4 << ends;
		func3();
	}

	void func3() 
	{
		cout << 5 << ends;
	}

};

class B : public A
{
public:
	B() 
	{ 
		cout << 6 << ends;
		func1(); 
	}
	~B() 
	{
		cout << 7 << ends;
	}

	virtual void func1()
	{
		cout << 8 << ends;
		func2();
	}

	void func2()
	{
		cout << 9 << ends;
		func3();
	}

	void func3()
	{
		cout << 10 << ends;
	}
};


int _tmain(int argc, _TCHAR* argv[])
{
	A* pb = new B;	// 1 3 4 5 6 8 9 10
	delete pb;		// 2
	return 0;
}


你可能感兴趣的:(C++)