《(完整版)C++面向对象程序设计(谭浩强)课后习题答案text-P67.pdf》由会员分享,可在线阅读,更多相关《(完整版)C++面向对象程序设计(谭浩强)课后习题答案text-P67.pdf(4页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、班级:班级:B11512B11512姓名:陈德桥姓名:陈德桥学号:学号:30301.以下是根据题意订正后的程序和运行结果:#include#includeusing namespace std;using namespace std;class Timeclass Timepublic:public:void set_time(void);void set_time(void);void show_time(void);void show_time(void);int hour;int hour;int minute;int minute;int sec;int sec;Time t;Time
2、 t;int main()int main()t.set_time();t.set_time();t.show_time();t.show_time();return 0;return 0;void Time:set_time(void)void Time:set_time(void)coutset_time:endl;coutset_time:t.hour;cint.hour;cint.minute;cint.minute;cint.sec;cint.sec;void Time:show_time(void)void Time:show_time(void)coutshow_time:end
3、l;coutshow_time:endl;coutt.hour:t.minute:t.secendl;coutt.hour:t.minute:t.secendl;2.以下是根据题目要求对例2.1 进行改写后的程序和运行结果:#include#includeusing namespace std;using namespace std;class Timeclass Time private:private:int hour;int hour;int minute;int minute;int second;int second;public:public:void settime(int h,
4、int m,int s)void settime(int h,int m,int s)tl.hour=h,tl.minute=m,tl.second=s;tl.hour=h,tl.minute=m,tl.second=s;void showtime()void showtime()coutshowtime:endl;coutshowtime:endl;couttl.hour:tl.minute:tl.secondendl;couttl.hour:tl.minute:tl.secondendl;tl;tl;int main()int main()tl.settime(22,24,0);tl.se
5、ttime(22,24,0);tl.showtime();tl.showtime();return 0;return 0;3.以下是根据题目要求对例2.1 进行改写后的程序和运行结果:#include#includeusing namespace std;using namespace std;class Timeclass Time private:private:int hour;int hour;int minute;int minute;int second;int second;public:public:void settime(int h,int m,int s);void se
6、ttime(int h,int m,int s);void showtime();void showtime();tl;tl;int main()int main()tl.settime(22,24,0);tl.settime(22,24,0);tl.showtime();tl.showtime();return 0;return 0;void Time:settime(int h,int m,int s)void Time:settime(int h,int m,int s)tl.hour=h,tl.minute=m,tl.second=s;tl.hour=h,tl.minute=m,tl.
7、second=s;void Time:showtime()void Time:showtime()coutshowtime:endl;coutshowtime:endl;couttl.hour:tl.minute:tl.secondendl;couttl.hour:tl.minute:tl.secondendl;4.以下根据要求对本章 2、3、3 进行完善后的程序和运行结果:#include#include#include#includeusing namespace std;using namespace std;class Studentclass Student public:publi
8、c:void display()void display()coutnum:numendl;coutnum:numendl;coutname:nameendl;coutname:nameendl;coutsex:sexendl;coutsex:sexendl;void set_value1(int n);void set_value1(int n);void set_value2(string m);void set_value2(string m);void set_value3(char s);void set_value3(char s);private:private:int num;
9、int num;string name;string name;char sex;char sex;Stu;Stu;int main()int main()Stu.set_value1(12);Stu.set_value1(12);Stu.set_value2(liu);Stu.set_value2(liu);Stu.set_value3(F);Stu.set_value3(F);Stu.display();Stu.display();return 0;return 0;void Student:set_value1(int n)void Student:set_value1(int n)Stu.num=n;Stu.num=n;void Student:set_value2(string m)void Student:set_value2(string m)Stu.name=m;Stu.name=m;void Student:set_value3(char s)void Student:set_value3(char s)Stu.sex=s;Stu.sex=s;