#include<iostream>
using namespace std;
class Father
{
public:
Father()
{
cout<<"father"<<endl;
}
Father(int)
{
cout<<"int father"<<endl;
}
~Father()
{
cout<<"~father"<<endl;
}
virtual void show()
{
cout<<"father.show"<<endl;
}
};
class Sun:public Father
{
public:
Sun()
{
cout<<"sun"<<endl;
}
Sun(int)
{
cout<<"int sun"<<endl;
}
~Sun()
{
cout<<"~sun"<<endl;
}
void show()
{
cout<<"sun.show"<<endl;
}
};
int main()
{ Sun s;
Father f;
// s.show();
// f.show();
Father *p=&s;//--------------不加virtual输出的是父类的show。
p->show();
}
多态->虚函数
最新推荐文章于 2024-09-05 20:03:57 发布