请打开考生文件夹下的解决方案文件proj1,此工程中含有一个源程序文件proj1.cpp”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: Constructor called. The value is 10 Max

admin2019-04-24  29

问题 请打开考生文件夹下的解决方案文件proj1,此工程中含有一个源程序文件proj1.cpp”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
    Constructor called.
    The value is 10
    Max nllmber is 20
    Destruetor called.
    注意:只能修改注释“//ERROR****found****”的下一行语句,不要改动程序中的其他内容。
    1  //proj1.cpp
    2  #include
    3  using namespace std;
    4
    5  class MyClass {
    6 public:
    7    //ERROR ********found********
    8    void MyClass(int i)
    9    {value=i;cout<<"Con structor called."<    10
    11    int Max(int x,int y){return x>y?x:y;}//求两个整数的最大值
    12
    13    //ERROR  ********found********
    14    int Max(int x,int y,int z=0)
    15  //求三个整数的最大值
    16    {
    17    if(x>y)
    18    return x>z?x:z ;
    19    else
    20    return y>z?y:z ;
    21   
    22
    23    int GetValue()const { return  value;}
    24
    25    ~MyClass () {cout<<"De structor called."<    26
    27 private:
    28    int value;
    29  };
    30
    3l  int main()
    32  f
    33    MyClass obj(10);
    34    //ERROR********found********
    35    cout<<"The value is"  <<  value()<    36    cout<<"Max number is"  <<   obj.Max(10,20)<    37    return 0;
    38  }

选项

答案(1)MyClass(int i) (2)int Max(int x,int y,int z) (3)cout<<"The value is"<
解析 (1)考查构造函数,构造函数前不加void或其他任何类型名,直接使用MyClass(int i)即可。
    (2)主要考查函数重载,在int Max(int x,int y)  {return x>y?x:y;}中两个形参变量都是int型,而语句int Max(int x,inty,int z=0)的前两个形参也都是int型,第三个形参定义默认值,那么这两个Max函数在调用时它们的参数个数和参数类型都一样,因为函数重载要求形参类型或形参个数不同,所以要把int z=0改为int z,才能构成函数重载。
    (3)主要考查成员函数的调用,因为value是私有成员,所以不能被类外函数直接调用,而且value()的用法也是错误的,可以使用成员函数obj.GetValue()得到value的值。
转载请注明原文地址:https://jikaoti.com/ti/OYt0FFFM
0

最新回复(0)