C面向对象程序设计习题解答与上机指导(第二版)源程序

上传人:go****ng 文档编号:163892222 上传时间:2022-10-23 格式:DOC 页数:72 大小:250.01KB
收藏 版权申诉 举报 下载
C面向对象程序设计习题解答与上机指导(第二版)源程序_第1页
第1页 / 共72页
C面向对象程序设计习题解答与上机指导(第二版)源程序_第2页
第2页 / 共72页
C面向对象程序设计习题解答与上机指导(第二版)源程序_第3页
第3页 / 共72页
资源描述:

《C面向对象程序设计习题解答与上机指导(第二版)源程序》由会员分享,可在线阅读,更多相关《C面向对象程序设计习题解答与上机指导(第二版)源程序(72页珍藏版)》请在装配图网上搜索。

1、C+面向对象程序设计习题解答与上机指导(第2版)习题参考答案源代码 使用源程序的几点注意事项 (1) 由于源程序在复制、编辑、解压缩等过程中可能引起部分符号(主要是标点符号,如分号、冒号、逗号、引号)的字体、半全角等发生变化,在编译时可能被检出语法错误,只要使用“替换”功能,纠正后即能顺利运行。(2) 有的C+系统(如Visual C+6.0)没有完全实现C+标准,它所提供的不带后缀的.h的头文件不支持友元运算符重载函数,在Visual C+6.0中编译会出错,这时可采用带后缀的.h头文件。将程序中的#includeusing namespace std;修改成#include即可顺利运行。第

2、2章 C+基础【2.2】下面是一个C程序,改写它,使它采用C+风格的I/O语句。#include int main() int a,b,d,min; printf(Enter two numbers:); scanf(%d%d,&a,&b); min=ab? b:a; for (d=2; dmin; d+) if (a%d)=0)&(b%d)=0) break; if (d=min) printf(No common denominatorsn); return 0; printf(The lowest common denominator is %dn,d); return 0; 【解】#i

3、nclude using namespace std;int main() int a,b,d,min; couta;cinb; min=ab? b:a; for (d=2; dmin; d+) if (a%d)=0)&(b%d)=0) break; if (d=min) coutNo common denominatorsn; return 0; coutThe lowest common denominator is endld; return 0;【2.24】写出下列程序的运行结果。#include using namespace std;int i=15;int main() int

4、i; i=100; :i=i+1; cout:iendl; return 0;运行结果:101Please any key to continue。【2.25】写出下列程序的运行结果。#include using namespace std;void f(int &m,int n) int temp; temp=m; m=n; n=temp;int main() int a=5,b=10; f(a,b); couta bendl; return 0;结果:10 10Please any key to continue。【2.26】分析下面程序的输出结果。#include using names

5、pace std;int &f(int &i) i+=10; return i;int main() int k=0; int &m=f(k); coutkendl; m=20; coutkendl; return 0;运行结果:10 20Please any key to continue。【2.27】 编写一个C+风格的程序,用动态分配空间的方法计算Fibonacci数列的前20项并存储到动态分配的空间中。【解】实现本题功能的程序如下:#include using namespace std;int main() int *p=new int20; /动态分配20个整型内存空间 *p=1;

6、 *(p+1)=1; /对前面2个内存空间赋值1 cout*pt*(p+1)t; p=p+2; /p指向第3个内存空间 for (int i=3;i=20;i+) *p=*(p-1)+*(p-2); cout*pt; if (i%5=0) coutendl;p+; /p指向下一个内存空间;return 0;【2.28】 编写一个C+风格的程序,建立一个被称为sroot的函数,返回其参数的二次方根。重载函数sroot三次,让它返回整数、长整数与双精度数的二次方根(计算二次方根时,可以使用标准库函数sqrt)。【解】实现本题功能的程序如下:#include #include using names

7、pace std;double sroot(int i) return sqrt(i);double sroot(long l) return sqrt(l); double sroot(double d) return sqrt(d);int main() int i=12; long l=1234; double d=12.34;couti的二次方根是:sroot(i)endl;coutl的二次方根是:sroot(l)endl;coutd的二次方根是:sroot(d)endl;return 0;【2.29】 编写一个C+风格的程序,解决百钱问题:将一元人民币兑换成1、2、5分的硬币,有多少

