#include <iostream>
using namespace std;
class myjob
{
public:
virtual void say()
{
cout<<"i am virtual function!"<<endl;
}
};
class son:public myjob
{
public:
virtual void say()
{
cout<<"i am son!"<<endl;
}
};
class daughter:public myjob
{
public:
virtual void say()
{
cout<<"i am daughter!"<<endl;
}
};
int main()
{
//将子类对象指针赋给父类
myjob* father=new son();
//调用虚函数
father->say();
return 0;
}
C++多态的实现
最新推荐文章于 2024-06-18 12:11:18 发布