首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,其中有枚举DOGCOLOR、狗类Dog和主函数main的定义。程序中位于每个“//ERROR****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是:
请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,其中有枚举DOGCOLOR、狗类Dog和主函数main的定义。程序中位于每个“//ERROR****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是:
admin
2018-03-13
19
问题
请使用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 motley 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* get;Name()
const{return*name;}
const char* getColor String()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()corlst{
tout;<<"There is a"<
}
};
int main(){
//ERROR********found********
Dog dog1("Hoho",WHITE),dog2("Haha",BLACK);dog3("Hihi",OTHER);
dog1.show();
dog2.show();
dog3.show()j
return 0;
}
选项
答案
(1)this->color=color; (2)const char getName()const{return*name;} (3)Dog dogl("Hoho",WHITE),dog2("Haha",BIACK),dog3("Hihi",OTHER);
解析
(1)主要考查考生对strcpy函数的掌握,如果看到上一条语句strcpy(this->name,name);,就以为本条语句也要用strcpy函数来赋值,这是错误的。Strcpy函数只能复制字符串,根据类的私有成员声明可知,color是DOGCOLOR型的,这里直接使用赋值语句“=”即可。
(2)主要考查考生对函数返回值的掌握,先解读语句const char*getName()const{return*name;},要返回的是一个const的字符指针,同时函数内的值不能改变,name在类的私有成员声明中是个字符数组,*name代表字符数组而不是字符指针,问题就出来了,需要修改返回类型:const char getName()const{return*name;}。
(3)语法错误,定义变量时,变量之间应使用“,”分开。
转载请注明原文地址:https://jikaoti.com/ti/fLt0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
以下类定义中可能会引起错误的语句是classA{public:~A(){}//1inti;//2private:intj;//3
下列对派生类的描述中,错误的是
已知类Myst有一个只需要一个double型参数的构造函数,且将运算符“-”重载为友元函数。要使语句序列Mystx{3.2),y(5.5),z(0.0);z=8.9-y;y=x-6.3;能够正常运行,运算符函数ope
定义如下枚举类型:enum{Monday,Tuesday,Wednesday,Thrusday,Friday=2};则下列语句正确的是
若执行语句:cout<<setfill(’*’)<<setw(10)<<123<<"OK"<<end1后将输出
执行语句序列intn:cin>>n:switch(n){case1:case2:cout<<’1’;case3:case4:cout<<’2’:break;
______允许用户为类定义一种模式,使得类中的某些数据成员及某些成员函数的返回值能取任意类型。
下列程序的输出结果为2,请将程序补充完整。#include<iostream>usingnamespacestd;classBase{public:______voidfun(){cout<<1;}
随机试题
下列有关审计要素的相关说法中,正确的是()。
眼震颤可见于
患儿颈部瘰疬,疮口脓出清稀,夹有败絮样物,形体消瘦,精神倦怠,面色无华,舌淡质嫩,苔薄,脉细。治疗常用方剂是()
女,44岁,经诊断为慢性牙周炎并经基础治疗4周后,左上6牙周袋探诊深度仍然为6mm,且探诊易出血,龈退缩1mm,附着龈宽度为4mm,X-ray示根分叉区透射,则该患者最适宜做
下列哪一项不属于我国发行短期债券的期限?()
生产力决定生产关系,但是生产关系不能反作用于生产力。()
在生产关系中,具有决定意义的是______。
Outside-the-classroomLearningPuttingagroupofcollegestudentsinchargeofa$300,000DanceMarathon,fundraisersurel
Whathappenswhenonestudenthurtsanother’sindividualrights?Standardsandrulesmustbeestablishedthatmaintainorder,en
A、Shewantstogetsomesleep.B、Sheneedstimetowriteapaper.C、Shehasaliteratureclasstoattend.D、Sheistroubledbyh
最新回复
(
0
)