#include <iostream>
using namespace std;
#include <conio.h>
class base
{
public:
void who()
{
cout<<"base class!"<<endl;
}
};
class derive1:public base
{
public:
void who()
{
cout<<"derive1 class"<<endl;
}
};
class derive2:public base
{
public:
void who()
{
cout<<"derive2 class"<<endl;
}
};
void main()
{
base obj1,*p;
derive1 obj2;
derive2 obj3;
p=&obj1;
p->who();
p=&obj2;
p->who();//仍调用基类的who,失败;把基类的who申明为 virtual 后,调用派生类的who
p=&obj3;//仍调用基类的who,失败;把基类的who申明为 virtual 后,调用派生类的who
p->who();
obj2.who();
obj3.who();
char c =getch();
return;
}