8、种换法?【解】实现本题功能的程序如下:#include using namespace std;int main() int i,j,sum=0; for(i=0;i=20;i+) for (j=0;j=0) sum+; cout100-5*i-2*jtjtiendl; coutsum is sumendl; return 0;【2.30】编写一个C+风格的程序,输入两个整数,将它们按由小到大的顺序输出。要求使用变量的引用。【解】实现本题功能的程序如下:#include using namespace std;int main() void change(int &,int &);int a,

9、b;cinab;if(ab)change(a,b);couta bendl;return 0;void change(int &a1,int &b1) int temp;temp=a1;a1=b1;b1=temp;【2.31】编写C+风格的程序,用二分法求解f(x)=0的根。【解】实现本题功能的程序如下:#include #include using namespace std;inline float f(float x) return 2*x*x*x-4*x*x+3*x-6;int main() float left,right,middle,ym,yl,yr; coutplease tw

10、o number:leftright; yl=f(left); yr=f(right); do middle=(right+left)/2; ym=f(middle); if (yr*ym0) right=middle;yr=ym;else left=middle;yl=ym; while (fabs(ym)=1e-6); coutnRoot is :middle;return 0;第3章 类和对象(一)【3.18】写出下面程序的运行结果。#include using namespace std;class test public: test() ; test() ; private: int

11、 i;test:test() i = 25; for (int ctr=0; ctr10; ctr+) coutCounting at ctrn; test anObject;int main() return 0;【3.19】写出下面程序的运行结果。#include using namespace std;class Testprivate: int val;public: Test() coutdefault.endl; Test(int n) val=n; coutCon.endl; Test(const Test& t) val=t.val; coutCopy con.endl;int

12、 main() Test t1(6); Test t2=t1; Test t3; t3=t1; return 0;【3.20】指出下列程序中的错误,并说明为什么。#include using namespace std;class Student public: void printStu(); private: char name10; int age; float aver;int main() Student p1,p2,p3; p1.age =30; return 0;【3.21】指出下列程序中的错误,并说明为什么。#include using namespace std;class

13、Student int sno; int age; void printStu(); void setSno(int d);void printStu() coutnSno is sno, ; coutage is age.endl;void setSno(int s) sno=s;void setAge(int a) age=a;int main() Student lin; lin.setSno(20021); lin.setAge(20); lin.printStu();【3.22】指出下列程序中的错误,并说明为什么。#include using namespace std;class

14、Pointpublic:int x,y; private:Point() x=1; y=2;int main() Point cpoint; cpoint.x=2; return 0;【3.23】下面是一个计算器类的定义,请完成该类成员函数的实现。class counter public: counter(int number); void increment(); /给原值加1 void decrement(); /给原值减1 int getvalue(); /取得计数器值int print(); /显示计数 private:int value;【解】class counter public

15、: counter(int number); void increment(); /给原值加1 void decrement(); /给原值减1 int getvalue(); /取得计数器值int print(); /显示计数 private:int value;counter:counter(int number) value=number;void counter:increment() value+;void counter:decrement() value-;int counter:getvalue() return value;int counter:print() coutva

16、lue is valueendl; return 0;【3.24】根据注释语句的提示,实现类Date的成员函数。#include using namespace std;class Date public: void printDate(); /显示日期 void setDay(int d); /设置日的值 void setMonth(int m); /设置月的值 void setYear(int y); /设置年的值private: int day,month,year;int main() Date testDay; testDay.setDay(5); testDay.setMonth(

17、10); testDay.setYear(2003); testDay.printDate(); return 0;【解】void Date:printDate() coutnDate is day.; coutmonth.yearendl;void Date:setDay(int d) day=d;void Date:setMonth(int m) month=m;void Date:setYear(int y) year=y;【3.25】建立类cylinder,cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度。用类cylinder计算圆柱体的体积,并存储在一

