C++期末复习题库 下载本文

void main( ){ f a; a.f1(1,3); a.get ( );

} private:int x=0,y=0; 数据成员声明时不允许直接赋值 44.#include class point{private:float x; public:void f(float a){x=a;} void f( ){x=0;}

friend float max(point& a,point& b); };

float max(point& a,point& b) {return(a.x>b.x)? a.x:b.x;} void main( ){ point a,b; a.f(2.2);b.f(3.3); cout<

} cout<

45.#include template class f{

private:T x,y;

public:void f1(T a,T b){x=a;y=b;} T max( ){retum(x>y)?x:y;} };

void main( ){ f a;

a.f1(1.5,3.8);

cout<

} cout<

四、完成程序题(本大题共5小题,每小题4分,共20分) 46.完成下面类中的成员函数的定义。 class point { private: int m,n; public: point(int,int); point(point&); };

point::point(int a,int b) {

m=a; ______ n __=b; }

point::point(____point&t ____) { m=t.m; n=t.n; }

47.下面是一个输入半径,输出其面积和周长的C++程序,在下划线处填上正确的语句。 #include using namespace std;

____ const double ____pi=3.14159; void main( ) {

double r; cout<<″r=″; ____ cin>>r _______ ; double l=2.0*pi*r; double s=pi*r*r;

cout<<″\n The long is:″<

}

48.在下划线处填上缺少的部分。 #include #include using namespace std; class complex { public: int real; int imag;

complex(int r=0,int i=0) {

real=r; imag=i; } };

complex operator+(___ complex&a _____, {

int r=a.real+b.real; int i=a.imag+b.imag;

return____ complex(r,i) _____; }

complex& b)