ImageVerifierCode 换一换
格式:DOCX , 页数:66 ,大小:33.70KB ,
资源ID:5809882      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5809882.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++课后习题第八章第十二章.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C++课后习题第八章第十二章.docx

1、C+课后习题第八章第十二章第八章1. 请检查下面的程序,找出其中的错误(先不要上机,在纸面上作人工检查),并改正。然后上机调试,使之能正常运行。运行时从键盘输入时、分、秒的值,检查输出是否正确。原文:#include using namespace std;class Time void set_time(void) ; void show_time(void); int hour; int minute; int sec;Time t;int main() set_time(); show_time(); return 0;void set_time(void) cint.hour; cin

2、t.minute; cint.sec;void show_time(void) coutt.hour:t.minute:t.secendl;改:#include using namespace std;class Time public: /成员改为公用的 int hour; int minute; int sec;Time t;void set_time(void) /在 main 函数之前定义 cint.hour; cint.minute; cint.sec;void show_time(void) /在 main 函数之前定义 coutt.hour:t.minute:t.secendl;

3、int main() set_time(); show_time(); return 0;2. 改写例8.1程序,要求:(1)将数据成员改为私有的;(2)将输入和输出的功能改为由成员函数实现;(3)在类体内定义成员函数;#include using namespace std;class Time public: void set_time(void) cinhour; cinminute; cinsec; void show_time(void) couthour:minute:secendl; private: int hour; int minute; int sec;Time t;in

4、t main() t.set_time(); t.show_time(); return 0;3. 在第2题的基础上进行如下修改:在类体内声明成员函数,而在类外定义成员函数。#include using namespace std;class Time public: void set_time(void); void show_time(void); private: int hour; int minute; int sec;void Time:set_time(void) cinhour; cinminute; cinsec;void Time:show_time(void) couth

5、our:minute:secendl;Time t;int main() t.set_time(); t.show_time(); return 0;4. 在第8.3.3节中分别给出了包含类定义的头文件student.h,包含成员函数定义的源文件studnet.cpp以及包含主函数的源文件main.cpp。请对该程序完善化,在类中增加一个对数据成员赋初值的成员函数set_value。原文8.3.3:#include using namespace std;class Student public: void display() coutnum:numendl; coutname:nameend

6、l; coutsex:sexendl; ; private: int num; char name20; char sex ;int main() Student stud; stud.display(); return 0;改:main.cpp#include#include student.husing namespace std;int main() Student s ; s.SetValue(); s.Display(); return 0; Student.husing namespace std;class Student public: void Display(); void

7、 SetValue(); private: int num; char name20; char sex;Student.cpp#include#includestudent.husing namespace std;void Student : Display() cout num: num endl; cout name: name endl; cout sex: sex num name sex;5. 将例8.4改写为一个多文件的程序:(1)将类定义放在头文件arraymax.h中;(2)将成员函数定义放在源文件arrymax.cpp中;(3)主函数放在源文件file1.cpp中。原文例

8、8.4:#include using namespace std;class Array_max public: void set_value(); void max_value(); void show_value(); private: int array10; int max;void Array_max:set_value() int i; for (i=0; iarrayi;void Array_max:max_value() int i; max=array0; for (i=1; imax) max=arrayi;void Array_max:show_value() coutm

9、ax=maxendl;int main() Array_max arrmax; arrmax.set_value(); arrmax.max_value(); arrmax.show_value(); return 0;改:main.cpp#include#includearraymax.husing namespace std;int main() ArrayMax arrmax; arrmax.SetValue(); arrmax.MaxValue(); arrmax.ShowMax(); return 0;arraymax.hclass ArrayMax public: void Set

10、Value(); /设置数组元素值 void MaxValue(); /找出最大值 void ShowMax(); /输出最大值 private: int array10; /整型数组 int max; /数组最大值;arraymax.cpp#include#includearraymax.husing namespace std;void ArrayMax : SetValue() int i; for(i = 0; i arrayi; void ArrayMax : MaxValue() int i; max = array0; for(i = 1; i 10; i+) if(max ar

