1、c面向对象程序设计课后习题答案谭浩强版第一章5:#include using namespace std;int main() coutThisis; coutaC+; coutprogram.endl; return 0;6:#include using namespace std;int main() int a,b,c; a=10; b=23; c=a+b; couta+b=; coutc; coutendl; return 0;7:#include using namespace std;int main() int a,b,c; int f(int x,int y,int z); c
2、inabc; c=f(a,b,c); coutcendl; return 0;int f(int x,int y,int z) int m;if (xy) m=x; else m=y; if (zm) m=z; return(m); 8: #include using namespace std;int main() int a,b,c; cinab; c=a+b; couta+b=a+bendl; return 0; 9: #include using namespace std;int main()int add(int x,int y); int a,b,c; cinab; c=add(
3、a,b); couta+b=cendl; return 0;int add(int x,int y)int c; c=x+y; return(c);10:#include using namespace std;int main()void sort(int x,int y,int z);int x,y,z; cinxyz; sort(x,y,z); return 0;void sort(int x, int y, int z) int temp; if (xy) temp=x;x=y;y=temp; / 内3个语句的作用是将x和y的值互换) if (zx) coutz,x,yendl; el
4、se if (zy) coutx,z,yendl; else coutx,y,zendl; 11:#include using namespace std;int main()int max(int a,int b,int c=0); int a,b,c; cinabc; coutmax(a,b,c)=max(a,b,c)endl; coutmax(a,b)=max(a,b)a) a=b; if(ca) a=c; return a;12:#include using namespace std;int main() void change(int ,int ); int a,b; cinab;
5、 if(ab) change(a,b); coutmax=a min=bendl; return 0;void change(int ,int ) int r1,r2,temp; temp=r1; r1=r2; r2=temp; 13:#include using namespace std;int main()void sort(int &,int &,int &); int a,b,c,a1,b1,c1; coutabc; a1=a;b1=b;c1=c; sort(a1,b1,c1); couta b c in sorted order is ; couta1 b1 c1j) change
6、(i,j); if (ik) change(i,k); if (jk) change(j,k);void change(int &x,int &y) int temp; temp=x; x=y; y=temp;14:#include #include using namespace std;int main() string s1=week,s2=end; couts1=s1endl; couts2=s2endl; s1=s1+s2; coutThe new string is:s1endl; return 0; 15:#include #include using namespace std
7、;int main() string str; int i,n; char temp; coutstr; n=str.size(); for(i=0;in/2;i+) temp=stri;stri=strn-i-1;strn-i-1=temp; coutstrendl;return 0; 16:#include #include using namespace std;int main() int i; string str5=BASIC,C,FORTRAN,C+,PASCAL; void sort(string ); sort(str); coutthe sorted strings :en
8、dl; for(i=0;i5;i+) coutstri ; coutendl; return 0;void sort(string s)int i,j; string t; for (j=0;j5;j+) for(i=0;isi+1) t=si;si=si+1;si+1=t; 17: #include #include using namespace std;int main() long c5=10100,-123567, 1198783,-165654, 3456; int a5=1,9,0,23,-45; float b5=2.4, 7.6, 5.5, 6.6, -2.3 ; void
9、sort(int );void sort(float ); void sort(long ); sort(a); sort(b); sort(c); return 0;void sort(int a)int i,j,t; for (j=0;j5;j+) for(i=0;iai+1) t=ai;ai=ai+1;ai+1=t; coutthe sorted numbers :endl; for(i=0;i5;i+) coutai ; coutendlendl; void sort(long a)int i,j; long t; for (j=0;j5;j+) for(i=0;iai+1) t=ai
10、;ai=ai+1;ai+1=t; coutthe sorted numbers :endl; for(i=0;i5;i+) coutai ; coutendlendl;void sort(float a)int i,j; float t; for (j=0;j5;j+) for(i=0;iai+1) t=ai;ai=ai+1;ai+1=t; coutthe sorted numbers :endl; for(i=0;i5;i+) coutai ; coutendlendl;18: #include #include using namespace std;template void sort(
11、T a) int i,j,min; T t; for(i=0;i5;i+) min=i; for (j=i+1;jaj) min=j; t=ai; ai=amin; amin=t; coutthe sorted numbers :endl; for(i=0;i5;i+) coutai ; coutendlendl;int main() int a5=1,9,0,23,-45; float b5=2.4, 7.6, 5.5, 6.6, -2.3 ; long c5=10100,-123567, 1198783,-165654, 3456; sort(a); sort(b); sort(c); r
12、eturn 0;第二章1#include using namespace std;class Time public: void set_time(); void show_time();private: /成员改为公用的 int hour; int minute; int sec; ;void Time:set_time() /在main函数之前定义 cinhour; cinminute; cinsec; void Time:show_time() /在main函数之前定义 couthour:minute:secendl; int main()Time t1;t1.set_time(); t
13、1.show_time(); return 0; 2:#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;int main() t.set_time(); t.show_time(); return 0; 3:#include using namespace std;cl
14、ass 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)couthour:minute:secendl;Time t;int main() t.set_time(); t.show_time(); return 0; 4:/xt2-4-1.cpp(main.cpp)#include using n
15、amespace std;#include xt2-4.hint main()Student stud; stud.set_value(); stud.display(); return 0;/xt2-4-2.cpp(即student.cpp)#include xt2-4.h /在此文件中进行函数的定义#include using namespace std; /不要漏写此行void Student:display( ) coutnum:numendl; coutname:nameendl; coutsex:sexnum;cinname; cinsex;5:/xt2-5-1.cpp(file1
16、.cpp)#include #include xt2-5.hint main() Array_max arrmax; arrmax.set_value(); arrmax.max_value(); arrmax.show_value(); return 0; /xt2-5-2.cpp(arraymax.cpp)#include using namespace std;#include xt2-5.hvoid Array_max:set_value() int i; for (i=0;iarrayi; void Array_max:max_value() int i; max=array0; f
17、or (i=1;imax) max=arrayi; void Array_max:show_value() coutmax=maxendl; 6:解法一#include using namespace std;class Boxpublic: void get_value(); float volume(); void display(); public: float lengh; float width; float height; ;void Box:get_value() coutlengh; cinwidth; cinheight;float Box:volume() return(l
18、engh*width*height);void Box:display() coutvolume()endl;int main()Box box1,box2,box3; box1.get_value(); coutvolmue of bax1 is ; box1.display(); box2.get_value(); coutvolmue of bax2 is ; box2.display(); box3.get_value(); coutvolmue of bax3 is ; box3.display(); return 0;解法二:#include using namespace std
19、;class Boxpublic: void get_value(); void volume(); void display(); public: float lengh; float width; float height; float vol; ;void Box:get_value() coutlengh; cinwidth; cinheight;void Box:volume() vol=lengh*width*height;void Box:display() coutvolendl;int main()Box box1,box2,box3; box1.get_value(); b
20、ox1.volume(); coutvolmue of bax1 is ; box1.display(); box2.get_value(); box2.volume(); coutvolmue of bax2 is ; box2.display(); box3.get_value(); box3.volume();coutvolmue of bax3 is ; box3.display(); return 0;第三章2:#include using namespace std;class Date public: Date(int,int,int); Date(int,int); Date(
21、int); Date(); void display(); private: int month; int day; int year; ;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
22、 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; 3:#include using namespace std;class Date public: Date(int=1,int=1,int=2005); void display(); private: int month; int day; int year; ;Date:Date(int m,int d,int y):mont
23、h(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: #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 scoreendl;int main()Student stud5= Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5); Student *p=stud; for(int i=0;idisplay(); return 0; 5:#include using namespace std
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1