18、个double变量中。在类cylinder中包含一个成员函数vol,用来显示每个cylinder对象的体积。 【解】实现本题功能的程序如下:#include using namespace std;class cylinderpublic: cylinder(double a,double b); void vol();private: double r,h; double volume;cylinder:cylinder(double a,double b) r=a; h=b; volume=3.141592*r*r*h;void cylinder:vol() coutvolume is:v

19、olumen;int main() cylinder x(2.2,8.09); x.vol();return 0;【3.26】构建一个类Stock,含字符数组stockcode及整型数据成员quan、双精度型数据成员price。构造函数含3个参数:字符数组na及q、p。当定义Stock的类对象时,将对象的第1个字符串参数赋给数据成员stockcode,第2和第3个参数分别赋给quan、price。未设置第2和第3个参数时,quan的值为1000,price的值为8.98。成员函数 print没有形参,需使用this指针,显示对象数据成员的内容。假设类Stoc第1个对象的三个参数分别为:6000

20、01, 3000和 5.67 ,第2个对象的第1个数据成员的值是600001,第2和3数据成员的值取默认值。要求编写程序分别显示这两个对象数据成员的值。【解】 实现本题功能的程序如下:#include using namespace std;const int SIZE=80;class Stockpublic: Stock() strcpy(stockcode, );Stock(char code, int q=1000, double p=8.98) strcpy(stockcode, code); quan=q; price= p; void print(void) coutstockc

21、ode; cout quan priceendl; private: char stockcodeSIZE; int quan; double price;int main() Stock st1(600001,3000,5.67); st1.print(); Stock st2(600002); st2.print(); return 0;第4章 类和对象(二)【4.12】以下程序的运行结果是( )。#include using namespace std;class B public: B() B(int i,int j) x=i; y=j; void printb() coutx,yen

22、dl;private:int x,y;class Apublic: A() A(int I,int j);void printa();private:B c;A:A(int i,int j):c(i,j) void A:printa() c.printb();int main() A a(7,8);a.printa();return 0;A) 8,9 B)7,8C) 5,6 D)9,10【4.13】以下程序的运行结果是( )。#include using namespace std;class Apublic: void set(int i,int j) x=i; y=j; int get_y

23、() return y;private:int x,y;class boxpublic:void set(int l,int w,int s,int p) length=l;width=w;label.set(s,p);int get_area() return length*width;private:int length,width;A label;int main() box b;b.set(4,6,1,20);coutb.get_area()endl;return 0;A) 24 B) 4C) 20 D) 6【4.14】以下程序的运行结果是( )。#include using name

24、space std;class Samplepublic: Sample( int i,int j) x=i; y=j;void disp() coutdisp1endl;void disp() const coutdisp2endl;private:int x,y;int main() const Sample a(1,2);a.disp(); return 0;A) disp1 B) disp2C) disp1 disp2 D) 程序编译出错【4.15】以下程序的运行结果是( )。#include using namespace std;class Rpublic: R(int r1,in

25、t r2) R1=r1; R2=r2;void print(); void print() const;private:int R1,R2;void R:print() coutR1,R2endl;void R:print() const coutR1,R2endl;int main() R a(6,8);const R b(56,88);b.print(); return 0;A) 6,8 B) 56,88C) 0,0 D) 8,6【4.16】指出下面程序中的错误,并说明原因。#include using namespace std;class Studentpublic: Student(

26、) +x; coutSno; static int get_x() return x; int get_Sno() return Sno; private: static int x; int Sno;int Student:x=0;int main() cout Student:get_x() Student existn; Student stu1; Student *pstu=new Student; cout Student:get_x() Student exist,y=get_Sno()n; cout Student:get_x() Student exist,y=get_Sno(

27、)n; return 0; 【4.17】指出下面程序中的错误,并说明原因。#include using namespace std;class CTestpublic: const int y2; CTest (int i1,int i2):y1(i1),y2(i2) y1=10; x=y1; int readme() const; /.private: int x; const int y1;int CTest:readme() const int i; i=x; x+; return x;int main() CTest c(2,8); int i=c.y2; c.y2=i; i=c.y1

