问答题
使用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了用于表示人基本信息的类CHumanlnfo,但类CHumanlnfo的定义并不完整。请按要求完成下列操作,将类CHumanlnfo的定义补充完成: (1)定义私有数据成员bloodType用于表示血型,血型为char型的数据。请在注释“//********1********”之后添加适当的语句。 (2)完成构造函数的定义,要求具有缺省值,缺省值为身高175,体重70,血型A。请在注释“//********2********”之后添加适当的语句。 (3)完成类外CHumanInfo成员函数Setlnfo的定义。请在注释“//********3********”之后添加适当的语句。 (4)在主函数中调用成员函数SetInfo,把对象d2的三个私有数据成员分别设定为身高170,体重64,血型为B。请在注释“//********4********”之后添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。#include<iostream.h>class CHumanInfo{private: int height; int weight; //********1********public: //********2******** :height(ht),weight(wt),bloodType(bt){}; CHumanInfo(CHumanInfo &h1):height(h1.height), weight(h1.weight),bloodType(h1.bloodType){}; int GetHeight() { return height; } int GetWeight() { return weight; } int GetBloodType() { return bloodType; } void SetInfo(int ht,int wt,char bt); void Display(); }; //********3******** { height=ht; weight=wt; bloodType=bt; } void CHumanInfo::Display() { cout<<"HumanInfo:"; cout<<height<<"cm, "<<weight<<"Kg,BloodType"<<bloodType<<end1;}void main(){ CHumanInfo h1(169,61,'A'); CHumanInfo h2; CHumanInfo h3(h1); CHumanInfo h4(h2); //********4******** h1.Display(); h2.Display(); h3.Display(); h4.Display();}
【正确答案】正确答案:(1)添加语句:char bloodType; (2)添加语句:CHumanInfo(int ht=175,int wt=70,char bt='A') (3)添加语句: void CHumanInfo::SetInfo(int ht,int wt,char bt) (4)添加语句:h2.SetInfo(170,64,'B');
【答案解析】解析:(1)第1个标识下定义私有数据成员char型的bloodType,故第1个标识下应添加“charbloodType;”。 (2)构造CHumanInfo()完成三个成员的初始化,并且带有缺省值参数,缺省值为身高175,体重70,血型A,由函数体语句可知参数名分别为ht、 wt和bt,因此第2个标识下应添加“CHumanInfo(int ht=175,intwt=70,char bt='A')”。 (3)第3个标识下在类外完成成员函数SetInfo的定义,在类外定义成员函数的格式为:<返回值类型><类名>::<成员函数>(<参数表>),故第3个标识下应添加“void CHumanInfo::SetInfo(int ht,int wt,char bt)”。 (4)调用函数SetInfo()需要3个参数,程序要求把对象d2的三个私有数据成员分别设定为身高170,体重64,血型为B,即把这三个值传入函数SetInfo(),因此第4个标识下应添加“h2.SetInfo(170,64,'B');”。