使用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了用于表示人基本信息的类CHumanlnfo,但类CHumanln~的定义并不完整。请按要求完成下列操作,将类CHumanlnfo的定义补充完成: (1)定义私有数据成员bloodT

admin2021-02-22  32

问题 使用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了用于表示人基本信息的类CHumanlnfo,但类CHumanln~的定义并不完整。请按要求完成下列操作,将类CHumanlnfo的定义补充完成:
    (1)定义私有数据成员bloodType用于表示血型,血型为char型的数据。请在注释“//****************”之后添加适当的语句。
    (2)完成构造函数的定义,要求具有缺省值,缺省值为身高175,体重70,血型A。请在注释“//********2********之后添加适当的语句。
    (3)完成类外CHumanlnfo成员函数Setlnfo的定义。请在注释“//********3********之后添加适当的语句。
    (4)在主函数中调用成员函数Setlnfo,把对象d2的三个私有数据成员分别设定为身高170,体重64,血型为B。请在注释“//********4********”之后添加适当的语句。
    注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
    1    #include
    2  class CHumanInfo
    3    {
    4   private:
    5    int height ;
    6    int weight;
    7    //********1********
    8
    9   public:
    10    //********2********
    11
    12    :height(ht),weight(wt),
        bloodType(bt){);
    13    CHumanlnfo(CHumanInfo&h1):
        height(h1.height),
    14    weight(h1.weight),
        bloodType(h1.bloodType){);
    15    int GetHeight()
    16    {
    17    return height ;
    18    }
    19    int GetWeight()
    20    {
    21    return weight;
    22    }
    23    int GetBloodType()
    24    {
    25    return bloodType ;
    26    }
    27    void  SetInfo(int ht,intwt,char bt);
    28    void Di splay();
    29  };
    30    //********3********
    31
    32  {
    33 height=ht ;
    34    weight=wt;
    35    bloodType=bt;
    36  }
    37  void CHumanInfo::Display()
    38  {
    39    cout<<"HumanInfo:";
    40    cout  <    weight<<"Kg,BloodType"<<
    bloodType((endl;
    41    }
    42  VOid mein()
    43    {
    44    CHumanlnfo h1(169,61,’A’);
    45    CHumanInfo h2;
    46    CHumanInfo h3(h1);
    47    CHuman工nfo h4(h2);
    48    //********4********
    49   
    50   h1.Display();
    51   h2.Display();
    52   h3.Display();
    53   h4.Di splay();
    54    }

选项

答案(1)添加语句:char bloodType; (2)添加语句:CHumanInfo(int ht=175,intwt=70,char bt=’A’) (3)添加语句:void CHumanlnfo::SetInfo(intht,int wt,char bt) (4)添加语句:h2.SetInfo(17 0,64,’B’);

解析 类CHumanlnfo有3个成员变量:用于表示血型的bloodType、表示身高的height和表体重的weight,成员函数GetHeight()返回height值,GetWeight()返回weight值,GetBloodType()返回bllodType值,Setlnfo(int ht,int wt,char bt)可改变bloodType、height和weight值,成员函数Display()在屏幕上打印三个成员变量值。
    [解题思路]
    (1)第1个标识下定义私有数据成员char型的bloodType,故第1个标识下应添加“char bloodType;”。
    (2)构造CHumanlnfo()完成三个成员的初始化,并且带有缺省值参数,缺省值为身高175,体重70,血型A,由函数体语句可知参数名分别为ht、wt和bt,因此第2个标识下应添加“CHumanlnfo(int ht=175,int wt=70,charbt=’A’)”。
    (3)第3个标识下在类外完成成员函数Setlnfo的定义,在类外定义成员函数的格式为:<返回值类型><类名>::<成员函数>(<参数表>),故第3个标识下应添加“voidCHumanlnfo::Setlnfo(int ht,int wt,char bt)”。
    (4)调用函数Setlnf00需要3个参数,程序要求把对象d2的三个私有数据成员分别设定为身高170,体重64,血型为B,即把这三个值传入函数Setlnfo(),因此第4个标识下应添加“h2.Setlnfo(170,64,’B’);”。
转载请注明原文地址:https://kaotiyun.com/show/Phfp777K
0

相关试题推荐
最新回复(0)