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

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

来源:网络收集 时间:2026-04-26
导读: 6.阅读下面程序,写出输出结果。 #include using namespace std; class A { public: A() { cout class B: public A { public: B() { cout int main(void) { B obj; return 0; } 上面程序的输出结果为:A B ~B ~A

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

class A {

public: A() { cout << \ ~A() { cout << \ };

class B: public A {

public: B() { cout << \ ~B() { cout << \ };

int main(void) { B obj; return 0; }

上面程序的输出结果为:A B ~B ~A

四、完成程序填题(本大题共4个小题,每小题3分,共12分)下面程序都留有空白,请将程序补充完整。

1.将如下程序补充完整。 #include using namespace std;

class A {

private: int n;

public: A(int n) { [1] this->n或A::n = n; } // 将数据成员n初始化为形参n void Show() const { cout << n << endl; } };

int main() { A i = 8; i.Show(); return 0; }

2.将如下程序补充完整。

#include using namespace std;

class A {

private: int a;

public: A(int m): a(m) {} void Show() const { cout << a << endl; } };

class B: A {

public: 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 Test

// 将数据成员a初始化为m

{

private: static int num;

public: Test() { num++; } ~Test() { num--; } static void ShowObjectNum() { cout << num << endl; } };

[3] int Test::num = 0; // 静态数据成员的初始化为0

int main(void) { Test::ShowObjectNum(); Test obj; Test::ShowObjectNum(); return 0; }

4.将如下程序补充完整。 #include using namespace std;

class Integer {

private: int i;

public: Integer(int x = 0): i(x) { } [4] operator int() { return i; }// 类类型转换函数,将类Integer转换为基本类型int };

int main() { Integer a, b(18) ; cout << a << endl; cout << int(b) << endl; return 0;

}

五、编程题(本大题共2小题,第1小题12分,第2小题16分,共28分)

1.编写一个函数模板,用于求数组中各元素之和,并编写测试程序进行测试。 函数模板声明如下: template Type Sum(Type a[], int n);

参考程序:

#include using namespace std; template Type Sum(Type a[], int n) { Type tSum = 0; for (int i = 0; i < n; i++) { tSum = tSum + a[i]; } return tSum; }

int main() { int a[] = {1, 2, 3}; double b[] = {1.5, 2.8, 8.9, 8}; cout << Sum(a, 3) << endl; cout << Sum(b, 4) << endl; return 0; }

2.定义一个抽象类Shape,它有一个纯虚函数GetPerimeter();派生出四边型类Rectangle和圆类Circle,在派生类中重载函数GetPerimeter(),用于求图形的周长,编写测试程序进行测试。

参考程序:

#include using namespace std; class Shape {

public: virtual double GetPerimeter() const = 0; };

class Rectangle:public Shape {

public: Rectangle(double w, double h) { width = w; height = h; } double GetPerimeter() const { return 2 * (width + height); } private: double width, height; };

class Circle:public Shape {

public: Circle(double r) { radius = r; } double GetPerimeter() const { return 3.1415926 * radius * radius; }

private: double radius; };

int main() { Rectangle oRectangle(2, 3); cout << oRectangle.GetPerimeter() << endl; Circle oCircle(10); cout << oCircle.GetPerimeter() << endl; return 0; }

…… 此处隐藏:154字,全部文档内容请下载后查看。喜欢就下载吧 ……
四川大学C++面向对象程序设计模拟试题2(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)