c面向对象程序设计课后习题答案(谭浩强版).pdf

上传人:索**** 文档编号:76431431 上传时间:2023-03-10 格式:PDF 页数:50 大小:47.68KB
返回 下载 相关 举报
c面向对象程序设计课后习题答案(谭浩强版).pdf_第1页
第1页 / 共50页
c面向对象程序设计课后习题答案(谭浩强版).pdf_第2页
第2页 / 共50页
点击查看更多>>
资源描述

《c面向对象程序设计课后习题答案(谭浩强版).pdf》由会员分享,可在线阅读,更多相关《c面向对象程序设计课后习题答案(谭浩强版).pdf(50页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、第一章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);cinabc;c=f(a,b,c);coutcendl;return

2、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(a,b);couta+b=cendl;return 0;int add(int x,int y)int c;c=x

3、+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;else if(zy)coutx,z,yendl;else coutx,y,zendl;11:#include using namespace st

4、d;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;if(ab)change(a,b);coutmax=a min=bendl;return 0;void change(int,int)int r1,r2,temp;temp=r1

5、;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(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#includ

6、e 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;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;retur

7、n 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:endl;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 u

8、sing 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 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:

9、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;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;i

10、5;i+)coutai;coutendlendl;18:#include#include using namespace std;template void sort(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,

11、-123567,1198783,-165654,3456;sort(a);sort(b);sort(c);return 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:minut

12、e:secendl;int main()Time t1;t1.set_time();t1.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:#inclu

13、de 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)couthour:minute:secendl;Time t;int main()t.set_time();t.show_time();return 0;4:/xt2-4-1.cpp(main.cpp)#includ

14、e using namespace std;#include xt2-4.h int 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(file

15、1.cpp)#include#include xt2-5.h int 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.h void Array_max:set_value()int i;for(i=0;iarrayi;void Array_max:max_value()int i;max=array0;for(i=1;imax

16、)max=arrayi;void Array_max:show_value()coutmax=maxendl;6:解法一#include using namespace std;class Box public: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(lengh*width*height);void Box:

17、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;class Box public:void get_value();void vo

18、lume();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();box1.volume();coutvolmue of bax1 is;box1.display();box2.get

19、_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(int);Date();void display();private:int month;int day;int year;Date:Date(in

20、t 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;d1.display();d2.display();d3.

21、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):month(m),day(d),year(y)void Date:display()coutmonth/day/yearendl;int main()Date d1(10,13,2005);Date d2(12,30);Dat

22、e 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

23、(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;class Student public:Student(int n,float s):num(n),score(s)int num;float score;void main()Student stud5=Student(101,78.5),Student(102,85.5),Student(103,98.5

24、),Student(104,100.0),Student(105,95.5);void max(Student*);Student*p=&stud0;max(p);void max(Student*arr)float max_score=arr0.score;int k=0;for(int i=1;imax_score)max_score=arri.score;k=i;coutarrk.num max_scoreendl;6:#include using namespace std;class Student public:Student(int n,float s):num(n),score

25、(s)void change(int n,float s)num=n;score=s;void display()coutnum scoreendl;private:int num;float score;int main()Student stud(101,78.5);stud.display();stud.change(101,80.5);stud.display();return 0;7:解法一#include using namespace std;class Student public:Student(int n,float s):num(n),score(s)void chang

26、e(int n,float s)num=n;score=s;void display()coutnum scoreendl;/可改为:void display()const coutnum scoreendl;private:int num;float score;int main()const Student stud(101,78.5);stud.display();/stud.change(101,80.5);stud.display();return 0;解法二:#include using namespace std;class Student public:Student(int

27、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.5);stud.display();stud.change(101,80.5);stud.display();return 0;解法三:#include using namespace std;class Student

28、 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;8:#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 di

