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