#include<iostream>
using namespace std;
class Tree{
public:
virtual void fa(){cout<<"Tree:higher"<<endl;}
virtual void fb(){cout<<"Tree:lower"<<endl;}
virtual int fc(int m){ return 3*m; }
};
class Grass:public Tree{
public:
// void fa(){cout<<"Grass:wide"<<endl;}
void fc(){cout<<"Grass:thin"<<endl;}
};
class Leaf:public Grass{ //b
public:
void fa(){cout<<"Leaf:green"<<endl;}
void fb(int i=0){cout<<"Leaf_age:"<<i<<endl;}
int fc(int n=1){cout<<"Leaf_num="<<n<<endl;return 0;}
};
int main()
{
Grass a;
Leaf b;
Tree c,*p=&b;
b.fb(5);
p->fb();
/*a.fc();
p->fa();
p->fb(); //Tree:lower
b.fb(c.fc(6));
b.fc(p->fc(6));*/
return 1;
}
这是输出结果
第二个p->fb()为什么输出基类里面的呢,我以为是Leaf_age:1
两个函数不一样,没有起到动态绑定的功能