c++(ii)练习题(1)

上传人:第*** 文档编号:34590788 上传时间:2018-02-26 格式:DOC 页数:6 大小:55KB
返回 下载 相关 举报
c++(ii)练习题(1)_第1页
第1页 / 共6页
c++(ii)练习题(1)_第2页
第2页 / 共6页
c++(ii)练习题(1)_第3页
第3页 / 共6页
c++(ii)练习题(1)_第4页
第4页 / 共6页
c++(ii)练习题(1)_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《c++(ii)练习题(1)》由会员分享,可在线阅读,更多相关《c++(ii)练习题(1)(6页珍藏版)》请在金锄头文库上搜索。

1、1一、给出一个有理数类,实现有理数的加法运算。请按后列要求作答。1仔细、完整地阅读程序,对应题号填写注释。注释的内容包括两部分:1.本行的语句是什么;2.简述其功能或特点。例如,注释可以这样写:“友员函数;重载加法运算符(+)。 ” (每小题 1 分,共 10 分)#include#include#includeclass Rational private:int numerator,denominator; /(例)数据成员;表示一个有理数的分子、分母。int maxcommonFactor(int,int); /(1) void simplify( ); /(2) void DtoF(do

2、uble,int /(3) public:Rational(int n,int d=1); /(4) Rational(double x=0); /(5) Rational(); /(6) void plus(const Rational /(7) Rational operator + (const Rational /(例)公有成员函数;重载加法运算符(+)。void showFraction( ); /(8) operator double( ); /(9) friend ostream &operator1e-15)d*=10;n=int(x*d);Rational:Rational(

3、int n,int d)if(d=0) cout=0 & denominator0)coutclass A private: double x;public:A(double px=1):x(px) double getx( ) const return x;void show( )coutclass A /长度 x,y public:double x,y;A(double px=1,double py=1):x(px),y(py) virtual A( ) void show( )coutshow( );p=p=p=写出上述程序的运行结果。(27) 在类 A 的 show 函数之前加上关键字

4、 virtual,再写出上述程序的运行结果。(28) 在(28)题基础上,于原 main 函数的末尾添加 (B2)c).show( ); 结果将多显示一行:(29) 把类 A 的 show 函数改写成纯虚函数。(30) 若类 A 的 show 函数改写成纯虚函数,原来的 main 函数不能正常运行,为什么?请简单说明理由。(31) 类 A 的 show 函数改写成纯虚函数后,请你对原来的 main 函数作简单的删改,使其可以输出派生类的数据。(32) 三、给出一些关于使用模板的程序段,按要求作答。1下列程序用函数模板实现两个数据的交换,请把有关的语句填充完整。(每小题 3 分,共 9 分)#i

5、ncludetemplate void swap(34) ) (35) void main() int j=1,k=3;cout; swap(j,k); cout; swap(x,y); cout; swap(p,q); cout6#include#includeusing (36) ;const int size=10;void display(vectorV,int n)int i;for(i=0;iV(a,a+size); / 用数组对模板向量赋初值cout#include #include void main( ) ofstream outstuf ; / 建立输出文件流对象outstuf.open(e:newfile.dat,(39) ); if(!outstuf)cerrerror!endl;abort( ); / 调用重载算符函数测试流outstufThis is a file of example.n; / 写入一行内容outstuf (40) ;

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

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号