请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,其中有枚举DOGCOLOR、狗类Dog和主函数main的定义。程序中位于每个“//ERROR****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是:

admin2021-02-22  21

问题 请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,其中有枚举DOGCOLOR、狗类Dog和主函数main的定义。程序中位于每个“//ERROR****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是:
  There is a white dog named Hoho.
  There is a black dog named Haha.
  There is a morley dog named Hihi.
  注意:只修改每个/ERROR****found****下的那一行,不要改动程序中的其他内容。
  #include
  using namespace std;
  //狗的颜色:黑、白、黄、褐、花、其他
  enum DOGCOLOR{BLACK,WHITE,YELLOW,BROWN,PIEBALD,OTHER};
  class Dog{//狗类
    DOGCOLOR color;
    char name[20];
    static int count;
  public:
    Dog(char name[],DOGCOLOR color){
    strcpy(this->name,name);
    //ERROR**********found**********
    strcpy(this->color,color);
    }
    DOGCOLOR getColor()const{return color;}
    //ERROR**********found**********
    const char*getName()const{return*name;}
    const char*getColorStfing()const{
    switch(color){
    case BLACK:return "black":
    case WHITE:return "white":
    case YELLOW:return "yellow":
    case BROWN:return "brown":
    case PIEBALD:return "piebald":
    }
    return"motley";
    }
    void show()const{
    cout<<"There is a"<    }
    };
    int main(){
    //ERROR**********found**********
    Dog dogl("Hoho",WHITE),dog2("Haha",BLACK);dos3("Hihi",OTHER);
    dogl.show();
    dog2.show();
    dog3.show();
    return 0;
    }

选项

答案(1)this->color=color; (2)const char getName()const{return*namc!;} (3)Dog dog1(”}toho”,WHITE),dog2(”Haha”,BLACK),dog3("Hihi",OTHER);

解析 (1)主要考查考生对strcpy函数的掌握,如果看到上一条语句strcpy(this->name,Ilame);,就以为本条语句也要用strcpy函数来赋值,这是错误的。strcpy函数只能复制字符串,根据类的私有成员声明可知,color是DOGC()LOR型的,这里直接使用赋值语句“=”即可。
    (2)主要考查考生对函数返回值的掌握,先解读语句const char*getName()const{return*name;},要返回的是一个const的字符指针,同时函数内的值不能改变,name在类的私有成员声明中是个字符数组,*flame代表字符数组而不是字符指针,问题就出来了,需要修改返回类型:const char getName()const{return*name;}。
    (3)语法错误,定义变量时,变量之间应使用“,”分开。
转载请注明原文地址:https://kaotiyun.com/show/Jzfp777K
0

随机试题
最新回复(0)