3. 下面程序用于输出最小值,有一处错误。 #include < iostream.h > class Test { int a, b; int getmin() {return (a<b? a:b);} public: int c; void setValue( int x1, int x2, int x3 ) { a =x1; b =x2; c =x3;} int GetMin(); }; int Test::GetMin() { int d = getmin (); return (d=d<c? d:c); } void main() { Test t1; t1.setValue(34,6,2); cout << t1.getmin () << endl; }
2. 完成下面类中成员函数的定义。 #include < iostream.h > template < class T > class f { private: T x, y, s; public: f(T a =0, T b =0) {x =a; y =b;} void sum() {s=x+y;} T gets (); }; template < class T > ______ { return s; } void main() {______a(1.5,3.8); a.sum(); cout << a.gets () << endl; }
T f<T>::gets() f<double>
3. 下面程序用于输出三角形的面积,请在下横线处填上正确的语句。 #include < iostream.h > #include < math.h > void area() { double a, b, c; cout << "Input a b c:" ; ______ if(a +b >c&&a +c > b&&c + b >a) { double 1 = ( a + b + c)/2; ______ cout << "The area is:" << s << endl; } else cout << "Error" << endl; } void main() {area();}
cin>>a>>b>>c; double s=sqrt(1*(1-a)*(1-b)*(1-c));
4. #include < iostream.h > class Samp { public: void Setij(int a, int b){i =a, j =b; } ~ Samp() { cout << "Destroying.. " << i << endl; } int GetMuti() { return i * j;} protected : int i; int j; }; int main() { Samp * p; p = new Samp ; if( ! p) { cout << " Allocation error\n" ; return 1; } for(int j =0; j <5; j ++ ) p[j]..Setij(j, j); for(int k =0; k <5; k ++) cout <<"Muti[" <<k <<" ] is:" <<p[k].______<<endl; ______ return 0; }
GetMuti() delete []p;
5. #include < iostream.h > class AA { public: AA(______) { A = i; B = j; cout << "Constructor\n"; } AA ( AA &obj) { A = obj.A; B =obj.B; cout << " Copy Constructor\n"; } ~ AA() { cout << " Destructor\n" ; } void show() { cout << "A = " << A << ", B = " << B << endl; } private: int A, B; }; void main() {AA a1 (2,3); AA a2(a1); a2.show(); ______=&a2; pa->show(); }
int i, int j AA *pa
五、程序分析题
1. 给出下面程序的输出结果。 #include < iostream.h > static int a = 3; void fun(); void main () { for(int i = 1; i <a; i ++ ) fun(); cout << endl; } void fun () { static int a = a; cout<<(a+ =2) <<' '; }
2 4
2. 给出下面程序的输出结果。 #include < iostream.h > class A { public: A() {cout<<"A构造函数\n"; fun(); } visual void fun() {cout<<"A::fun()函数\n";} }; class B:public A {public: B() {cout<<"B构造函数\n"; fun(); } void fun() {cout<<"B::fun() calle函数\n";} }; void main() {B d;}
A构造函数 A::fun()函数 B构造函数 B::fun()calle函数
六、程序设计题
1. 写一个程序,定义一个抽象类Shape,由它派生3个类:Square(正方形)、Trapezoid(梯形)和Triangle三角形。用虚函数分别计算几种图形面积、并求它们的和。要求用基类指针数组,使它的每一个元素指向一个派生类对象。 #include<iostream.h> class Shape {public: visual double area()const=0; };