28、; return 0;【解】#include using namespace std;class CTestpublic: const int y2;CTest (int i1,int i2):y1(i1),y2(i2) y1=10; / 错误, y1是用const定义的,不能修改 x=y1; int readme() const; /.private: int x; const int y1;int CTest:readme() const int i; i=x; x+; / 错误,函数定义用了const,表示该函数不能修改对象 return x;int main() CTest c(2,8

29、); int i=c.y2; c.y2=i; / 错误, y2是常量,不能修改 i=c.y1; / 错误,y1私有变量,不能直接存取 return 0;【4.18】指出下面程序中的错误,并说明原因。#include using namespace std;class CTest public:CTest () x=20; void use_friend(); private: int x; friend void friend_f(CTest fri);void friend_f(CTest fri) fri.x=55;void CTest:use_friend() CTest fri; th

30、is-friend_f(fri); :friend_f(fri);int main() CTest fri,fri1; fri.friend_f(fri); friend_f(fri1); return 0;【解】#include using namespace std;class CTestpublic: CTest() x=20; void use_friend();private: int x; friend void friend_f(CTest fri);void friend_f(CTest fri) fri.x=55;void CTest:use_friend() CTest f

31、ri;this-friend_f(fri); / 错误, 友元函数不是成员函数,/ 所以不能用this-调用友元函数 :friend_f(fri);int main() CTest fri,fri1;fri.friend_f(fri); / 错误友元函数不是成员函数,/ 所以不能用“对象.函数名”调用友元函数 friend_f(fri1); return 0;【4.19】指出下面程序中的错误,并说明原因。#include using namespace std;class CTestpublic: CTest () x=20; void use_this();private: int x;vo

32、id CTest:use_this() CTest y,*pointer; this=&y; *this.x=10; pointer=this; pointer=&y;int main() CTest y; this-x=235; return 0;【解】#include using namespace std;class CTestpublic: CTest () x=20; void use_this();private: int x;void CTest:use_this() CTest y,*pointer; this=&y; / 错误,不能对this直接赋值。 *this.x=10;

33、 / 错误, 按优先级原句的含义是*(this.x)=10,显然不对/ 正确的写法是(*this).x=10;或 this-x=10; pointer=this; pointer=&y;int main() CTest y; this-x=235; / 错误,this的引用不能在外部函数中,只能在内部函数中。 Return 0;【4.20】写出下面程序的运行结果。#include using namespace std;class toy public: toy(int q, int p) quan = q; price = p; int get_quan() return quan; int

34、 get_price() return price;private: int quan, price; ;int main() toy op32= toy(10,20),toy(30,48), toy(50,68),toy(70,80), toy(90,16),toy(11,120),; for (int i=0;i3;i+) coutopi0.get_quan(),; coutopi0.get_price()n; coutopi1.get_quan(),; coutopi1.get_price()n; coutendl; return 0;【4.21】写出下面程序的运行结果。#include

35、 using namespace std;class example public: example(int n) i=n; coutConstructingn ; example() cout Destructingn; int get_i() return i; private: int i;int sqr_it(example o) return o.get_i()* o.get_i();int main() example x(10); coutx.get_i()endl; coutsqr_it(x)endl; return 0;【4.22】写出下面程序的运行结果。#include u

36、sing namespace std;class aClass public:aClass() total+;aClass() total-;int gettotal() return total;private:static int total;int aClass:total=0;int main() aClass o1,o2,o3; couto1.gettotal() objects in existencen; aClass *p; p=new aClass; if (!p) coutAllocation errorn; return 1; couto1.gettotal(); cou

37、t objects in existence after allocationn; delete p; couto1.gettotal(); cout objects in existence after deletionn; return 0;【4.23】写出下面程序的运行结果。#include using namespace std;class test public: test() ; test() ; private: int i;test:test() i = 25; coutHeres the program output. n; coutLets generate some st

