1、四川大学C面向对象程序设计模拟试题7C+面向对象程序设计模拟试题七一、单项选择题(本大题共10小题,每题2分,共20分)在每题列出的四个备选项中,只有一个是符合题目要求的,请将其代码填写在题后的括号内。错选、多项选择或未选均无分。1以下语句中,错误的选项是( )。A)const int buffer = 168; B)const int num;C)const double *p; D)double * const p = new double;2C+中的模板包括( )。A)对象模板和函数模板 B)对象模板和类模板C)函数模板和类模板 D)变量模板和对象模板3关于常对象,C+遵循( )。A)通
2、过常对象能够挪用常成员函数 B)通过常对象能够所有成员函数C)常对象的成员都是常成员 D)通过常对象能够挪用任何不改变对象值的成员函数4以下关键字不能用来声明类的访问权限的是( )。A)public B)static C)protected D)private5以下关于this指针的表达中,正确的选项是( )。A)与类相关的所有函数都含有this指针B)所有类的成员函数都有this指针C)所有函数都有this指针D)只有类的非静态成员函数才有this指针6派生类的成员函数不能直接访问基类的( )。A)爱惜成员 B)公有成员C)私有成员 D)前面各选项都正确7在以下函数原型中,能够作为类Test
3、构造函数的是( )。A)void Test (int); B)int Test ();C)Test (int); D)Test (int);8在下面4个关键字中,用于说明虚基类的是( )。A)virtual B)publicC)protected D)private9在C+中,用于实现编译时多态性的是( )。A)友元函数 B)重载函数 C)静态成员函数 D)虚函数10以下关于C+函数的说明中,正确的选项是( )。A)内置函数确实是概念在另一个函数体内部的函数B)函数体的最后一条语句必需是return语句C)C+标准要求在挪用一个函数之前,若是没概念函数,那么必需先声明其原型D)编译器会依照函数
4、的返回值类型和参数表来区分函数的不同重载形式二、填空题(本大题共5小题,每题2分,共10分)不写解答进程,将正确的答案写在每题的空格内。错填或不填均无分。1一个函数名为Show,返回值类型为void,没有参数的纯虚常成员函数能够声明为( )。2假设将一个二元运算符重载为类的友员函数,其形参个数应该是( )个。3采纳struct概念的类中数据成员和成员函数的默许访问权限是( )。4在面向对象方式中,( )描述的是具有相同属性与操作的一组对象。5模板类型形参表利用的关键字为( )。三、程序分析题(本大题共6小题,每题5分,共30分)给出下面各程序的输出结果。1阅读下面程序,写出输出结果。#incl
5、ude using namespace std; class Testprivate: int n; public: Test(int i = 16) n = i; int Get() return n; int Get() const return n - 2; ;int main() Test a; const Test b(18); cout () , () endl; return 0; 上面程序的输出结果为:2阅读下面程序,写出输出结果。#include using namespace std; class Apublic: A() cout A endl; A() cout A e
6、ndl; ;class B: Apublic: B() cout B endl; B() cout B endl; ;int main() B obj; return 0;上面程序的输出结果为:3阅读下面程序,写出输出结果。#include using namespace std; int f(int a) return a * a + 8; template T f(const T &a) return a * a; int main() cout f(1) , f endl; return 0; 上面程序的输出结果为:4阅读下面程序,写出输出结果。#include using namesp
7、ace std; namespace ns int x = 18; int x = 5; int main() int x = 10; cout x :x ns:x endl; return 0; 上面程序的输出结果为:5阅读下面程序,写出输出结果。#include using namespace std; class Apublic: virtual void Show() const cout A:Show() endl; ;class B: public Apublic: void Show() const cout B:Show() endl; ;void Refers(const A
8、 &obj) (); int main() A obj1; B obj2; Refers(obj1); Refers(obj2); return 0; 上面程序的输出结果为:6阅读下面程序,写出输出结果。#include using namespace std; class Aprotected: int a; public: A(int x): a(x) void Show() const cout a endl; ;class B: public A protected: int b; public: B(int x, int y): A(x), b(y) void Show() cons
9、t cout b endl; ;int main() B obj(18, 5); (); :Show(); return 0; 上面程序的输出结果为:四、完成程序填题(本大题共4个小题,每题3分,共12分)下面程序都留有空白,请将程序补充完整。1将如下程序补充完整。#include using namespace std; class Integerprivate: int n; public: Integer(int n): n(n) Integer & 1 -n; return *this; / 重载前缀-运算符 void Show() const cout n endl; ;int ma
10、in() Integer i(168); -i; (); return 0; 2将如下程序补充完整。#include using namespace std; class Aprivate: int a; public: A(int m = 0): a(m) 2 / 类型转换函数,A类对象转转成int型量 return a; ;int main() A a = ; cout a endl; return 0; 3将如下程序补充完整。#include using namespace std; class Aprivate: int m; public: A(int a): m(a) virtua
11、l void Show() const cout m endl; ;class B: public Aprivate: int n; public: B(int a, int b): A(a) n = b; void Show() const cout n endl; ;int main() B obj(158, 98); 2 Show(); / 调用基类的Show() return 0; 4将如下程序补充完整。#include using namespace std; class Complexprivate: double realPart; double imagePart; publi
12、c: Complex(double real = 0, double image = 0): realPart(real), imagePart(image) Complex 3 (const Complex &a) const / 重载减法运算符+ return Complex(realPart - , imagePart - ; void Show() const cout realPart + imagePart i endl;int main() Complex a(2, 9), b(1, 1), c; c = a - b; cout a=; (); cout b=; (); cout
13、 c=; (); return 0; 五、编程题(本大题共2小题,第1小题12分,第2小题16分,共28分)1试利用函数模板实现求一个数组各元素的积,要求编写测试程序。函数模板声明如下:template Type Mul(Type a, int n); / 求数组a各元素的积2设计一个日期类Date,,要求:(1)包括年(year)、月(month)和日(day)私有数据成员。(2)包括构造函数,重载输出运算符“”。要求编写测试程序。C+面向对象程序设计模拟试题七参考答案一、单项选择题(本大题共10小题,每题2分,共20分)在每题列出的四个备选项中,只有一个是符合题目要求的,请将其代码填写在题
14、后的括号内。错选、多项选择或未选均无分。1B) 2C) 3A) 4B) 5D)6C) 7D) 8A) 9B) 10C)二、填空题(本大题共5小题,每题2分,共10分)不写解答进程,将正确的答案写在每题的空格内。错填或不填均无分。1参考答案:virtual void Show() const = 0;2参考答案:23参考答案:public或公有4参考答案:类5参考答案:class三、程序分析题(本大题共6小题,每题5分,共30分)给出下面各程序的输出结果。1参考答案:16,162参考答案:ABBA3参考答案:9,14参考答案:10 5 185参考答案:A:Show()B:Show()6参考答案:
15、518四、完成程序填题(本大题共4个小题,每题3分,共12分)下面程序都留有空白,请将程序补充完整。1参考答案:1 operator-()2参考答案:2 operator int() const或operator int()3参考答案:3 :4参考答案:4 operator-五、编程题(本大题共2小题,第1小题12分,第2小题16分,共28分)1参考程序: #include using namespace std; template Type Mul(Type a, int n) / 求数组a各元素的积 Type t = 1; for (int i = 0; i n; i+) t *= ai;
16、 return t; int main() int a = 1, 2, 3, 4, 5, 6, 7, 8, 9; cout Mul(a, 9) endl; return 0; 2参考程序: #include using namespace std; class Dateprivate: int year; int month; int day; public: Date(int y = 2020, int m = 1, int d = 1):year(y), month(m), day(d) void SetYear(int y) year = y; void SetMonth(int m)
17、month = m; void SetDay(int d) day = d; int GetYear() const return year; int GetMonth() const return month; int GetDay() const return day; ;istream &operator(istream &in, Date &dt) int y, m, d; cout y; cout m; cout d; (y); (m); (d); return in; ostream &operator(ostream &out, const Date &dt) cout () 年 () 月 () d; cout d endl; return 0;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1