资源预览内容
第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
第9页 / 共10页
第10页 / 共10页
亲,该文档总共10页全部预览完了,如果喜欢就下载吧!
资源描述
下列代码中包含了类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);Pets& 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)/堆内存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 *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*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: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 AverageRecord()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()/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 *name = 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);Pets: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.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 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) StudentInfo(); float AverageRecord()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)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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号