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
第 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
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 double r; public: }; void main(){ } 3.编写一个点类,其中包括三个数据成员X,Y,Z分别表示这个点的三维坐标值;要求能计算两个点的和,差,积。请对上述操作符进行重载;并测试它。 #include 第 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 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 ?? 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页