#include <iostream.h>
class animal
{
public:
animal()
{
cout<<"hello kitty"<<endl;
}
virtual void eat()
{
cout<<"eat bianbian"<<endl;
}
};
class plant:public animal
{
public:
plant()
{
}
void eat()
{
cout<<"haha"<<endl;
}
};
void fn(animal *pan)
{
pan->eat ();
}
void main()
{
plant st;
animal*pan; //一個指向animal的指針
pan=&st; //把這個指針換成plant的類空間
fn(pan);//把這個被替換的指針賦給fn(),它貌似指向了,然后通過這個函式指向eat()
}
//函數中有兩個類,而且都有eat()函數最后到底指向哪個eat()呢!
//答:指向基類。如果想指向子類,辦法是有的!就是把基類的eat()函數虛化。加virtual前綴即可!
// `(*∩_∩*)