期末试卷-改错题-例题

上传人:风** 文档编号:118868745 上传时间:2019-12-27 格式:DOCX 页数:10 大小:16.33KB
返回 下载 相关 举报
期末试卷-改错题-例题_第1页
第1页 / 共10页
期末试卷-改错题-例题_第2页
第2页 / 共10页
期末试卷-改错题-例题_第3页
第3页 / 共10页
期末试卷-改错题-例题_第4页
第4页 / 共10页
期末试卷-改错题-例题_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《期末试卷-改错题-例题》由会员分享,可在线阅读,更多相关《期末试卷-改错题-例题(10页珍藏版)》请在金锄头文库上搜索。

1、下列代码中包含了类Pets(“宠物”) 和主函数main的定义。程序中位于每个“/ERROR *found*”之后的一行语句有错误,请加以改正,不要改动程序中的其他内容。改正后程序的输出结果应为:Name:Sonny Type:dogName:John Type:dogName:Danny Type:catName:John Type:dog#includeusing namespace std;class Pets private:char *name;char *type;public:Pets(const char *name = Sonny, char *type = dog);Pet

2、s& operator = (const Pets& s);Pets();void show() const;/ERROR *found*(1) /默认参数函数Pets:Pets(const char *name = Sonny, char *type = dog)this-name = new charstrlen(name) + 1;strcpy(this-name, name);/ERROR *found*(2) /this指针type = new charstrlen(type) + 1;strcpy(this-type, type);Pets:Pets()/ERROR *found*

3、(3)/堆内存name = /0;delete type;Pets& Pets:operator = (const Pets& s) /ERROR *found*(4)/赋值运算符 自检查if (s = *this) return *this;delete name;delete type;name = new charstrlen(s.name) + 1;/ERROR *found*(5)strcpy(this-name, name);type = new charstrlen(s.type) + 1;strcpy(this-type, s.type);return *this;/ERROR

4、 *found*(6) /类体外成员函数实现void show() const cout Name: name t Type: type endl;int main() Pets mypet1, mypet2(John, dog), mypet3(Danny, cat);mypet1.show();mypet2.show();mypet3.show();mypet3 = mypet2;mypet3.show();return 0;下列代码中包含了类StudentInfo (“学生信息”) 和主函数main的定义。程序中位于每个“/ERROR*found*”之后的一行语句有错误,请加以改正。改正

5、后程序的输出结果应为:Name:Smith Age:21 ID:99999 CourseNum:12 Record:970#includeusing namespace std;class StudentInfoprotected:/ERROR*found*(1)char Name;int Age, ID, CourseNum;float Record;public:StudentInfo(char* name, int Age, int ID, int courseNum, float record);/ERROR*found*(2)void StudentInfo() float Aver

6、ageRecord()return Record/CourseNum;void show() const;/ERROR*found*(3) StudentInfo StudentInfo(char* Name, int Age,int ID, int CourseNum, float Record)Name= new charstrlen(name)+1;strcpy(Name,name);/ERROR *found*(4)Age = Age;this-ID = ID;CourseNum = courseNum;Record = record;StudentInfo:StudentInfo()

7、/ERROR *found*(5) delete Name;/ERROR *found*(6)void StudentInfo:show()coutName:Name Age:Age ID:ID CourseNum: CourseNum Record:Recordendl;int main()StudentInfo st(Smith,21,99999,12,970);st.show();return 0;#includeusing namespace std;class Pets private:char *name;char *type;public:Pets(const char *nam

8、e = Sonny, char *type = dog);Pets& operator = (const Pets& s);Pets();void show() const;/ERROR *found*(1) /默认参数函数Pets:Pets(const char *name, char *type)this-name = new charstrlen(name) + 1;strcpy(this-name, name);/ERROR *found*(2) /this指针this-type = new charstrlen(type) + 1;strcpy(this-type, type);Pe

9、ts:Pets()/ERROR *found*(3)/堆内存deletename;delete type;Pets& Pets:operator = (const Pets& s) /ERROR *found*(4)/赋值运算符 自检查if (&s = this) return *this;delete name;delete type;name = new charstrlen(s.name) + 1;/ERROR *found*(5)strcpy(this-name, s.name);type = new charstrlen(s.type) + 1;strcpy(this-type, s

10、.type);return *this;/ERROR *found*(6) /类体外成员函数实现void Pets:show() const cout Name: name t Type: type endl;int main() Pets mypet1, mypet2(John, dog), mypet3(Danny, cat);mypet1.show();mypet2.show();mypet3.show();mypet3 = mypet2;mypet3.show();return 0;#includeusing namespace std;class StudentInfoprotect

11、ed:/ERROR*found*(1)char* Name;int Age, ID, CourseNum;float Record;public:StudentInfo(char* name, int Age, int ID, int courseNum, float record);/ERROR*found*(2) StudentInfo(); float AverageRecord()return Record/CourseNum;void show() const;/ERROR*found*(3) StudentInfo:StudentInfo(char* name, int Age,

12、int ID, int courseNum, float record)Name= new charstrlen(name)+1;strcpy(Name,name);/ERROR *found*(4)this-Age = Age;this-ID = ID;CourseNum = courseNum;Record = record;StudentInfo:StudentInfo()/ERROR *found*(5) delete Name;/ERROR *found*(6)void StudentInfo:show() constcoutName:Name Age:Age ID:ID CourseNum: CourseNum Record:Recordendl;int main()StudentInfo st(Smith,21,99999,12,970);st.show();r

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 资格认证/考试 > 其它考试类文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号