您的位置:首页 > 其它

类的构造函数与析构函数调用顺序

2015-06-30 12:40 190 查看
#include <stdio.h>
#include <Windows.h>

class Base
{
public:
	Base() { printf("I am base !\n"); } 
	void play();
	virtual void eat();
	virtual ~Base() { printf("I am ~base !\n"); }
};

void Base::play()
{
	printf("I am base play !\n");
}
void Base::eat()
{
	printf("I am base eat !\n");
}

class Sun : public Base
{
public:
	Sun() { printf("I am sun !\n"); }
	~Sun() { printf("I am ~sun !\n"); }
	void play();
	void eat();
};

void Sun::play() 
{
	printf("I am sun play !\n");
}
void Sun::eat()
{
	printf("I am sun eat !\n");
}

int main()
{
	Base *b = new Base;
	Base *p = new Sun;
	Sun *s = new Sun;

	b->play();
	b->eat();

	p->play();
	p->eat();

	s->play();
	s->eat();

	delete b;
	delete p;
	delete s;
	b = NULL;
	p = NULL;
	s = NULL;

	system("pause");
	return 0;

}


输出结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: