1、C+面向对象程序设计模拟试题2C+面向对象程序设计模拟试题二一、单项选择题(本大题共10小题,每小题2分,共20分)在每小题列出的四个备选项中,只有一个是符合题目要求的,请将其代码填写在题后的括号内。错选、多选或未选均无分。1说明内联函数的关键字是( )。A)inline B)virtual C)define D)static2假定CAb为一个类,则执行CAb oX;语句时将自动调用该类的( ) A)有参构造函数 B)无参构造函数 C)拷贝构造函数 D)赋值重载函数 3cin是某个类的标准对象的引用,该类是( )。 A)ostream B)istream C)stdout D)stdin4下面
2、的哪个保留字不能作为函数的返回类型?( )A)void B)int C)template D)long5派生类的成员函数不能访问基类的( )。A)保护成员 B)公有成员C)私有成员 D)前面各选项都正确6在语句“cout data;”中,cout是( )。A)C+的关键字 B)类名C)对象名 D)函数名7编译时多态性使用什么获得?( )A)重载函数 B)继承 C)虚函数 D)B和C 8拷贝构造函数的参数通常是( )。 A)无特殊要求 B. 指向对象的指针 C)本类对象的常引用 D)对象9C+有几种联编?( ) A)1种 B)2种 C)3种 D)4种10基类和派生类可以分别称为( )。 A)“大
3、类”和“小类” B)“父类”和“子类” C)“小类”和“大类” D)“子类”和“父类”二、填空题(本大题共5小题,每小题2分,共10分)不写解答过程,将正确的答案写在每小题的空格内。错填或不填均无分。1设函数max是由函数模板实现的,并且max(3.5, 5)和max(3, 5)都是正确的函数调用,则此函数模板具有( )个类型参数。2在C+中,函数重载与虚函数帮助实现了类的( )性。3由static修饰的数据成员为该类的所有对象( )。4重载函数一般在参数类型或参数个数上不同,但( )相同。5使用new建立的动态对象在不用时应该用( )释放所占用的空间。三、程序分析题(本大题共6小题,每小题5
4、分,共30分)给出下面各程序的输出结果。1阅读下面程序,写出输出结果。#include using namespace std;class Pointpublic: 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 u; const Point v(6, 8); cout u.GetX()
5、 endl; u.SetX(16); cout u.GetX() endl; u.SetY(18); cout u.GetY() endl; cout v.GetX() endl; cout v.GetY() endl; return 0;上面程序的输出结果为:2阅读下面程序,写出输出结果。#include using namespace std;template class Testpublic: Test(Type a, int iSize):elem(a) size = iSize; void Print() const for (int i = 0; i size; i+) cout
6、elemi ; private: Type *elem; int size;int main() int a = 1, 0, 8; double b = 1.6, 1.8; Test ar1(a, 3); ar1.Print(); Test ar2(b, sizeof(b) / sizeof(double); ar2.Print(); cout endl; return 0;上面程序的输出结果为:3阅读下面程序,写出输出结果。#include using namespace std;class Goodspublic: Goods(int w): weight(w) totalWeight =
7、 totalWeight + w; Goods(const Goods &g) weight = g.weight; totalWeight = totalWeight + weight; Goods() totalWeight = totalWeight - weight; void Print() const; static int GetTotalWeight() return totalWeight; private: int weight; static int totalWeight;int Goods:totalWeight = 0; void Goods:Print() con
8、st cout weight totalWeight ;int main() Goods g1(6); g1.Print(); Goods g2(g1); g2.Print(); cout Goods:GetTotalWeight(); cout endl; return 0;上面程序的输出结果为:4阅读下面程序,写出输出结果。#include using namespace std;template class Testpublic: Test(Type a = 0, Type b = 0, Type c = 0):z(c) x = a; y = b; void Print() cout x
9、 endl; cout y endl; void Print() const cout z endl; private: Type x, y; const Type z;int main() Test t1; t1.Print(); Test t2(1, 9, 6); t2.Print(); const Test t3(0, 6, 1.8); t3.Print(); return 0;上面程序的输出结果为:5阅读下面程序,写出输出结果。#include using namespace std; template Type Max(const Type &a, const Type &b) if
10、 (a b) return b; else return a; template Type Min(const Type &a, const Type &b) if (a b) return a; else return b; int main() double x = 5.38, y = 6.09; cout Max(x, y) Max(x, y) ; cout Min(x, y) Min(x, y) endl; return 0; 上面程序的输出结果为:6阅读下面程序,写出输出结果。#include using namespace std; class Apublic: A() cout
11、A endl; A() cout A endl; ;class B: public Apublic: B() cout B endl; B() cout B endl; ;int main(void) B obj; return 0; 上面程序的输出结果为:四、完成程序填题(本大题共4个小题,每小题3分,共12分)下面程序都留有空白,请将程序补充完整。1将如下程序补充完整。#include using namespace std; class Aprivate: int n; public: A(int n) 1 = n; / 将数据成员n初始化为形参n void Show() const c
12、out n endl; ;int main() A i = 8; i.Show(); 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: Apublic: B(int m): 2 / 将数据成员a初始化为m void Show() const A:Show(); ;int main() B obj(8); obj.Show(); return 0; 3将如下程序补充完整。#include using namespace std; class Testprivate: static int num; public: Test() num+; Test() num-; static void ShowObjectNum() cout num endl; ;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1