1、C+面向对象程序设计模拟试题6C+面向对象程序设计模拟试题六一、单项选择题(本大题共10小题,每小题2分,共20分)在每小题列出的四个备选项中,只有一个是苻合题目要求的。请将其代码填写在题后的括号内。错选,多选或未选均无分。1类的析构函数( d )。 A)作为类的一般成员函数 B)类初始化时被调用C)对象初始化时被调用 D)对象被删除时被调用*2一个类的友元函数或友元类可以访问该类的( d )。A)私有成员 B)保护成员 C)公有成员 D)所有成员*3下列关于成员函数特征的描述中,( a ) 是错误的。A)成员函数一定是内联函数 B)成员函数可以重载C)成员函数可以设置参数的默认值 D)成员函
2、数可以是静态的*4下列函数中,( c ) 不是类的成员函数。A)构造函数 B)析构函数 C)友元函数 D)拷贝构造函数 *5下列对派生类的描述中,( b ) 是错误的。A)一个派生类可以作为另一个派生类的基类B)派生类至少有一个基类C)派生类的成员除了它自己的成员外, 还包含了它的基类的成员D)派生类中继承的基类成员的访问权限到派生类中保持不变 6下列的描述中,( d ) 是错误的。A)公有继承时基类中的public成员在派生类中仍是public的 B)公有继承时基类中的private成员在派生类中仍是private的 C)公有继承时基类中的protected成员在派生类中仍是protecte
3、d的 D)私有继承时基类中的public成员在派生类中仍是private的 7下列虚基类的声明中正确的是( a )。 A)class virtual B: public A B)virtual class B: public A C)class B: public A virtual D)class B: virtual public A 8若类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
4、, 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; D)void Make( ) 9若有以下程序:#include using namespace std;class A public: int a; A() a =10; ; class A1: public A public: A1() a = a + 1; ; class A2: public A public:
5、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;则程序编译或运行后的输出结果为:aA)提示语法错误 B)13 C)12 D)1010在下面的4个关键字中,( a ) 是用来说明虚函数的。A)virtual B)public C)protected D)private 二、填空题(本大题共5小题,每小题2分,共10分)将正确的答案写在每小题的空格内,错填或不填均无分。1通过类创建( 对象 )来
6、要调用构造函数。2假定AB为一个类,则执行“AB a10;”语句时,系统自动调用该类的构造函数的次数为( 10 )。3使用函数模板的方法是先说明函数模板,然后实例化成相应的( 模板函数 )进行调用执行。4拷贝构造函数用它所在类的( 引用 )作为参数。5重载运算符“”的函数名为( operator )。三、程序分析题(本大题共6小题,每小题5分,共30分)给出下面各程序的输出结果。1若有以下程序:#include using namespace std;class A int a; public: A(int aa = 0 ) a = aa; cout A(): a endl; ;class B
7、: 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;输出结果为:2若有以下程序:#include using namespace std;class Point private: int x, y;public: Point(int m = 0, int n = 0) x = m; y = n; void Show() const cout x= x , y= y Show(); delete p; ret
8、urn 0;输出结果为:3若有以下程序:#include using namespace std;class Sample private: int n;public: Sample(int i) n = i; s += n; static int s; void Show() const cout s endl; ;int Sample:s = 0;int main() Sample a(2), b(5), c(8); c.Show(); return 0;输出结果为: 4若有以下程序:#include using namespace std;class Sample int n;publi
9、c: 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;输出结果为:5若有以下程序:#include using namespace std;class Test int x; public: void Setx(int i) x = i; int Putx() return x; ;int main() Test *
10、p; Test a3; a0.Setx(5); a1.Setx(6); a2.Setx(7); for( int j = 0; j 3; j+) p = &aj; cout Putx() ,; cout endl; return 0;输出结果为: 6若有以下程序:#include using namespace std;class Basepublic: Base(int x) a = x; void Show() cout a endl; private: int a; ;class Derived: public Basepublic: Derived (int i): Base(i +
11、1), b(i) void Show() cout b Show(); return 0;输出结果为: 四、完成程序填题(本大题共4个小题,每小题3分,共12分)下面程序都留有空白,请将程序补充完整。1将如下程序补充完整。#include using namespace std;class Aprivate: static int n; public: A() n+; static void Show() cout 共有 n 个对象! endl; ; 1 n = 0; / 为静态数据成员赋初值int main() A obj1, obj2, obj3; A:Show(); return 0;
12、2下列程序的输出结果为“2”,试将程序补充完整。#include using namespace std; class Apublic: 2 Show() const cout 1 endl; ;class B: public Apublic: void Show() const cout 2 Show(); delete p; return 0; 3将如下程序补充完整。#include using namespace std; class Baseprivate: int m; public: Base(int a): m(a) virtual void Show() const cout
13、m endl; ;class Derived: public Baseprivate: int n; public: Derived(int a, int b): Base(a), n(a) void Show() const cout n Show(); return 0; 4将如下程序补充完整。#include using namespace std; class Intprivate: int i; public: Int(int x = 0): i(x) () return i; / 类类型转换函数, 将类Int转换为基本类型int;int main() Int a; cout a e
14、ndl; return 0; 五、编程题(本大题共2小题,第1小题12分,第2小题16分,共28分)1设计一个类Rect,要求如下:(1)该类中的私有数据成员length,width存放它的长和宽,并且设置它们的默认值是0。(2)通过成员函数设置其长和宽,并确保长和宽都在(0,50)范围之内。 (3)实现求周长函数GetPerimeter()。2定义一个二维座标类Vector2d, 二个数据成员为double型x, y 为private属性。定义代二个参数的构造函数和一个Show( ) 函数用以输出x, y的值, 另外作为成员函数重载的运算苻”+”的功能是将此类二个对象的数据成员x和y对应相加
15、。这些成员函数的属性均为public. 请用C+编写此程序, 并编写测试程序进行测试。C+面向对象程序设计模拟试题六参考答案一、单项选择题(本大题共10小题,每小题2分,共20分)在每小题列出的四个备选项中,只有一个是苻合题目要求的。请将其代码填写在题后的括号内。错选,多选或未选均无分。1D) 2D) 3A) 4C) 5D)6B) 7D) 8A) 9A) 10A).二、填空题(本大题共5小题,每小题2分,共10分)不写解题过程, 将正确的答案写在每小题的空格内,错填或不填均无分1参考答案:对象2参考答案:103参考答案:模板函数4参考答案:引用5参考答案:operator 三、程序分析题(本大
16、题共6小题,每小题5分,共30分)给出下面各程序的输出结果。1参考答案:A(): 5B(): 0A(): 6B(): 72参考答案:x=1, y=683参考答案:154参考答案:1:n=10,2:n=205参考答案:5,6,76参考答案:2四、完成程序填题(本大题共4个小题,每小题3分,共12分)下面程序都留有空白,请将程序补充完整。1参考答案:1 int A:2参考答案:2 virtual void3参考答案:3 Base:4参考答案:4 operator int五、编程题(本大题共2小题,第1小题12分,第2小题16分,共28分)1参考程序:#include using namespace
17、 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 = 50 | width = 50 ) throw 数据不在指定范围(0,50)!; length = l; width = w; double GetPerimeter() return 2 * (length + width); ;int main() try Rect obj(1, 8); co
18、ut 周长: obj.GetPerimeter() endl; catch (char *str) cout 异常信息: str endl; return 0;2参考程序:#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;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1