川大c++期末复习题

上传人:jiups****uk12 文档编号:48506251 上传时间:2018-07-16 格式:DOC 页数:13 大小:88KB
返回 下载 相关 举报
川大c++期末复习题_第1页
第1页 / 共13页
川大c++期末复习题_第2页
第2页 / 共13页
川大c++期末复习题_第3页
第3页 / 共13页
川大c++期末复习题_第4页
第4页 / 共13页
川大c++期末复习题_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《川大c++期末复习题》由会员分享,可在线阅读,更多相关《川大c++期末复习题(13页珍藏版)》请在金锄头文库上搜索。

1、C+面向对象程序设计模拟试题一一、单项选择题(本大题共10小题,每小题2分,共20分)在每小题列出的四个备选项中,只有一个是符合题目要求的,请将其代码填写在题后的括号内。错选、多选或未选均无分。1说明虚函数的关键字是( )。A)inlineB)virtualC)defineD)static2在标准C+中,每个程序中都必须包含有这样一个函数,该函数的函数名为( )。A)main B)MAIN C)name D)function3cout是某个类的标准对象的引用,该类是( )。A)ostreamB)istreamC)stdoutD)stdin4如果在类外的非类的成员函数中有函数调用CPoint:f

2、unc();则函数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)constB)newC)dele

3、teD)template9对虚基类的定义( )。A)不需要使用虚函数B)必须使用虚函数C)必须使用privateD)必须使用public10类类型转换函数( )。A)不能带有参数B)只能带一个参数C)只能带2个参数D)只能带3个参数二、填空题(本大题共5小题,每小题2分,共10分)不写解答过程,将正确的答案写在每小题的空格内。错填或不填均无分。1在用C+进行程序设计时,最好用( )代替malloc。2函数模板中紧随template之后尖括号内的类型参数都要寇以保留字( )。3编译时多态性可以用( )函数实现。4拷贝构造函数用它所在类的( )作为参数。5用关键字static修饰的类的成员称为(

4、)成员。三、程序分析题(本大题共6小题,每小题5分,共30分)给出下面各程序的输出结果。1阅读下面程序,写出输出结果。#include using namespace std;class Arraypublic:Array(int a, int iSize):elem(a), size(iSize)int GetSize()return size;int &operator(int i)return elemi - 1;private:int *elem;int size;int main()int s=3, 7, 2, 1, 5;Array ar(s, 5);ar1 = 9;for (int

5、 i = 1; i = 5; i+)cout ari ;cout endl;return 0;上面程序的输出结果为:2阅读下面程序,写出输出结果。#include using namespace std;template void Print(Type a, int n)for (int i = 0; i n; i+)cout ai ;int main()int a = 5, 6, 8;double b = 6.8, 9.6;Print(a, sizeof(a) / sizeof(int);Print(b, 2);cout endl;return 0;上面程序的输出结果为:3阅读下面程序,写出

6、输出结果。#include using namespace std;class Testpublic: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() constcout num count ;int main()Test oTest1(6);oTest1.Print();Test oTest2(8);oTest2.Print();cout

7、 Test:GetCount();cout endl;return 0;上面程序的输出结果为:4阅读下面程序,写出输出结果。#include using namespace std;class Testpublic:Test(int a = 0, int b = 0, int c = 0):x(a), y(b), z(c) void Print()cout x endl;cout y endl;void Print() constcout z endl;private:int x, y;const int z;int main()Test obj1;obj1.Print();Test obj2

8、(1, 6, 8);obj2.Print();const Test obj3(6, 0, 18);obj3.Print();cout endl;return 0;上面程序的输出结果为:5阅读下面程序,写出输出结果。#include using namespace std;class MyClassprivate:static int n;public:MyClass() n += 1; MyClass() n -= 1; static int GetNum() return n; ; int MyClass:n = 0;int main()cout MyClass:GetNum() endl;

9、MyClass obj;cout MyClass:GetNum() endl;MyClass *p = new MyClass;cout MyClass:GetNum() endl;delete p;cout MyClass:GetNum() endl;cout end endl;return 0; 上面程序的输出结果为:6阅读下面程序,写出输出结果。#include using namespace std;class Aprivate:int a;public:A() cout 无参构造函数 endl; A(int a) cout 含参构造函数a= a endl; A(const A &co

10、py): a(copy.a) cout 复制构造函数 endl; A() cout 析构函数 endl; ;int main()A obj1, obj2(1), obj3(obj2);return 0; 上面程序的输出结果为:四、完成程序填题(本大题共4个小题,每小题3分,共12分)下面程序都留有空白,请将程序补充完整。1将如下程序补充完整。#include using namespace std; class Testprivate:int num;public:Test(int num = 0) 1 = num; /初始化数据成员num为形参numint GetNum() const re

11、turn num; ;int main()Test obj;cout obj.GetNum() endl;return 0; 2将如下程序补充完整。#include using namespace std; class Aprivate:int a;public:A(int m): a(m) void Show() const cout a endl; ;class B: Aprivate:int b;public:B(int m, int n = 0): 2 / 初始化数据成员b的值为nvoid Show() const A:Show();cout b endl;int main()B ob

12、j(8);obj.Show();return 0; 3下列程序的输出结果为:010试将程序补充完整。#include using namespace std; class Pointprivate: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 count endl; ; 3 / 静态数

13、据成员的初始化为0int main()Point:ShowCount();Point *p = new Point;Point:ShowCount();delete p;Point:ShowCount();return 0; 4将如下程序补充完整。#include using namespace std;class Complexprivate:double realPart;double imagePart;public:Complex(double real = 0, double image = 0): realPart(real), imagePart(image) double Ge

14、tRealPart() 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 a= a.GetRealPart() + a.Ge

15、tImagePart() i endl;cout b= b.GetRealPart() + b.GetImagePart() i endl;cout c= c.GetRealPart() + c.GetImagePart() i endl;return 0; 五、编程题(本大题共2小题,第1小题12分,第2小题16分,共28分)1编写一个函数模板,用于求参数的绝对值,并编写测试程序进行测试。函数模板声明如下:template Type Abs(Type a);2定义一个复数类Complex,定义带有2个参数(其中一个为缺省参数)的构造函数,显示复数值的函数Show(), 重载“+”运算符(用成

16、员函数实现),并编写测试程序进行测试。C+面向对象程序设计模拟试题一参考答案一、单项选择题(本大题共10小题,每小题2分,共20分)在每小题列出的四个备选项中,只有一个是符合题目要求的,请将其代码填写在题后的括号内。错选、多选或未选均无分。1B)2A)3A)4C)5C)6D)7D)8D)9A)10A)二、填空题(本大题共5小题,每小题2分,共10分)不写解答过程,将正确的答案写在每小题的空格内。错填或不填均无分。1参考答案:new2参考答案:class或typename3参考答案:重载4参考答案:对象5参考答案:静态三、程序分析题(本大题共6小题,每小题5分,共30分)给出下面各程序的输出结果

17、。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参考程

18、序: #include using namespace std;template Type Abs(Type a)if (a = 0) return a;else return - a;int main()cout Abs(5) endl;cout Abs(-5) endl;cout Abs(2.5) endl;cout Abs(-2.5) endl;return 0;2参考程序: #include using namespace std;class Complexpublic:Complex(double r, double i = 0)real = r;image = i;void Sho

19、w()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号