c++虛函數

含有虛函數的類是



#include<iostream>
using namespace std;
class Animal
{
public:
	virtual void Shout()=0;//純虛函數
	virtual void Impl()=0;
	Animal()
	{
		printf("Animal Constructor\n");
	}
	virtual ~Animal()
	{
		printf("Animal destroy!\n");
	}
};

class Dog:public Animal
{
public:
	Dog()
	{
		printf("Dog Constructor\n");
	}
	virtual void Shout()
	{
		printf("wang!\n");
	}
	virtual void Impl()
	{
		printf("implement of Dog!\n");
	}
	virtual ~Dog(){printf("Dog destroy!\n");}
};

void Test_func()
{
	//Animal a; //抽象類不能建立對象
	Dog dog;
	Animal *animal=&dog;
	animal->Shout();
	animal->Impl();
}
int main()
{
	Test_func();
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章