C++面向对象程序设计模拟试题五
一、单项选择题(本大题共10小题,每小题2分,共20分)在每小题列出的四个备选项中,只有一个是苻合题目要求的。请将其代码填写在题后的括号内。错选,多选或未选均无分。
1.下列对类的构造函数和析构函数描述正确的是( A )。
A)构造函数可以重载,析构函数不能重载 B)构造函数不能重载,析构函数可以重载 C)构造函数可以重载,析构函数可以重载 D)构造函数不能重载,析构函数不能重载 2.在函数定义前加上关键字“inline”,表示该函数被定义为( B )。
A)重载函数 B)内联函数 C)成员函数 D)普通函数
3.下面有关重载函数的说明中, ( C )是正确的。
A)重载函数必须具有不同的返回值类型 B)重载函数形参个数必须不同
C)重载函数一般具有不同的形参列表 D)重载函数名可以不同
4.下列有关类与对象的说法中,( C ) 是不正确的。
A)对象是类的一个实列
B)任何一个对象只能属于一个具体的类 C)一个类只能有一个对象
D)类和对象的关糸和数椐类型与变量的关糸类似
5.已知: Print( )函数是一个类的常成员函数,它无返回值,下列表示中,正确的是 ( A )。
A)void Print( ) const; B)const void Print( ); C)void const Print( ); D)void Print(const)
6.假定Myclass为一个类,那么下列的函数说明中( D )为该类的析构函数。
A)void ~Myclass( ); B)~Myclass( int n); C)Myclass( ); D)~Myclass( )
7.下面类的定义中有( B ) 处错误。 C哪来的三个错误啊。 class myclass { int i=0; public: void myclass( ); ~myclass(value); };
A)1 B)2 C)3 D)4 8.说明虚函数的关键字是( B )。
A)inline B)virtual C)define D)static 9.cout是某个类的标准对象的引用,该类是( B吧 )。 A.
A)ostream B)istream C)stdout D)stdin
10.如果class类中的所有成员在定义时都没有使用关键字public、private或protected,则所有成员缺省定义为( C )。
A)public B)protected C)private D)static 二、填空题(本大题共5小题,每小题2分,共10分)不写解题过程, 将正确的答案写在每小题的空格内,错填或不填均无分。
1.重载运算苻“-” 的函数名为( operator )。 operator -
2.C++中类的用途有两种, 一种是类的实例化, 即生成类的对象, 另一种是通过 ( 继承? ),派生出新的类。嗯对了
3.派生类中的成员不能直接访问基类中的( 私有 )成员。 4.编译时多态性可以用( 重载 )函数实现。
5.使用new建立的动态对象在不用时应该用( delete )删除,以便释放所占用空间。
三、程序分析题(本大题共6小题,每小题5分,共30分)给出下面各程序的输出结果。 1.若有以下程序:
#include
class A { int a;
public: A(int aa = 0 ){ a = aa; } ~A( ) {cout << \ };
class B: public A { int b;
public: B(int aa = 0, int bb = 0): A(aa) { b = bb; } ~B() { cout << \ };
int main() { B x(5), y(6,7); return 0; }
上面程序的输出结果为:
2.若有以下程序:
#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 << \ };
int main() { Point *p = new Point; p->SetPoint(5, 12); p->DisPoint(); delete p; return 0;
} 上面程序的输出结果为:
3.若有以下程序:
#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;
} 上面程序的输出结果为:
4.若有以下程序:
#include
class Base {
public: void Fun() { cout << \ };
class Derived:public Base { public: void Fun() { cout << \};
int main() { Derived a; Base *p; p=&a; p->Fun(); a.Fun(); return 0; }
上面程序的输出结果为: 不对啊 应该是22啊
5.若有以下程序:
#include
template
int main() { double d; int i ; d = 99.99; i = 88; Fun(d,i); cout << \ return 0; }
上面程序的输出结果为:
6.阅读下面程序,写出输出结果。 #include
class Point {
public: Point (int a = 0, int b = 0): x(a), y(b) { } int GetX() const { return x; } int GetY() const { return y; } void SetX(int a) { x = a; } void SetY(int a) { y = a; }
private: int x; int y; };
int main() { Point p1; const Point p2(3, 4); cout << p1.GetX() << endl; p1.SetX(1);