int i;
for (i=0;________ ) sum(i); } 30、 class A {
int a,b; public:
A(int aa=0,int bb=0)_____________{} //分别用39和bb对应初始化a和b }; main() {
___________; //定义类A的对象x并用5初始化数据成员a,同时定义y并用x初始化
数//据成员a
___________; //定义p指针,使之指向对象x }
31、仔细阅读下列求两个点之间距离的程序,根据程序的输出结果在划线处填入正确语句。 #include
class point {
float x,y; public:
point(float a,float b) { x=a; y=b; } float distance(point &p) {
float dx=_________________;
float dy=_________________; return (float) sqrt (dx*dx+dy*dy); } };
void main()
{
point p1(2,3),p2 (32,43);
cout<<___________________< 32、两个复数只有当它们的实部和虚部分别相等时,才被认为它们相等。在空格处填入合适的内容,以完成下面的程序,使其重载运算符“= =”,用以比较两个复数的相等。请在主函数中输出比较的结果。 #include 16 double real,imag; public: complex(double r,double i) { real=r; imag=i; } bool operator= =(complex &); }; bool complex:: operator= =(complex &com) { return bool (_______________________________); } void main( ) { complex c1(12.3,32.5),c2(21.7,18.6); if( ___________ ) cout<<”true\\n”; else cout<<”false\\n”; } 33、在下面程序的横线处填上适当字句,使该程序执行结果为6。 #include int X; public: ________________________________ // 为X置值 _________________________________ // 取X值 }; void main() { base test; test.init(6); cout< 34、设计可流的复数类,即要求在复数类中重载输入运算符“>>”和输出运算符“<<”。 #include double real,imag; ostream& operator<<(ostream&,Complex&); friend istream& operator>>(istream&,Complex&); }; ostream& operator<<(ostream& output,Complex& c) { <<\return output;} 17 istream& operator>>(istream& input,Complex& c) {cout<<\ >>c.real>>c.imag; return ;} void main() {Complex c1,c2; >>c1>>c2; \\\\输入c1和c2 <<\输出c1和c2 } 35、 此程序的功能是:构造一个类Box,含有3个数据成员,分别表示盒子的3条边长,还有一个成员函数,用来计算盒子的体积,请给程序填空。 #include double line,width,height; double volume; : Box(double a, double b, double c); void vol( ); }; (double a, double b, double c) { line=a;width=b;height=c; ; } void Box::vol( ) { cout< { Box x(3.4, 4.5, 8.3),y(2.0, 4.0, 6.0); x.vol( ); y.vol( ); } 36、完成下面类的定义: #include Person(char *nam) { ;//为name 申请内存空间 ;//给name初始化 cout<<\} ~ Person ( ) { ;//释放内存空间 cout<<\ 18 } void show( ); private: char *name; }; void ::show( ) {cout< {Person student1(\ .show( );} 37、定义一个Point类,由它公有派生出矩形类Rectangle和圆类Circle,计算各派生类对象的面积。 #include double y; public: Point(double a,double b){x=a; ;} }; class Rectangle: {public: Rectangle (double a,double b):Point(a,b){ } void area(); }; void Rectangle ::area( ) { cout<<\Rectangle is:\ < Circle (double a,double b,double c): { r=c;} void area(); private: double r; }; void Circle ::area( ) { cout<<\ < { Rectangle r(5.5,6.0); Circle c(0,0,5.0); r.area( ); c.area( ); } 38、设计一个2行3列的矩阵类Matrix,重载流插入运算符 \和流提取符\,使之能用于该矩阵的输入和输出。 19 #include friend ostream& operator<<(ostream& output,matrix& m); friend istream& operator>>(istream& input,matrix& m); }; ostream& operator<<(ostream& output,matrix& m) { for(int i=0;i<2;i++) {for(int j=0;j<3;j++) < istream& operator>>(istream& input,matrix& m) { cout<<\输入6个数:\for(int i=0;i<2;i++) for(int j=0;j<3;j++) >>m.a[i][j]; return ; } void main() {matrix m; >>m; < 39、设计一个包括姓名、学号和成绩的学生类;先把1个学生数据写入文件aaa.dat中,再从文件中读出该学生数据并显示在屏幕上。 #include char name[20]; long number; int score; Student(char *na = “0”, long nu = 0, int sc = 0) 20