11、rayi) max = arrayi; void ArrayMax : ShowMax() cout max= max endl;6. 需要求3个长方柱的体积,请编写一个基于对象的程序。数据成员包括length(长)、width(宽)和weight(高)。要求用成员函数实现以下功能。(1)由键盘分别输入3个长方柱的长、宽和高。(2)计算长方柱的体积;(3)输出3个长方柱的体积。#include using namespace std;class Box public: void get_value(); float volume(); void display(); public: float

12、 lengh; float width; float height;void Box:get_value() coutlengh;/由键盘分别输入3个长方柱的长、宽和高。 cinwidth; cinheight;float Box:volume() return(lengh*width*height);/计算长方体体积。void Box:display() coutvolume()endl;int main() Box box1,box2,box3;/输出3个长方体的体积。 box1.get_value(); coutvolmue of bax1 is ; box1.display(); bo

13、x2.get_value(); coutvolmue of bax2 is ; box2.display(); box3.get_value(); coutvolmue of bax3 is ; box3.display(); return 0;第九章2. 分析下面的程序,写出其运行时的输出结果。#include using namespace std;class Date public: Date(int,int,int); Date(int,int); Date(int); Date(); void display(); private: int month; int day; int y

14、ear;Date:Date(int m,int d,int y):month(m),day(d),year(y) Date:Date(int m,int d):month(m),day(d) year=2005;Date:Date(int m):month(m) day=1; year=2005;Date:Date() month=1; day=1; year=2005;void Date:display() coutmonth/day/yearendl;int main() Date d1(10,13,2005); Date d2(12,30); Date d3(10); Date d4;

15、d1.display(); d2.display(); d3.display(); d4.display(); return 0;3. 如果将第2题程序的第5行改为用默认参数,即Date(int=1;int=1;int=2005);分析程序有无问题。上机编译,分析出错信息,修改程序使之能通过编译。要求保留上面一行给出的构造函数,同时能输出与第2题程序相同的输出结果。改:#include using namespace std;class Date public: Date(int=1,int=1,int=2005); void display(); private: int month; in

16、t day; int year;Date:Date(int m,int d,int y):month(m),day(d),year(y) void Date:display() coutmonth/day/yearendl;int main() Date d1(10,13,2005); Date d2(12,30); Date d3(10); Date d4; d1.display(); d2.display(); d3.display(); d4.display(); return 0;4. 建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5学生的数据

17、。#include using namespace std;class Student public: Student(int n,float s):num(n),score(s) void display(); private: int num; float score;void Student:display() coutnum=num score=scoreendl;int main() Student stud5= Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,

18、95.5) ; Student *p=stud; for(int i=0; idisplay(); return 0;5. 建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针做函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。#include using namespace std;class Student public: Student(int n,float s):num(n),score(s) int num; float score;int main() Student stud5= Student(101,78.5),Studen

19、t(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5) ; void max(Student* ); Student *p=&stud0; max(p); return 0;void max(Student *arr) float max_score=arr0.score; int k=0; for(int i=1; imax_score) max_score=arri.score; k=i; coutnum=arrk.num max_score=max_scoreendl;6. 阅读下面程序,分析其执行过程,写出

20、输出结果#include using namespace std;class Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) num=n; score=s; void display() coutnum scoredisplay();P-change(101,80.5);P-display(); return 0;其他部分仍同第6题程序。(4)在(2)的基础上将main函数第3行改为 const Student *p=&stud;(5)再把main函数第3行改为 Student

21、*const p=&stud;(1)Error passing const Student as this argument of void Student:display() discards qualifiers -fpermissiveError passing const Student as this argument of void Student:change(int, float) discards qualifiers -fpermissiveError passing const Student as this argument of void Student:displa

22、y() discards qualifiers -fpermissive(2)#include using namespace std;class Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) const num=n; score=s; void display() const coutnum scoreendl; private: mutable int num; mutable float score;int main() const Student stud(101,78

23、.5); stud.display(); stud.change(101,80.5); stud.display(); return 0;(3)#include using namespace std;class Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) num=n; score=s; void display() coutnum scoredisplay(); p-change(101,80.5); p-display(); return 0;(4)#include using namespace std;class Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) num=n; score=s; void display() const coutnum scoredisplay(); stud.change(101,80.5); p-display(); return 0;(5)#include using namespace std;class Student public: Student(int n,float s):num(n

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1