C++面向对象程序设计模拟试题1

举报
资源描述
C++面向对象程序设计模拟试题一一、单项选择题(本大题共 10 小题,每小题 2 分,共 20 分)在每小题列出的四个备选项中,只有一个是符合题目要求的,请将其代码填写在题后的括号内。错选、多选或未选均无分。1.说明虚函数的关键字是( ) 。A)inline B)virtual C)define D)static2.在标准 C++中,每个程序中都必须包含有这样一个函数,该函数的函数名为( ) 。A)main B)MAIN C)name D)function3.cout 是某个类的标准对象的引用,该类是( ) 。A)ostream B)istream C)stdout D)stdin4.如果在类外的非类的成员函数中有函数调用 CPoint::func();则函数 func()是类 CPoint 的( ) 。A)私有静态成员函数 B)公有非静态成员函数C)公有静态成员函数 B)友元函数5. 如果 class 类中的所有成员在定义时都没有使用关键字 public、private 或 protected,则所有成员缺省定义为( ) 。A)public B)protected C)private D)static 6.一个类的所有对象共享的是( ) 。A)私有数据成员 B)公有数据成员C)保护数据成员 D)静态数据成员7.动态联编所支持的多态性称为( ) 。A)虚函数 B)继承C)编译时多态性 D)运行时多态性8.定义类模板时要使用关键字( ) 。A)const B)new C)delete D)template9.对虚基类的定义( ) 。A)不需要使用虚函数 B)必须使用虚函数C)必须使用 private D)必须使用 public10.类类型转换函数( ) 。A)不能带有参数 B)只能带一个参数C)只能带 2 个参数 D)只能带 3 个参数二、填空题(本大题共 5 小题,每小题 2 分,共 10 分)不写解答过程,将正确的答案写在每小题的空格内。错填或不填均无分。1.在用 C++进行程序设计时,最好用( )代替 malloc。2.函数模板中紧随 template 之后尖括号内的类型参数都要寇以保留字( ) 。3.编译时多态性可以用( )函数实现。4.拷贝构造函数用它所在类的( )作为参数。5.用关键字 static 修饰的类的成员称为( )成员。三、程序分析题(本大题共 6 小题,每小题 5 分,共 30 分)给出下面各程序的输出结果。1.阅读下面程序,写出输出结果。#include using namespace std;class Array{public:Array(int a[], int iSize):elem(a), size(iSize){}int GetSize(){return size;}int &operator[](int i){return elem[i - 1];}private:int *elem;int size;};int main(){int s[]={3, 7, 2, 1, 5};Array ar(s, 5);ar[1] = 9;for (int i = 1; i using namespace std;template void Print(Type a[], int n){for (int i = 0; i using namespace std;class Test{public:Test(int n):num(n){count++;}~Test(){}void Print() const;static int GetCount(){return count;}private:int num;static int count;};int Test::count = 0;void Test::Print() const{cout num count using namespace std;class Test{public:Test(int a = 0, int b = 0, int c = 0):x(a), y(b), z(c) {}void Print(){cout using namespace std;class MyClass{private:static int n;public:MyClass() { n += 1; }~MyClass() { n -= 1; }static int GetNum() { return n; }}; int MyClass::n = 0;int main(){cout using namespace std;class A{private:int a;public:A() { cout using namespace std; class Test{private:int num;public:Test(int num = 0) { [1] = num; } //初始化数据成员 num 为形参 numint GetNum() const { return num; }};int main(){Test obj;cout using namespace std; class A{private:int a;public:A(int m): a(m) {}void Show() const { cout using namespace std; class Point{private:int x, y;static int count;public:Point(int m = 0, int n = 0): x(m), y(n) { count++; } ~Point() { count--; }int GetX() const { return x; }int GetY() const { return y; }static void ShowCount() { cout using namespace 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; } [4] (const Complex &a) const // 重载加法运算符+{ Complex b;b.realPart = this->realPart + a.realPart; b.imagePart = this->imagePart + a.imagePart; return b;}};int main(){Complex a(1, 2), b(2, 6), c;c = a + b;cout Type Abs(Type a)2.定义一个复数类 Complex,定义带有 2 个参数(其中一个为缺省参数)的构造函数,显示复数值的函数 Show(), 重载“+”运算符(用成员函数实现) ,并编写测试程序进行测试。C++面向对象程序设计模拟试题一参考答案一、单项选择题(本大题共 10 小题,每小题 2 分,共 20 分)在每小题列出的四个备选项中,只有一个是符合题目要求的,请将其代码填写在题后的括号内。错选、多选或未选均无分。1.B) 2.A) 3.A ) 4.C) 5.C)6.D) 7.D) 8.D ) 9.A) 10.A)二、填空题(本大题共 5 小题,每小题 2 分,共 10 分)不写解答过程,将正确的答案写在每小题的空格内。错填或不填均无分。1.参考答案:new2.参考答案:class 或 typename3.参考答案:重载4.参考答案:对象5.参考答案:静态三、程序分析题(本大题共 6 小题,每小题 5 分,共 30 分)给出下面各程序的输出结果。1.参考答案:9 7 2 1 52.参考答案:5 6 8 6.8 9.63.参考答案:6 1 8 2 24.参考答案:0016185.参考答案:0121end6.参考答案:无参构造函数含参构造函数 a=1复制构造函数析构函数析构函数析构函数四、完成程序填题(本大题共 4 个小题,每小题 3 分,共 12 分)下面程序都留有空白,请将程序补充完整。1.参考答案:[1] this->num 或 Integer::num2.参考答案:[2] A(m), b(n)3.参考答案:[3] int Point::count = 0;4.参考答案:[4] Complex operator+五、编程题(本大题共 2 小题,第 1 小题 12 分,第 2 小题 16 分,共 28 分)1.参考程序: #include using namespace std;template Type Abs(Type a){if (a >= 0) return a;else return - a;}int main(){cout using namespace std;class Complex{public:Complex(double r, double i = 0){real = r;image = i;}void Show(){cout 0) cout << "+" << image << "i" << endl;else if(image < 0) cout << "-" << -image << "i" << endl;else cout << endl;}Complex operator+(const Complex &obj){Complex temp(real + obj.real, image + obj.image);return temp;}private:double real, image;};int main(){Complex z1(2, 6), z2(3, 8), z3(0);z1.Show();z2.Show();z3.Show();z3 = z1 + z2;z3.Show();return 0;}
展开阅读全文
温馨提示:
金锄头文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档


电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号