38、uff.n; for (int ctr=0; ctr10; ctr+) coutCounting at ctrn; test anObject;int main( ) return 0;【4.24】 构建一个类book,其中含有两个私有数据成员qu和price, 建立一个有5个元素的数组对象,将qu初始化为15,将price 初始化为qu的10倍。显示每个对象的qu*price。【解】实现本题功能的程序如下:#include using namespace std;class book public: book(int a, int b) qu= a;price= b; void show_m

39、oney() coutqu*pricen;private: int qu,price;int main() book ob5= book(1,10),book(2,20), book(3,30),book(4,40),book(5,50) ; int i; for(i=0; i5; i+) obi.show_money(); return 0;【4.25】 修改上题,通过对象指针访问对象数组,使程序以相反的顺序显示对象数组的qu*price。【解】实现本题功能的程序如下:#include using namespace std;class book public: book(int a, in

40、t b) qu= a;price= b; void show_money() coutqu*pricen;private: int qu,price;int main() book ob5= book(1,10),book(2,20), book(3,30),book(4,40), book(5,50) ; int i;book *p;p=&ob4; for(i=0; ishow_money();p-; return 0;【4.26】使用 C+ 的类建立一个简单的卖玩具的程序。类内必须具有玩具单价、售出数量以及每种玩具售出的总金额等数据,并为该类建立一些必要的函数,并在主程序中使用对象数组建立

41、若干个带有单价和售出数量的对象,显示每种玩具售出的总金额。【解】实现本题功能的程序如下:#include using namespace std;class toy public: toy() toy(int p,int c) Price=p; Count=c; void Input(int P, int C) ; void Compute() ; void Print() ;private: int Price; int Count; long Total;void toy:Input (int P, int C) Price=P; Count=C;void toy:Compute() To

42、tal=(long) Price*Count;void toy:Print() coutPrice=Price Count=Count Total=Total n; int main() toy te(2,100); / 测试构造函数 toy* ob; ob = new toy6; ob0.Input(25,130); ob1.Input(30,35); ob2.Input(15,20); ob3.Input(25,120); ob4.Input(45,10); ob5.Input(85,65); for (int i=0; i6; i+) obi.Compute(); for (i=0; i

43、6; i+) obi.Print(); delete ob; return 0;【4.27】 构建一个类Stock,含字符数组stockcode及整型数据成员quan、双精度型数据成员price。构造函数含3个参数:字符数组na及q、p。当定义Stock的类对象时,将对象的第1个字符串参数赋给数据成员stockcode,第2和第3个参数分别赋给quan、price。未设置第2和第3个参数时,quan的值为1000,price的值为8.98。成员函数 print()使用this指针,显示对象内容。 【解】实现本题功能的程序如下:#include using namespace std;const

44、 int SIZE=80;class Stockpublic: Stock() strcpy(stockcode, );Stock(char code, int q=1000, double p=8.98) strcpy(stockcode, code); quan=q; price= p; void print(void) coutstockcode; cout quan priceendl; private: char stockcodeSIZE; int quan; double price;int main() Stock st1(600001, 3000, 8.89); st1.pr

45、int(); Stock st2; char stockc=600002; st2=stockc; st2.print(); return 0;【4.28】 编写一个有关股票的程序,其中有两个类:一个是深圳类shen_stock,另一个是上海类shang_stock。类中有三项私有数据成员:普通股票个数general、ST股票个数st和PT股票个数pt,每一个类分别有自己的友元函数来计算并显示深圳或上海的股票总数(三项的和)。两个类还共用一个count(),用来计算深圳和上海总共有多少股票并输出。【解】实现本题功能的程序如下:#include using namespace std;class

46、 shen_stock; / 向前引用class shang_stock / 深圳类 public:shang_stock(int g,int s,int p); / 构造函数friend void shang_count(const shang_stock ss); / 计算上海的股票总数friend void count(const shang_stock ss,const shen_stock zs); / 计算上海和深圳的股票总数private:int general; / 普通股票个数 int st; / ST股票个数 int pt; / PT股票个数;shang_stock: shang_stock(int g,int s,int p) / 构造函数 general=g; st=s; pt=p;class shen_stock int general; / 普通股票个数 int s

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


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