问答题 使用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了用于表示人基本信息的类CHumanlnfo,但类CHumanln的定义并不完整。请按要求完成下列操作,将类CHumanInfo的定义补充完成: (1)定义私有数据成员bloodType用于表示血型, 血型为char型的数据。请在注释“∥********1********之后添加适当的语句。 (2)完成构造函数的定义,要求具有缺省值,缺省值为身高175,体重70,血型A。请在注释“∥********2********之后添加适当的语句。 (3)完成类外CHumanlnfo成员函数Setlnfo的定义。请在注释“∥********3********”之后添加适当的语句。 (4)在主函数中调用成员函数SetInfo,把对象d2的三个私有数据成员分别设定为身高170,体重64,血型为B。请在注释“∥********4********,,之后添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。 #include Class CHumanInfo { private: int height; int weight; ∥********1******** public: ∥********2******** :height(ht),weight(wt), bloodType(bt){}; CHumanlnfo(CHumanInfo&hi): height(h1.height), weight(h1.weight), bloodType(h1.bloodType){); int GetHeight() { return height; } int GetWeight() { return welght; } int GetBloodType() { return bloodType; } void SetInfo(int ht,int wt,char bt); void Di splay(); }; ∥********3******** { height=ht; weight=wt; bloodType=bt; } void CHumanInfo::Display() { cout<<“HumanInfo:”; cout<
【正确答案】正确答案:(1)添加语句:char bloodType; (2)添加语句:CHumanInfo(int ht=175,int wt=7 0,char bt:’A’) (3)添加语句:void CHumanInfo::SetInfo(int ht,int wt,char bt) (4)添加语句:h2.SetInfo(170,64,'B');
【答案解析】解析:类CHumanlnfo有3个成员变量:用于表示血型的bloodType、表示身高的height和表体重的weight,成员函数GetHeight()返回height值,GetWeight()返回weight值,GetBloodType()返回bllodType值,Setlnfo(int ht,int wt,char bt)可改变bloodType、height和weight值,成员函数Display()在屏幕上打印三个成员变量值。