教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 法律文档 >

四川大学C++面向对象程序设计模拟试题2

来源:网络收集 时间:2026-04-26
导读: C++面向对象程序设计模拟试题二 一、单项选择题 1.说明内联函数的关键字是(A)。 A)inline B)virtual C)define D)static 2.假定CAb为一个类,则执行CAb oX;语句时将自动调用该类的(B) A)有参构造函数 B)无参构造函数 C)拷贝构造函数 D)赋值重

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

一、单项选择题

1.说明内联函数的关键字是(A)。

A)inline B)virtual C)define D)static 2.假定CAb为一个类,则执行CAb oX;语句时将自动调用该类的(B)

A)有参构造函数 B)无参构造函数 C)拷贝构造函数 D)赋值重载函数 3.cin是某个类的标准对象的引用,该类是(B)。 A)ostream B)istream C)stdout D)stdin 4.下面的哪个保留字不能作为函数的返回类型?(C)

A)void B)int C)template D)long 5.派生类的成员函数不能访问基类的(C)。

A)保护成员 B)公有成员 C)私有成员 D)前面各选项都正确 6.在语句“cout << data;”中,cout是(C)。

A)C++的关键字 B)类名 C)对象名 D)函数名 7.编译时多态性使用什么获得?(A)

A)重载函数 B)继承 C)虚函数 D)B和C 8.拷贝构造函数的参数通常是(C)。 A)无特殊要求 B. 指向对象的指针 C)本类对象的常引用 D)对象 9.C++有几种联编?(B) A)1种 B)2种 C)3种 D)4种 10.基类和派生类可以分别称为(B)。 A)“大类”和“小类” B)“父类”和“子类” C)“小类”和“大类” D)“子类”和“父类” 二、填空题

1.设函数max是由函数模板实现的,并且max(3.5, 5)和max(3, 5)都是正确的函数调用,则此函数模板具有(2)个类型参数。

2.在C++中,函数重载与虚函数帮助实现了类的(多态)性。 3.由static修饰的数据成员为该类的所有对象(共享)。

4.重载函数一般在参数类型或参数个数上不同,但(函数名)相同。

5.使用new建立的动态对象在不用时应该用(delete)释放所占用的空间。

三、程序分析题(本大题共6小题,每小题5分,共30分)给出下面各程序的输出结果。 1.阅读下面程序,写出输出结果。 #include using namespace std;

class Point {

public: 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() << 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; }

上面程序的输出结果为:0 16 18 6 8

2.阅读下面程序,写出输出结果。 #include using namespace std;

template class Test {

public: Test(Type a[], int iSize):elem(a) { size = iSize; } void Print() const { for (int i = 0; i < size; i++) cout << elem[i] << \

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; }

上面程序的输出结果为:1 0 8 1.6 1.8

3.阅读下面程序,写出输出结果。 #include using namespace std;

class Goods {

public: Goods(int w): weight(w) { totalWeight = 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() const { cout << this->weight << \ \ \}

int main() { Goods g1(6); g1.Print(); Goods g2(g1); g2.Print(); cout << Goods::GetTotalWeight(); cout << endl; return 0; }

上面程序的输出结果为:6 6 6 12 12

4.阅读下面程序,写出输出结果。 #include using namespace std;

template class Test {

public: Test(Type a = 0, Type b = 0, Type c = 0):z(c) { void Print() { cout << x << endl; cout << y << endl; } void Print() const { cout << z << endl; }

private: Type x, y; const Type z;

x = a; y = b; } };

int main() { Test t1; t1.Print(); Test t2(1, 9, 6); t2.Print(); const Test t3(0, 6, 1.8); t3.Print(); return 0; }

上面程序的输出结果为:0 0 1 9 1.8

5.阅读下面程序,写出输出结果。 #include using namespace std;

template

Type Max(const Type &a, const Type &b) { if (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) << \ cout << Min(x, y) << \ return 0; }

上面程序的输出结果为:6.09 6 5.38 5

…… 此处隐藏:673字,全部文档内容请下载后查看。喜欢就下载吧 ……
四川大学C++面向对象程序设计模拟试题2.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/434954.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)