C++面向对象程序设计复习题2
一、单项选择题,在每小题列出的四个备选项中,
1.下列关于C++函数的说明中,正确的是( C )。
A)内联函数就是定义在另一个函数体内部的函数
B)函数体的最后一条语句必须是return语句
C)调用一个函数之前,如果还没有定义这个函数,必须先声明其原型
D)编译器会根据函数的返回值类型和参数表来区分函数的不同重载形式
2.假定MyCIass为一个类,那么下列的函数说明中,( C )为该类的无参构造函数。
A)void MyClass();B)~MyClass(int n); C)MyClass( ); D)~MyClass( );
3.下列叙述中,错误的是( C )。
A)派生类可以使用private派生
B)对基类成员的访问必须是无二义性的
C)基类成员的访问能力在派生类中维持不变
D)赋值兼容规则也适用于多继承的组合
4.当一个类的某个函数被说明为virtual时,该函数在该类及其所有派生类中( A ) 。
A. 都是虚函数 B. 只有被重新说明为vittual时才是虚函数
C. 都不是虚函数 D. 上面都不正确
5.派生类的构造函数的成员初始化表中,不能包含( C )。
A)基类的构造函数 B)派生类中子对象的初始化
C)基类中子对象的初始化 D)派生类中一般数据成员的初始化
6.下列是重载加法运算符的函数原型声明,其中错误的是( A A)MyClass operator+(double,double);
B)MyClass operator+(double,MyClass);
C)MyClass operator+(MyClass,double);
D)MyClass operator+(MyClass,MyClass);
7.派生类中的成员不能直接访问基类中的( B )成员。
)。
A)public B)private C)virtual D)protected
8.实现运行时的多态性要使用( D )。
A)重载函数 B)析构函数 C)构造函数 D)虚函数
9.如果在类MyClass的外面使用函数调用MyClass::f();则函数f()是类MyClass的( A )。
A. 静态成员函数 B. 非静态成员函数
C. 友元函数 D. 前面都不正确
10.由于常对象不能被更新,因此( A )。
A)通过常对象只能调用它的常成员函数
B)通过常对象只能调用静态成员函数
C)常对象的成员都是常成员
D)通过常对象可以调用任何不改变对象值的成员函数
11.下列对类的构造函数和析构函数描述正确的是( A )。
A)构造函数可以重载,析构函数不能重载
B)构造函数不能重载,析构函数可以重载
C)构造函数可以重载,析构函数可以重载
D)构造函数不能重载,析构函数不能重载
12.在函数定义前加上关键字“inline”,表示该函数被定义为(A)重载函数 B)内联函数
C)成员函数 D)普通函数
13.下面有关重载函数的说明中, ( C )是正确的。
A) 重载函数必须具有不同的返回值类型
B) 重载函数形参个数必须不同
C) 重载函数必须具有不同的形参列表
D) 重载函数名可以不同
14.下列有关类与对象的说法中,( C ) 是不正确的。
A) 对象是类的一个实列
)。 B
B) 任何一个对象只能属于一个具体的类
C) 一个类只能有一个对象
D) 类和对象的关糸和数椐类型与变量的关糸类似
15.已知: Print( )函数是一个类的常成员函数,它无返回值,下列表示中,正确的是 ( A )。
A)void Print( ) const; B) const void Print( );
C) void const Print( ); D) void Print(const)
16.假定Myclass为一个类,那么下列的函数说明中( D )为该类的析构函数。
A) void ~Myclass( ); B) ~Myclass( int n);
C) Myclass( ); D) ~Myclass( )
17.下面类的定义中有( C ) 处错误。
class myclass{int i=0; public: void myclass( );
~myclass(value); }
A) 1 B)2 C)3 D)4
18.说明虚函数的关键字是( B )。
A. inline B. virtual C. define D. static
19.cout是某个类的标准对象的引用,该类是( A )。
A. ostream B. istream C. stdout D. stdin
20.如果class类中的所有成员在定义时都没有使用关键字public、protected,则所有成员缺省定义为( C )。
A. public B. protected C. private D.static
21.定义类模板时要使用关键字( D )。
A. const B. new C. delete D. template
22.一个类的所有对象共享的是( D )。
A. 私有数据成员 B. 公有数据成员
C. 保护数据成员 D. 静态数据成员
23.静态成员函数没有( B )。
A. 返回值 B. this指针 C. 指针参数 D. 返回类型
或private
24.解决多重继承中二义性问题的方法有( C )。
A. 只能使用作用域分辨操作符
B. 使用作用域分辨操作符或赋值兼容规则
C. 使用作用域分辨操作符或虚基类
D. 使用虚基类或赋值兼容规则
25.如果在类CTest的外面函数调用CTest::f( );则函数f( )是类CTest的( A. 静态成员函数 B. 非静态成员函数
C. 友元函数 D. 前面都不正确
26.类的析构函数的作用是( D )。
A) 作为类的一般成员函数
B) 类的初始化
C) 对象的初始化
D) 对象的删除
A )。
27.一个类的友元函数或友元类可以访问该类的( D )。
A) 私有成员 B) 保护成员 C) 公有成员 D) 所有成员
28.下列关于成员函数特征的描述中,( A ) 是错误的。
A) 成员函数一定是内联函数 B) 成员函数可以重载
C) 成员函数可以设置参数的默认值 D) 成员函数可以是静态的
29.下列函数中,( C ) 不是类的成员函数。
A) 构造函数 B) 析构函数 C) 友元函数 D) 拷贝构造函数
30.下列对派生类的描述中,( D ) 是错误的。
A) 一个派生类可以作为另一个派生类的基类
B) 派生类至少有一个基类
C) 派生类的成员除了它自己的成员外, 还包含了它的基类的成员
D) 派生类中继承的基类成员的访问权限到派生类中保持不变
31.下列的描述中,( B ) 是错误的。
A) 公有继承时基类中的public成员在派生类中仍是public的
B) 公有继承时基类中的private成员在派生类中仍是private的
C) 公有继承时基类中的protected成员在派生类中仍是protected的
D) 私有继承时基类中的public成员在派生类中仍是private的
32.下列虚基类的声明中正确的是( D )。
A) class virtual B: public A B) virtual class B: public A
C) class B: public A virtual D) class B: virtual public A
33.若类A和类B的定义如下:
class A
{ int i, j;
public: A(int m, int n): i(m), j(n) {}
int Geti() { return i;} };
class B: public A
{int k;
public: B(int m, int n, int u): A(m, n), k(u) {}
void Make() { k = i * j; } };
int main()
{B b(1, 2, 3);
return 0;}
则上述定义中,( A ) 是非法的表达式.
A) k=i*j; B) int k; C) return i; 34.若有以下程序:
#include using namespace std; class A { public: D) void Make( ) int a; A() { a =10; } }; class A1: public A { public: A1() { a = a + 1; } }; class A2: public A { public: A2() { a = a + 2; } }; class B: public A1, public A2 { public: B() {} void Print() { cout << a << endl; } }; int main() {B obj; obj.Print(); return 0;} 则程序运行后的输出结果为:( A ) A) 产生语法错误 B) 13 C) 12 D)10 35.在下面的4个关键字中,( A ) 是用来说明虚函数的。 A) virtual B)public C)protected D)private 36.实现运行时的多态要使用( D )。 A) 重载函数 B) 析构函数 C) 构造函数 D) 虚函数 37.如果一个类至少有一个纯虚函数, 那么该类称为( A )。 A) 抽象类 B) 虚基类 C) 派生类 D) 以上都不对 38.cin是某个类的标准对象,该类是( B )。 A) ostream B) istream C) stdout D) stdin 39.调用一个成员函数时,使用动态联编的情况是( B )。 A) 通过对象调用一虚函数 B) 通过指针或引用调用一虚函数 C) 通过对象调用一静态函数 D) 通过指针或引用调用一静态函数 40.假定CTest为一个类,并且有一无默认值的有参构造函数和一无参构造函数,则执行“CTest objTest;”语句时将自动调用该类的( B )。 A) 有参构造函数 B) 无参构造函数 C) 拷贝构造函数 D) 赋值重载函数 二、填空题,不写解答过程,将正确的答案写在每小题的空格内。错填或不填均无分。 1.对于派生类的构造函数,在定义对象时构造函数的执行顺序为:先执行调用( 基类 )的构造函数,再执行调用子对象类的构造函数,最后执行派生类的构造函数体中的内容。 2.声明类模板应使用关键字( template )。 3.重载的关系运算符和逻辑运算符的返回类型应当是( bool或布尔型 )。 4.在面向对象方法中,类的实例称为( 对象 )。 5.在类的对象被释放时,( 析构函数 )函数会被自动调用。 6.重载运算苻”-” 的函数名为(oprator- )。 7.C++中类的用途有两种, 一种是类的实例化, 即生成类的对象, 另一种是通过 ( 继承),派生出新的类。 8.在下面程序的横线处填上正确的语句, 以实现动态多态。 #include class Base { public: virtual void Fun() { cout << \"Base::Fun\" << endl; } }; class Derived: public Base { public: void Fun() { cout << \"Derived::Fun\" << endl; } }; int main() {Base a, *pb; Derived b; pb=&b ; pb->Fun( ); //调派生类的成员函数Fun( ),以实现动态多态 return 0;} 9.编译时多态性可以用( 重载 )函数实现。 10.使用new建立的动态对象在不用时必须用( delete )删除,以便释放所占用空间。 11.类的具体表现是通过创建( 对象 )来操作的。 12.假定AB为一个类,则执行“AB a[10];”语句时,系统自动调用该类的构造函数的次数为( 10 ) 。 13.使用函数模板的方法是先说明函数模板,然后实例化成相应的( 模板函数 )进行调用 执行。 14.拷贝构造函数用它所在类的( 引用 )作为参数。 15.重载运算符“>>”的函数名为( operator >> )。 16.在用C++进行程序设计时,最好用( new )代替malloc。 17.函数模板中紧随template之后尖括号内的类型参数都要寇以保留字( class或typename )。 18.编译时多态性可以用( 重载 )函数实现。 19.拷贝构造函数用它所在类的( 对象 )作为参数。 20.用关键字static修饰的类的成员称为( 静态 )成员。 21.重载运算符“+”的函数名为( operator+ )。 22.设函数max是由函数模板实现的,并且max(3.5, 5)和max(3, 5)都是正确的函数调用,则此函数模板具有( 2 )个类型参数。 23.在C++中,函数重载与虚函数帮助实现了类的( 多态 )性。 24.由static修饰的数据成员为该类的所有对象( 共享 )。 25.重载函数在参数类型或参数个数上不同,但( 函数名 )相同。 26.使用new建立的动态对象在不用时必须用( delete )释放所占用的空间。 27.重载运算符“<<”的函数名为( operator<< )。 28.定义一个函数名为fun,返回值为int,没有参数的纯虚函数的声明是( int fun() = 0; )。 29.定义静态成员函数应使用关键字( static )。 30.在C++中,如果类的名称是CTest,则此类的构造函数名称为( CTest )。 31.在类的对象被创建时,( 构造函数 )函数会被自动调用。 32.类中数据成员和成员函数的默认访问级别为( private或私有 )。 三、完成程序填题,下面程序都留有空白,请将程序补充完整, 1.请完成下面的程序 #include using namespace std; // 使用标准命名空间std class Test // 测试静态成员类 {private: int a; // 数据成员 public: Test(int x = 0): [1] {} // 构造函数 void Show(){ cout << \"a:\" << a << endl; }};// 显示数据成员之值 int main(void) // 主函数main(void) {Test obj(168); // 定义对象 obj.Show(); // 显示数据成员之值 return 0; } // 返回值0,返回操作系统 参考答案:[1]a(x) 2.请完成下面的程序 #include using namespace std; // 使用标准命名空间std class Integer// 整数类 {private: int a; // 数据成员 public: Integer(int x = 0){ a = x; } // 构造函数 [2] () { return a; }};// 类型转换函数(将类类型转换为整型) int main(void) // 主函数main(void) {Integer a = 6; // 定义整数对象 cout << a << endl; // 显示整数 return 0; }// 返回值0, 返回操作系统 参考答案:[2]operator int 3.请完成下面的程序 #include using namespace std; // 使用标准命名空间std class Complex// 复数类 {private: double realPart; // 实部 double imagePart; // 虚部 public: Complex(double real = 0, double image = 0): realPart(real), imagePart(image){ } // 构造函数 double GetRealPart() const{ return realPart; } // 返回实部 double GetImagePart() const{ return imagePart; } // 返回虚部 Complex operator+(const Complex &a) const // 重载加法运算符+ {return Complex( [3] ); }};// 返回和 int main(void) // 主函数main(void) {Complex a(1, 2), b(2, 6), c; // 定义复数对象 c = a + b; // 复数加法运算 cout << \"a=\" << a.GetRealPart() << \"+\" << a.GetImagePart() << \"i\" << endl; // 显示a cout << \"b=\" << b.GetRealPart() << \"+\" << b.GetImagePart() << \"i\" << endl; // 显示b cout << \"c=\" << c.GetRealPart() << \"+\" << c.GetImagePart() << \"i\" << endl; // 显示c return 0; }// 返回值0, 返回操作系统 参考答案:[3] realPart + a.realPart, imagePart + a.imagePart或this->realPart + a.realPart, this->imagePart + a.imagePart 4.请完成下面的程序 #include using namespace std; // 使用标准命名空间std class Test// 测试静态成员类 {private: [4] int count; // 对象数 public: Test(){ count++; } // 构造函数, 实现对对象进行计数 static void Show(){ cout << \"共有\" << count << \"个对象!\" << endl; }};// 显示对象数 int Test::count = 0; // 初始化静态数据成员赋初值 int main(void) // 主函数main(void) {Test obj1, obj2, obj3; // 定义对象 Test::Show(); // 显示对象数 return 0; } // 返回值0,返回操作系统 参考答案:[4]static 5.请完成下面的程序 #include using namespace std; // 使用标准命名空间std class Integer// 整数类 {private: int a; // 数据成员 public: Integer(int x = 0){ a = x; } // 构造函数 operator int() { return a; } // 类型转换函数 Integer operator+(const Integer &i) const // 重载加法运算符+ {return [5] ;} };// 返回和 int main(void) // 主函数main(void) {Integer a(2), b(3), c; // 定义整数对象 c = a + b; // 求和 cout << c << endl; // 显示和 return 0; }// 返回值0, 返回操作系统 参考答案:[5] a + i.a或this->a + i.a 6.请完成下面的程序 #include using namespace std; // 使用标准命名空间std template class A {private: ElemType a; // 数据成员 public: A(ElemType x): a(x){ } // 构造函数 void Show() const { cout << a << endl; } };// 显示a之值 int main(void) // 主函数main(void) {A< [6] > obj(\"Hello, world!\"); // 定义对象 obj.Show(); // 显示相关信息 return 0; }// 返回值0, 返回操作系统 参考答案:[6] char * 四、程序分析题,给出下面各程序的输出结果。 1.阅读下面程序,写出输出结果。 #include using namespace std; // 使用标准命名空间std class A {public: virtual void Show() const { cout << \"Class A\" << endl; }}; class B: public A {public: void Show() const { cout << \"Class B\" << endl; }}; void Show(const A & obj) { obj.Show(); } int main(void) {A a; B b; Show(a); Show(b); A *p; p = &a; p->Show(); p = &b; p->Show(); B *q; q = &b; q->Show(); return 0; }// 返回值0,返回操作系统 上面程序的输出结果为: Class A Class B Class A Class B Class B 2.阅读下面程序,写出输出结果。 #include using namespace std; // 使用标准命名空间std template void Show(ElemType a[], int n) {for (int i = 0; i < n; i++) { cout << a[i] << \" \"; }} int main(void) {int a[] = {1, 6, 9}; Show(a, sizeof(a) / sizeof(int)); Show(a, 2); cout << endl; return 0; }// 返回值0, 返回操作系统 上面程序的输出结果为: 1 6 9 1 6 3.阅读下面程序,写出输出结果。 #include using namespace std; // 使用标准命名空间std class MyClass {public: MyClass(){ count++; } ~MyClass(){ count--; } static int GetCount(){ return count; } private: static int count;}; int MyClass::count = 0; // 初始化静态数据成员 int main(void) {MyClass obj1; cout << MyClass::GetCount() << endl; MyClass obj2; cout << MyClass::GetCount() << endl; MyClass obj3; cout << obj1.GetCount() << endl; MyClass *p = new MyClass; cout << MyClass::GetCount() << endl; delete p; cout << MyClass::GetCount() << endl; return 0; }// 返回值0, 返回操作系统 上面程序的输出结果为:1 2 3 4 3 4.阅读下面程序,写出输出结果。 #include using namespace std; // 使用标准命名空间std class A {public: A() { cout << \"A()\" << endl; } ~A() { cout << \"~A()\" << endl; } virtual void f() { cout << \"A::f()\" << endl; }}; class B: public A {public: B() { cout << \"B()\" << endl; } ~B() { cout << \"~B()\" << endl; } void f() { cout << \"B::f()\" << endl; }}; int main(void) {B obj; A *p = &obj; p->f(); return 0; }// 返回值0, 返回操作系统 上面程序的输出结果为: A() B() B::f() ~B() ~A() 5.若有以下程序: #include class A { int a; public: A(int aa = 0 ){ a = aa; } ~A( ) {cout << \"Destructor A! \" << a << endl; } }; class B: public A { int b; public: B(int aa = 0, int bb = 0): A(aa) { b = bb; } ~B() { cout << \"Destructor B! \" << b << endl; } }; int main() { B x(5), y(6,7); return 0;} 上面程序的输出结果为: Destructor B! 7 Destructor A! 6 Destructor B! 0 Destructor A! 5 6.若有以下程序: #include class Point { int x, y; public: Point() { x = 0; y = 0; } void SetPoint(int x1, int y1) { x = x1; y = y1; } void DisPoint() { cout << \"x=\" << x <<\ int main() {Point *p = new Point; p->SetPoint(5, 12); p->DisPoint(); delete p; return 0;} 上面程序的输出结果为: x=5,y=12 7.若有以下程序: #include class Sample { int n; public: Sample (int i) { n =i; } void Add() { s += n; } static int s; void Dis() { cout << s << endl; } }; int Sample::s = 0; int main() {Sample a(2), b(5), c(8); a.Add( ); b.Add( ); c.Dis( ); return 0;} 上面程序的输出结果为:7 8.若有以下程序: #include class Base {public: void Fun() { cout << \"1\" << endl;} }; class Derived:public Base { public: void Fun() { cout << \"2\" << endl; }}; int main() {Derived a; Base *p; p=&a; p->Fun(); a.Fun(); return 0;} 上面程序的输出结果为: 1 2 9.若有以下程序: #include template void Fun(T1 &x, T2 &y) {if (sizeof(T1) > sizeof(T2) ) x = (T1)y; else y = (T2)x;} int main() { double d; int i ; d = 99.99; i = 88; Fun(d,i); cout << \"d=\" << d << \"i=\" << i < 上面程序的输出结果为: d=88i=88 10.阅读下面程序,写出输出结果。 #include using namespace std; class Point { public: Point (int x = 0, int y = 0): m_x(x), m_y(y){ } int GetX() const { return m_x; } int GetY() const { return m_y; } void SetX(int x) { m_x = x; } void SetY(int y) { m_y = y; } private:int m_x; // X坐标 int m_y; }; // X坐标 int main(void) { Point oPoint1; const Point oPoint2(3, 4); cout << oPoint1.GetX() << endl; oPoint1.SetX(1); cout << oPoint1.GetX() << endl; oPoint1.SetY(2); cout << oPoint1.GetY() << endl; cout << oPoint2.GetX() << endl; cout << oPoint2.GetY() << endl; return 0;} 上面程序的输出结果为: 0 1 2 3 4 11. 阅读下面程序,写出输出结果。 #include class Sample { int i; public: Sample(); void Display( ); ~Sample();}; Sample::Sample() { cout << \"constructor\" << \ i=0;} void Sample::Display() { cout << \"i=\" << i << \ Sample::~Sample() { cout << \"destructor\" << endl; } int main() { Sample a; a.Display(); return 0;} 上面程序的输出结果为:constructor,i=0,destructor 12.阅读下面程序,写出输出结果。 #include using namespace std; class A {int a,b; public: A() { a = b = 0; } A(int aa, int bb) { a = aa; b = bb; cout << a << ' ' << b << endl;}}; int main() {A x, y(2,3); return 0;} 上面程序的输出结果为: 2 3 13.若有以下程序: #include class A { int a; public: A(int aa = 0 ) { a = aa; cout << \"A(): \" << a << endl;} }; class B: public A { int b; public: B(int aa = 0, int bb = 0): A(aa) { b = bb; cout << \"B(): \" << b << endl;} }; int main() { B x(5), y(6,7); return 0;} 输出结果为: A(): 5 B(): 0 A(): 6 B(): 7 14.若有以下程序: #include class Point { int x, y; public: Point(int m = 0, int n = 0) { x = m; y = n; } void Show() { cout << \"x=\" << x << \ int main( ) {Point *p = new Point(1, 68); p->Show(); delete p; return 0;} 输出结果为: x=1,y=68 15.若有以下程序: #include class Sample { int n; public: Sample(int i) { n = i; s += n; } static int s; void Show() { cout << s << endl; }}; int Sample::s = 0; int main() {Sample a(2), b(5), c(8); c.Show(); return 0;} 输出结果为: 15 16.若有以下程序: #include class Sample { int n; public: Sample(int i) { n = i; } void Print() { cout << \"1:n=\" << n << \ void Print( ) const { cout << \"2:n=\" << n << endl; } }; int main() {Sample a(10); const Sample b(20); a.Print(); b.Print(); return 0;} 输出结果为: 1:n=10,2:n=20 17.若有以下程序: #include class Test { int x; public: void Setx(int i) { x = i; } int Putx() { return x; } }; int main() {Test *p; Test a[3]; a[0].Setx(5); a[1].Setx(6); a[2].Setx(7); for( int j = 0; j < 3; j++) { p = &a[j]; cout << p->Putx() << \ cout << endl; return 0;} 输出结果为: 5,6,7, 18.若有以下程序: #include class Base {public: Base(int x) { a = x; } void Show() { cout << a << endl; } private: int a; }; class Derived: public Base {public: Derived (int i): Base(i + 1), b(i) {} void Show() { cout << b << endl; } private: int b; }; int main() { Base b(5), *pb; Derived d(1); pb = &d; pb->Show(); return 0;} 输出结果为: 2 19.若有以下程序: #include class Base {public: void Fun() { cout << \"1\" << endl; }}; class Derived: public Base {public: void Fun() { cout << \"2\" << endl; }}; int main() {Derived a; Base *p; p = &a; (*p).Fun(); a.Fun(); return 0;} 输出结果为: 1 2 20. 下列程序的输出结果为 #include template class FF { TT a1,a2,a3; public: FF(TT b1,TT b2, TT b3) { a1 = b1; a2 = b2; a3 = b3; } TT Sum() { return a1 + a2 + a3; }}; int main() {FF cout << x.Sum( ) <<' ' << y.Sum() << endl; return 0;} 输出结果为: 9 21 五、程序改错题,指出下面程序中的错误的行,说明错误原因,并加以改正。 1.下面程序中类的定义中有一处错误,请指出出错的行,说明错误原因,并加以改正。 #include using namespace std; // 使用标准命名空间std //2 //3 class Test //4 { //5 public: //6 void Test(){ cout << \"构造函数\" << endl; } //7 ~Test(){ cout << \"析造函数\" << endl; } //8 }; //9 //10 int main(void) //11 { //12 Test obj; //13 //14 return 0; // 返回值0,返回操作系统 //15 } //16 参考答案: 第7行“void Test(){ cout << \"构造函数\" << endl; }”有错,构造函数没有返回值类型,应改为,应改为“Test(){ cout << \"构造函数\" << endl; }”。 2.下面程序中有错误,请指出出错的行,说明错误原因,并加以改正。 #include using namespace std; //2 //3 class MyClass { public: MyClass(int x = 0): a(x){} //8 void Show() { cout << a << endl; } //11 private: int a; }; //15 //4 //5 //6 //7 //9 //10 //12 //13 //14 int main(void) //16 { //17 const MyClass oTest(18); //18 oTest.Show(); //19 //20 return 0; //21 } //22 参考答案: 第9行,第18行与第19行有错,常对象只能调用常成员函数,可将第18行的“const MyClass oTest(18);”改为“MyClass oTest(18);”;或将第9行的“void Show()”改为“void Show() const”。 3.下面程序中类的定义中有一处错误,请指出出错的行,说明错误原因,并加以改正。 #include using namespace std; //2 //3 class A { public: virtual void Show() = 0; }; //9 class B:public A { public: B(int a = 0): x(a){ } //14 void Show() //4 //5 //6 //7 //8 //10 //11 //12 //13 //15 { cout << x << endl; } //19 private: int x; }; //23 int main(void) { A obj(10); obj.Show(); //28 //16 //17 //18 //20 //21 //22 //24 //25 //26 //27 return 0; //29 } //30 参考答案: 第26行“A obj(10)”有错,Cbase是抽象类,不能直接生成对象,可改为“B obj(10)”。 4.下面程序中类的定义中有一处错误,请指出出错的行,说明错误原因,并加以改正。 #include using namespace std; //2 //3 class A //4 { //5 public: //6 A(int i = 0, int j): mi(i), mj(j) {} //7 void Show() { cout << mi << ' ' << mj << endl; } //8 private: int mi, mj; }; //12 int main() { A obj(12,16); obj.Show(); return 0; } 参考答案: 第7行应从右自左默认,应改为 A(int i,int j=0 ): mi( i ), mj( j ) { } 或改为 A(int i = 0,int j = 0): mi(i), mj(j) { } //9 //10 //11 //13 //14 //15 //16 //17 //18 5.下面程序中类的定义中有一处错误,请指出出错的行,说明错误原因,并加以改正。 #include class A { public: A(int a):m_a = a{} void Show() { cout << m_a << endl; } private: int m_a; }; //1 //2 //4 //5 //6 //7 //8 //9 //10 //11 //12 //13 int main() //14 { //15 A obj(8); //16 obj.Show(); //17 return 0; //18 } //19 参考答案: 第7行,改为A(int a): m_a(a) {} 6.下面程序中类的定义中有一处错误,请指出出错的行,说明错误原因,并加以改正。 #include using namespace std; //2 //3 class Test //4 { public: Test(int iVar = 0):m_iArr(iVar) { cout << \"构造函数:\" << m_iArr << endl; } void ~Test() { cout << \"析造函数:\" << m_iArr << endl; } //11 private: int m_iArr; }; //15 int main() { //5 //6 //7 //9 //12 //13 //14 //16 //17 //8 //10 Test obj1, obj2(8); //18 cout << endl; //19 return 0; //20 } 参考答案: 第9行,析构函数无返回值类型(void类型也不行),应改为~CTest(){…} 7. 要求用下面的程序实现动态多态性, 但程序有错, 请指出错误并改正之。 #include using namespace std; //2 //3 class AA //4 { //5 public: //6 void f() { cout << \"AA\"; } //7 }; //9 class CC: public AA { public: virtual void f() { cout << \"CC\"; } }; //15 int main() { AA aa, *p; CC cc; p = &cc; //8 //10 //11 //12 //13 //14 //16 //17 //18 //19 //20 p. f(); //21 return 0; //22 } //23 参考答案:笫7行基类中虚函数f() 前应加关键字virtual修饰,应改为: virtual void f() { cout << \"AA\"; } 笫21行p.f(); 应改当p->f(); 六、编程题(本大题26分) 1.编写程序,定义抽象基类Shape(形状),由它派生出3个派生类: Circle(圆形)、Rectangle(矩形)和Square (正方形),用函数函数ShowArea()分别显示各种图形的面积,最后还要显示所有图形的总面积。 #include using namespace std; // 使用标准命名空间std const double PI = 3.1415926; // 定义常量PI class Shape {public: Shape() { } // 构造函数 virtual ~Shape() { } // 析构函数 virtual ShowArea() = 0; // 显示面积 static double totalArea; // 总面积 static void ShowTotalArea() { cout << \"总面积:\" << totalArea << endl; }}; class Circle: public Shape {private: double radius; // 半径 public: Circle(double r): radius(r) { totalArea += PI * r * r; } // 构造函数 ~Circle() { } // 析构函数 virtual ShowArea() { cout << \"圆面积:\" << PI * radius * radius << endl; };};// 显示面积 class Rectangle: public Shape {private: double length; // 长 double width; // 宽 public: Rectangle(double l, double w): length(l), width(w){ totalArea += l * w; } // 构造函数 ~Rectangle() { } // 析构函数 virtual ShowArea() { cout << \"矩形面积:\" << length * width << endl; };};// 显示面积 class Square: public Shape {private: double length; // 边长 public: Square(double l): length(l){ totalArea += l * l; } // 构造函数 ~Square() { } // 析构函数 virtual ShowArea() { cout << \"正方形面积:\" << length * length << endl; };};// 显示面积 double Shape::totalArea = 0; // 初始化totalArea int main(void) {Circle c(1); // 定义圆对象 c.ShowArea(); // 显示面积 Rectangle r(1, 2); // 定义矩形对象 r.ShowArea(); // 显示面积 Square z(3); // 定义正方形对象 z.ShowArea(); // 显示面积 Shape::ShowTotalArea(); // 显示总面积 return 0; } // 返回值0, 返回操作系统 2.设计一个类DateInfo,要求其满足下述要求: (1)要求有一个无参的构造函数,其初始的年、月、日分别为:2010,6,8。 (2)要求有一个带参数的构造函数,其参数分别对应年、月、日。 (3)要求用一个成员函数实现日期的设置。 (4)要求用一个成员函数实现输出日期。 要求用一个成员函数实现日期的获取。 #include using namespace std; class DateInfo { private: int year, month, day; public: DateInfo(): year(2010), month(6), day(8){ } DateInfo(int y, int m, int d): year(y), month(m), day(d){ } void Set(int y, int m, int d) { year = y; month = m; day = d;} void Show() { cout << year << \"年\" << month << \"月\" << day << \"日\" << endl; }}; int main() { DateInfo d1, d2(1988, 8, 18); d1.Show(); d2.Show(); d2.Set(1999, 9, 19); d2.Show(); return 0;} 3.定义一个复数类Complex, 二个数据成员为double型r, i 为private属性。定义代二个参数的构造函数和一个Show( ) 函数用以输出r, i的值, 另外作为成员函数重载的运算苻”+”的功能是将此类二个对象的数据成员r和i对应相加。这些成员函数的属性均 为public. 请用C++编写此程序, 并编写测试程序进行测试。 #include using namespace std; class Complex { private: double r, i; public: Complex(double a, double b): r(a), i(b) {} void Show() { cout << r << \" \" << i << endl; } Complex operator +(Complex obj) { return Complex(r + obj.r, i + obj.i); } }; int main() { Complex c1(3.5, 4.5), c2(2.5, 5.5), c3(0.0, 0.0); c3 = c1 + c2; c3.Show(); return 0;} 4.设计一个类Rect,要求如下: (1)该类中的私有数据成员length,width存放它的长和宽,并且设置它们的默认值是0。 (2)通过成员函数设置其长和宽,并确保长和宽都在(0,50)范围之内。 (3)实现求周长函数GetPerimeter()。 #include using namespace std; class Rect { private: double length, width; public: Rect(double l = 0, double w = 0): length(l), width(w){ } void Set(double l, double w) { if (length <= 0 || length >= 50 || width <= 0 || width >= 50 ) throw \"数据不在指定范围(0,50)!\"; // 抛出异常 length = l; width = w;} double GetPerimeter() { return 2 * (length + width); }}; int main() { try // 检查异常 {Rect obj(1, 8); cout << \"周长:\" << obj.GetPerimeter() << endl;} catch (char *str) // 捕捉异常 { // 处理异常 cout << \"异常信息:\" << str << endl; } // 输出异常信息 return 0;} 5.定义一个二维座标类Vector2d, 二个数据成员为double型x, y 为private属性。定义代二个参数的构造函数和一个Show( ) 函数用以输出x, y的值, 另外作为成员函数重载的运算苻”+”的功能是将此类二个对象的数据成员x和y对应相加。这些成员函数的属性均为public. 请用C++编写此程序, 并编写测试程序进行测试 #include using namespace std; class Vector2d { double x, y; public: Vector2d(double a, double b): x(a), y(b) {} void Show() { cout << x << \" \" << y << endl; } Vector2d operator+(Vector2d &obj); }; Vector2d Vector2d::operator+(Vector2d &obj) { return Vector2d(x + obj.x, y + obj.y); } int main() { Vector2d d1(3.5, 4.5), d2(2.5, 5.5), d3(0.0, 0.0); d3 = d1 + d2; d3.Show(); return 0;} 因篇幅问题不能全部显示,请点此查看更多更全内容