77范文网 - 专业文章范例文档资料分享平台

C++复习题(2)

来源:网络收集 时间:2019-04-23 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

cout<<”调用函数abs(int).”; return x<0?-x:x; }

double abs(double x) {

cout<<”调用函数abs(double).”; return x<0?-x:x; }

void main( ) {

cout<<”-20的绝对值是:”<

6写出下面程序的运行结果: #include class my { int a,b; static int s; public:

第 6页共 15页

my(int x,int y) {a=x;b=y;s++;} void print() {cout<

int my::s=0; void main() {

my m1(1,2),m2(4,5),m3(6,7); m3.print(); m2.print(); m1.print(); }

三.程序设计题:

1.声明一个时钟类clock ,有数据成员h,m,s分别表示时钟的小时、分钟、秒钟,成员函数setclock设置时钟的小时、分钟、秒钟的值,display( )显示时钟,构造函数为时钟初始化,拷贝构造函数使新对象可以通过老对象创建,构造二个clock的对象对clock类进行测试。 #include using namespace std; class Clock{

int h,m,s;

public:

Clock(int shi,int fen,int miao):h(shi),m(fen),s(miao){} Clock(const Clock& c){ }

h=c.h;m=c.m;s=c.s;

第 7页共 15页

};

void display(){ }

cout<

void main(){

Clock c1(9,9,9); Clock c2(c1); cout<<\c1.display(); cout<<\c2.display(); }

2.构造一个圆类,要求有半径,并能求面积,测试它。 #include using namespace std; class Circle{

double r;

public: };

void main(){ }

3.编写一个点类,其中包括三个数据成员X,Y,Z分别表示这个点的三维坐标值;要求能计算两个点的和,差,积。请对上述操作符进行重载;并测试它。 #include class Point{

第 8页共 15页

double area(double r1){return 3.14*r1*r1;} void set(double r1){r=r1;}

Circle c; c.set(5);

cout<<\

int x, y,z; public:

void set(int a, int b,int c){ x=a;y=b;z=c; }

void print()const{ cout<<\ friend Point operator+(const Point& a, const Point& b); friend Point operator-(const Point& a, const Point& b); friend Point operator*(const Point& a, const Point& b); };

Point operator+(const Point& a, const Point& b){ Point s;

s.set(a.x+b.x, a.y+b.y,a.z+b.z); return s; }

Point operator-(const Point& a, const Point& b){ Point s;

s.set(a.x-b.x, a.y-b.y,a.z-b.z); return s; }

Point operator*(const Point& a, const Point& b){ Point s;

s.set(a.x*b.x, a.y*b.y,a.z*b.z); return s; }

void main(){ Point a, b; a.set(9,9,9); b.set(6,6,6);

operator+(a,b).print(); operator-(a,b).print();

第 9页共 15页

operator*(a,b).print(); }

四.程序改错题:

1.请找出并改正下面程序中的错误. #include class A{ public:

void A(int i=0){m=i;} void show(){cout<

void main() {

A a(5); a.m+=10;

a.show(); }

2.一个源程序文件main.cpp运行有问题,请改正main函数中的错误,使程序的输出结果为:

member=0 ??member=5 ??menber=10

??源程序文件main.cpp清单如下: ?#include ?Class My{ ??public:

?? My(int i){member=i;}

?? void SetMember(int m){member=m;} ?? int GetMember()const{return member;}

?? void print()const{cout<<\??private: ?? int member; ??};

第 10页共 15页

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库C++复习题(2)在线全文阅读。

C++复习题(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/621973.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: