回复 3楼 yangfrancis
先搞错了,忘了继承了。应该这样:
#include<iostream>
using namespace std;
class grandfather
{
public:
grandfather(){cout<<"构造一个祖父类对象\n";}
virtual void AgeDescription(){cout<<"年纪在70以上\n";}
};
class father:public grandfather
{
public:
father(){cout<<"构造一个父类对象\n";}
virtual void AgeDescription(){cout<<"年纪在25以上\n";}
};
class son:public father
{
public:
son(){cout<<"构造一个儿子类对象\n";}
void AgeDescription(){cout<<"年纪在5岁以下\n";}
};
int main()
{
grandfather person1;
person1.AgeDescription();
father person2;
person2.AgeDescription();
son person3;
person3.AgeDescription();
return 0;
}
father类将执行本身及祖父类的构造函数,son类将执行本身,父亲类和祖父类的构造函数。但AgeDescription由于用上虚函数,将只执行各自的函数。