首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程pmjl,此工程中包含了类Pets(“宠物”)和主函数main的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程pmjl,此工程中包含了类Pets(“宠物”)和主函数main的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
admin
2019-05-22
12
问题
请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程pmjl,此工程中包含了类Pets(“宠物”)和主函数main的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
Name:sonny Type:dog
Name:John Type:dog
Name:Danny Type:eat
Name:John Type:dog
注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。
#include
using namespace st:d;
enllm Pets type{dog,cat,bird,fish};
class PetS{
private:
char*name;
Pets_type type;
public:
Pets(const char*flame=”sonny”,
Pets_type type=dog);
Pets&operator=(const Pets&s);
~Pets();
void show()const;
};
Pets::Pets(const char*name,Pets
type type)
//构造函数
{
this->name=new char[strlen
(name)+1];
strcpy(this一>name,name);
//ERROR*********found*********
type=type;
}
Pets::~Pets()//析构函数,释放name
所指向的字符串
{
//ERROR*********found*********
name=’/0’;
}
Pets&Pets::operator=(const Pets
&s)
{
if(&s=this)//确保不要向自身赋值
return*this;
delete[]name;
name=new char[strlen(S.name)+
1];
//ERROR*********found*********
strcpy(this一>name,name);
type=S.type;
return*thiS ;
}
void Pets::show()const
{
cout<<”Name:”<
switch(type)
{
case dog:cout<<”dog”;break;
case cat:cout<<”cat”;break;
case bird:cout<<”bird”;break;
case fish:cout<<”fish”;break;
}
cout<
}
int main()
{
Pets mypet1,mypet2(”John”,dog);
Pets youpet(”Danny”,cat);
mypet1.show(),
mypet2.shOW();
youpet.show();
youpet=mypet2;
youpet.show();
return 0;
}
选项
答案
(1)this->type=type; (2)delete[]nal/le; (3)strepy(this->nanle,s.name);
解析
(1)主要考查考生对构造函数的掌握情况,因为形参名和类的私有成员名称都是type,为了避免混淆,所以规定类的私有成员使用this指针调用,即:this一>type=type;。
(2)主要考查考生对析构函数的掌握情况,题目中要求,释放name所指向的字符串。要释放name指针用delete语句,即delete[]name;。
(3)主要考查考生对strepy函数的掌握情况,strcpy函数的形参为两个字符串,而name为指向字符串的指针,因此使用语句:strcpy(this一>nallle,s.name);.
转载请注明原文地址:https://jikaoti.com/ti/UrA0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
有如下类和对象的定义:classConstants{public:staticdoublegetPI(){return3.1416;}};Constantsconstants;下列各组语句中,能输
下列有关类继承的叙述中,错误的是
下列有关类成员的叙述中,正确的是
下列关于栈的叙述正确的是
结构化程序设计的基本原则不包括
在关系模型中,二维表的行称为___________。
ABBA此题考查的是类的构造与析构。建立类的对象时,构造函数的执行顺序如下:执行基类的构造函数,调用顺序按照各个基类被继承时声明的顺序(自左向右)->执行成员对象的构造函数,调用顺序按照各个成员对象在类中声明的顺序->执行自身的构造函数。析构顺序与之相反。
>=此题考查的是while循环语句和for循环语句。题中函数的功能是判断字符串是否对称。从字符串两端依次比较各个字符是否相同,for循环结束后,当i>-j表明字符串对称,否则说明不对称。
下列叙述中正确的是A)算法的效率只与问题的规模有关,而与数据的存储结构无关B)算法的时间复杂度是指执行算法所需要的计算工作量C)数据的逻辑结构与存储结构是一一对应的D)算法的时间复杂度与空间复杂度一定相关
某二叉树中有n个度为2的结点,则该二叉树中的叶子结点数为A)n+1B)n-1C)2nD)n/2
随机试题
"Doyoulikebeingmarried?""Yes,butI’mnotused______everynight."
流感病毒的成分中变异性强的是
首选用于治疗小儿风疹邪毒内盛证的方剂是
以下不属于风邪致病特点的是
证券公司从事上市公司并购重组财务顾问业务应当具备的条件包括()。Ⅰ.公司净资本符合中国证监会的规定Ⅱ.财务顾问主办人不少于3人Ⅲ.建立健全的尽职调查制度,具备良好的项目风险评估和内核机制Ⅳ.公司控股股东、实际控制人信誉良好且最近3年无重大
(2011年)下列税务处理中,符合个人独资企业和合伙企业征税规定的有()。
—HowmuchisthetickettoCentralPark?—Aone-wayticket________$40,andyoucan________another$20foraround-trip.
小学三年级学生还不能够根据行为的原因或从行为的因果关系上做出判断。()
巴拿马运河丑闻
In1971researchershopingtopredictearthquakesintheshorttermbyidentifyingprecursoryphenomena(thosethatoccurafew
最新回复
(
0
)