29、splay()coutnum scoreendl;private:int num;float score;int main()Student stud(101,78.5);void fun(Student&);fun(stud);return 0;void fun(Student&stu)stu.display();stu.change(101,80.5);stu.display();9:#include using namespace std;class Product public:Product(int n,int q,float p):num(n),quantity(q),price(

30、p);void total();static float average();static void display();private:int num;int quantity;float price;static float discount;static float sum;static int n;void Product:total()float rate=1.0;if(quantity10)rate=0.98*rate;sum=sum+quantity*price*rate*(1-discount);n=n+quantity;void Product:display()coutsu

31、mendl;coutaverage()endl;float Product:average()return(sum/n);float Product:discount=0.05;float Product:sum=0;int Product:n=0;int main()Product Prod3=Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5);for(int i=0;i3;i+)Prodi.total();Product:display();return 0;10:#include using namespace

32、std;class Date;class Time public:Time(int,int,int);friend void display(const Date&,const Time&);private:int hour;int minute;int sec;Time:Time(int h,int m,int s)hour=h;minute=m;sec=s;class Date public:Date(int,int,int);friend void display(const Date&,const Time&);private:int month;int day;int year;Da

33、te:Date(int m,int d,int y)month=m;day=d;year=y;void display(const Date&d,const Time&t)coutd.month/d.day/d.yearendl;coutt.hour:t.minute:t.secendl;int main()Time t1(10,13,56);Date d1(12,25,2004);display(d1,t1);return 0;11:#include using namespace std;class Time;class Date public:Date(int,int,int);frie

34、nd Time;private:int month;int day;int year;Date:Date(int m,int d,int y):month(m),day(d),year(y)class Time public:Time(int,int,int);void display(const Date&);private:int hour;int minute;int sec;Time:Time(int h,int m,int s):hour(h),minute(m),sec(s)void Time:display(const Date&d)coutd.month/d.day/d.yea

35、rendl;couthour:minute:secendl;int main()Time t1(10,13,56);Date d1(12,25,2004);t1.display(d1);return 0;12:#include using namespace std;template class Compare public:Compare(numtype a,numtype b);numtype max();numtype min();private:numtype x,y;template Compare:Compare(numtype a,numtype b)x=a;y=b;templa

36、te numtype Compare:max()return(xy)?x:y;template numtype Compare:min()return(xy)?x:y;int main()Compare cmp1(3,7);coutcmp1.max()is the Maximum of two integer numbers.endl;coutcmp1.min()is the Minimum of two integer numbers.endlendl;Compare cmp2(45.78,93.6);coutcmp2.max()is the Maximum of two float num

37、bers.endl;coutcmp2.min()is the Minimum of two float numbers.endlendl;Compare cmp3(a,A);coutcmp3.max()is the Maximum of two characters.endl;coutcmp3.min()is the Minimum of two characters.endl;return 0;第四章1:#include using namespace std;class Complex public:Complex()real=0;imag=0;Complex(double r,doubl

38、e i)real=r;imag=i;double get_real();double get_imag();void display();private:double real;double imag;double Complex:get_real()return real;double Complex:get_imag()return imag;void Complex:display()cout(real,imagi)endl;Complex operator+(Complex&c1,Complex&c2)return Complex(c1.get_real()+c2.get_real()

39、,c1.get_imag()+c2.get_imag();int main()Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;coutc3=;c3.display();return 0;2:#include using namespace std;class Complex public:Complex()real=0;imag=0;Complex(double r,double i)real=r;imag=i;Complex operator+(Complex&c2);Complex operator-(Complex&c2);Complex operator*(

40、Complex&c2);Complex operator/(Complex&c2);void display();private:double real;double imag;Complex Complex:operator+(Complex&c2)Complex c;c.real=real+c2.real;c.imag=imag+c2.imag;return c;Complex Complex:operator-(Complex&c2)Complex c;c.real=real-c2.real;c.imag=imag-c2.imag;return c;Complex Complex:ope

