四、完成程序题(本大题共5小题,每小题4分,共20分) 26.为使下面程序输出结果为: 1*2 3+4 请在横线处填上适当的字句,以使程序完整。 #include class A { private: int R1, R2; public: A(int r1, int r2) { R1=r1; R2=r2; } void print(); void print() const; }; void A::print() { cout<} void A::print() const { cout<} void main() { A a(1, 2); const A b(3, 4); a.print(); b.print(); } 27. 在下面横线处填上适当字句,完成类中成员函数的定义。 #include class A { private: int X,Y; public: A() { X=Y=0; } A(int xx,int yy) :X(xx),Y(yy) { } A(A &a) { ①__________ } int GetX() {return X;} int GetY() {return Y;} void SetXY(int x,int y) { X=x; Y=y; } }; int main() { A * Ptr=new A[2]; Ptr[0].SetXY(5,10); Ptr[1].SetXY(15,20); cout<<″Release Ptr ……″< ②__________; //释放动态分配内存 return 0; } 28.请在横线处填上适当的字句,以使程序完整。 #include #include ″math.h″ class Point { private: double X,Y; ①__________Line; public: Point(double x=0, double y=0) { X=x; Y=y; } Point(Point &p){ X=p.X; Y=p.Y; } }; class Line { private: Point p1,p2; public: Line(Point &xp1, Point &xp2): ②__________{} double GetLength(); }; double Line::GetLeng来源:91考试网th() { double dx=p2.X-p1.X; double dy=p2.Y-p1.Y; return sqrt(dx*dx + dy*dy); } void main() { Point p1,p2(3,4); Line L1(p1,p2); cout<} 29.下面程序横线处填上适当的字句,使类定义完整。 #include #include class ArrayFloat { protected: float *pA; int size; //数组大小(元素个数) public: ArrayFloat(int sz=10) { size=sz; pA=new float[size]; } ~ArrayFloat(void) { ①__________; //释放动态内存 } int GetSize(void) const { return size; } float& operator[](int i) //重载数组元素操作符″[]″ { return pA[i]; } void Print(); }; void ArrayFloat::Print() { int i; for(i=0; i< ②__________; i++) { if (i%10==0) cout << endl; cout<} cout<} void main() { ArrayFloat a(20); for (int i=0; i a[i]=(float)i* 2; a.Print(); } 30.在横线处填上适当字句,完成下面复数类的定义。 #include class Complex { private: double real, image; public: Complex(double r=0,double i=0) { real=r; image=i; } friend Complex operator+(Complex &a,const Complex &c); //复数加法运算符 Complex operator=(Complex c2); //复数赋值运算符 }; Complex operator+(Complex &a,const Complex &b) { Complex *t = new Complex (①__________); return *t; } Complex Complex:: operator = (Complex c2) { real= c2.real; image = c2.image; return ( ②__________); } 五、程序分析题(本大题共6小题,每小题5分,共30分) 31.写出下面程序的输出结果。 #include class B{ private: int Y; public: B(int y=0) { Y=y; cout<<″B(″<~B() { cout<<″~B()n″;} void print() { cout <} ; class D: public B{ private: int Z; public: D (int y, int z):B(y) { Z=z; cout<<″D(″< } ~D() { cout<<″~D()n″; } void print() { B∶∶print(); cout <} }; void main() { D d(11,22); d.print(); } 32.写出下面程序的输出结果。 #include class A { private: double X,Y; public: A(double xx=0, double yy=0) { X=xx; Y=yy; cout<<″构造函数被调用(″<} A(A &p) { X=p.X; Y=p.Y; } }; A f() { A a(1,2); return a; } void main() { A a(4,5); A b(a); b = f(); } 33.写出下面程序的输出结果。 #include class A { public: virtual void f() { cout<<″A::f()n″; } }; class B:public A { private: char *buf; public: B(int i) { buf=new char[i]; } void f() { cout<<″B::f()n″; } ~B() { delete []buf; } }; void main() { A *a=new A; a->f(); delete a; a=new B(15); a->f(); } 34.写出下面程序的输出结果。 #include void main() { int a[9]={1,2,3,4,5,6,7,8,9}; for(int i=0; i<9; i++) { cout << setw(4) << a[i]; if(i%3==2) cout<} } 35.写出下面程序的输出结果。 #include template void print(T a[],int n ) { for(int i=0; i{ cout<if (i%5==4) cout<} cout<} void main() { int a[]={1, 2, 3, 4, 5, 6, 7}; double b[4]={8, 9, 10, 11 }; print(a,sizeof(a)/sizeof(int)); print(b,4); } 36.写出下面程序的输出结果。 #include class A { private: static int n; int X; public: A(int x=0) { X=x; n++; } ~A() { n-- ; } static int GetNum(){ return n; } void print(); }; void A∶∶print() { cout << ″n=″ << n << ″, X=″ << X<< endl; } int A∶∶n = 0; void main() { A *p=new A(12); p->print(); A a(34); a.print(); delete p; cout << ″n=″ << A::GetNum() << endl; } |