41、rator*(Complex&c2)Complex c;c.real=real*c2.real-imag*c2.imag;c.imag=imag*c2.real+real*c2.imag;return c;Complex Complex:operator/(Complex&c2)Complex c;c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);return c;voi

42、d Complex:display()cout(real,imagi)endl;int main()Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;coutc1+c2=;c3.display();c3=c1-c2;coutc1-c2=;c3.display();c3=c1*c2;coutc1*c2=;c3.display();c3=c1/c2;coutc1/c2=;c3.display();return 0;3:#include /用 VC+时改为#include using namespace std;/用 VC+时为取消此行class Complex publi

43、c:Complex()real=0;imag=0;Complex(double r,double i)real=r;imag=i;Complex operator+(Complex&c2);Complex operator+(int&i);friend Complex operator+(int&,Complex&);void display();private:double real;double imag;Complex Complex:operator+(Complex&c)return Complex(real+c.real,imag+c.imag);Complex Complex:o

44、perator+(int&i)return Complex(real+i,imag);void Complex:display()cout(real,imagi)endl;Complex operator+(int&i,Complex&c)return Complex(i+c.real,c.imag);int main()Complex c1(3,4),c2(5,-10),c3;int i=5;c3=c1+c2;coutc1+c2=;c3.display();c3=i+c1;couti+c1=;c3.display();c3=c1+i;coutc1+i=;c3.display();return

45、 0;4:#include using namespace std;class Matrix/定义 Matrix 类public:Matrix();/默认构造函数friend Matrix operator+(Matrix&,Matrix&);/重载运算符“+”void input();/输入数据函数void display();/输出数据函数private:int mat23;Matrix:Matrix()/定义构造函数for(int i=0;i2;i+)for(int j=0;j3;j+)matij=0;Matrix operator+(Matrix&a,Matrix&b)/定义重载运算符

46、“+”函数Matrix c;for(int i=0;i2;i+)for(int j=0;j3;j+)c.matij=a.matij+b.matij;return c;void Matrix:input()/定义输入数据函数coutinput value of matrix:endl;for(int i=0;i2;i+)for(int j=0;jmatij;void Matrix:display()/定义输出数据函数for(int i=0;i2;i+)for(int j=0;j3;j+)coutmatij;coutendl;int main()Matrix a,b,c;a.input();b.i

47、nput();coutendlMatrix a:endl;a.display();coutendlMatrix b:endl;b.display();c=a+b;/用重载运算符“+”实现两个矩阵相加coutendlMatrix c=Matrix a+Matrix b:endl;c.display();return 0;5:#include /using namespace std;class Matrix public:Matrix();friend Matrix operator+(Matrix&,Matrix&);friend ostream&operator(istream&,Matri

48、x&);private:int mat23;Matrix:Matrix()for(int i=0;i2;i+)for(int j=0;j3;j+)matij=0;Matrix operator+(Matrix&a,Matrix&b)Matrix c;for(int i=0;i2;i+)for(int j=0;j(istream&in,Matrix&m)coutinput value of matrix:endl;for(int i=0;i2;i+)for(int j=0;jm.matij;return in;ostream&operator(ostream&out,Matrix&m)for(i

49、nt i=0;i2;i+)for(int j=0;j3;j+)outm.matij;outa;cinb;coutendlMatrix a:endlaendl;coutendlMatrix b:endlbendl;c=a+b;coutendlMatrix c=Matrix a+Matrix b:endlcendl;return 0;6:#include using namespace std;class Complex public:Complex()real=0;imag=0;Complex(double r)real=r;imag=0;Complex(double r,double i)re

50、al=r;imag=i;operator double()return real;void display();private:double real;double imag;void Complex:display()cout(real,imag)endl;int main()Complex c1(3,4),c2;double d1;d1=2.5+c1;coutd1=d1endl;c2=Complex(d1);coutc2=;c2.display();return 0;7:#include using namespace std;class Student public:Student(in

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 实施方案

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