2019 - - 2020 学年( 2 )学期期末考试试卷《 面向对象程序设计实验课程 》试卷(A 卷)参考答案

 题目一:

1.题目:

1.(40 分) )建立一个类 Intersection,求两个整数集合的交集,具体要求如下:
(1)私有数据成员
int set[20] //用数组空间存放数据集合
int len // 集合中元素的个数
(2) 公有成员函数
Intersection(int *s,int l):用 s 初始化集合,用变量 l 初始
化其长度
Intersection( ):重写构造函数,把 set 中各元素和长度初始
化为 0
int f(int num):判断整数 num 是否属于集合,是返回 1,否则
返回 0;
Intersection operator&&(Intersection t):重载&&,求当前对
象的集合和参数对象 t 的集合的交集,方法是用对象 t 的集合中的每个元素作
为参数调用 f 函数,若该元素属于当前对象的集合,则把它复制给交集。
void show():输出集合
(3) 在主函数中对定义的类进行测试。定义数组 s1:{1,3,4,5,7,8}、
s2:{1,2,3,5,7,9,11},并用它们初始化类 Intersection 的对象 obj1 和 obj2,
然后调用相关的成员函数输出集合;定义对象 obj3,并用 obj1 和 obj2 的与
运算符结果(交集)初始化该对象,并输出交集。

2.代码:

#include<iostream>
using namespace std;
class Intersection {
private:
	int set[20];//存放数据集合
	int len;//存放元素个数
public:
	Intersection(int* s, int l) {
		for (int i = 0; i < l; i++) {
			*(set + i) = *(s + i);
		}
		this->len = l;
	}
	Intersection() {
		*set = 0;
		len = 0;
	}
	int f(int num) {
		for (int i = 0; i < len; i++) {
			if (num == set[i])
				return 1;
		}
		return 0;
	}
	Intersection operator &&(Intersection t) {
		Intersection res;
		int j = 0;
		for (int i = 0; i < t.len; i++) {
			if (f(t.set[i])) {
				res.len++;
				res.set[j] = t.set[i];
				j++;
			}
		}
		return res;
	}
	void show() {
		for (int i = 0; i < len; i++) {
			cout << set[i]<<" ";
		}

	}
};
int main() {
	int s1[6] = { 1,3,4,5,7,8, };
	int s2[7] = { 1,2,3,5,7,9,11 };
	Intersection obj1(s1, 6);
	Intersection obj2(s2, 7);
	Intersection obj3;
	obj3 = obj1 && obj2;
	obj3.show();
}

题目二:

1.题目:

2. (40 分 )利用虚函数实现多态性来求正方体、球体和圆柱体的表面积和体
积。
具体要求如下:
1)从正方体、球体和圆柱体的各种运算中抽象出一个公共基类
container 为抽象类,在其中定义求表面积和体积的纯虚函数(该抽象类本身
是没有表面积和体积可言的)。
2)在抽象类中定义一个公共的数据成员 radius,此数据可作为球的
半径、正方体的边长、圆柱体底面圆半径。
3)由此抽象类派生出要描述的 3个类,即 cube、sphere和 cylinder。

2.代码:

#include<iostream>
using  namespace std;
double pi = 3.1415926;

class container {
public:
	double radius;//半径,边长
public:
	container(double r) {
		this->radius = r;
	}
	virtual double area() = 0;
	virtual double volume() = 0;
	
};
class cube :public container {
public:
	cube(double r):container(r) {	}
	double area() {
		return 6 * radius * radius;
	}
	double volume() {
		return radius * radius * radius;
	}
};
class sphere :public container{
public:
	sphere(double r):container(r){	}
	double area() {
		return 4 * pi * radius * radius;
	}
	double volume() {
		return 4 * pi * radius * radius * radius / 3;
	}
};
class cylinder :public container {
private:
	double h;
public:
	cylinder(double r, double h):container(r) {
		this->h = h;
	}
	double area() {
		return pi * radius * radius * 2 + 2 * pi * radius * h;
	}
	double volume() {
		return pi * radius * radius * h;
	}
};

int main() {
	cube c(2);
	sphere s(1);
	cylinder cy(3, 1);
	cout << c.area() << " " << c.volume() << endl;

	cout << s.area() << " " << s.volume() << endl;

	cout << cy.area() << " " << cy.volume() << endl;
}

题目三:

1.题目: 

3、( 20 分)对文件作如下操作:
(1)新建一个文本文件 data1.txt,该文件中写入十个个实数;
(2)在(1)中建立的文本文件中,读出这个十个实数,并求出其中的最
大数、最小数和平均值;
(3)向(1)中文件追加记录,并将该文件复制为 data2.txt。

2.代码:

#include<iostream>
#include<fstream>
#include<cstring>
#include <string>
using namespace std;
int main() {
	double a[10];//用于将数组中的数据存储到文件中
	double b[10];//用于从文件中读取数组
	ofstream outfile("f1.txt", ios::out);
	if (!outfile) {
		cerr << "open error!" << endl;
		exit(1);
	}
	cout << "input 10 integers: " << endl;
	for (int i = 0; i < 10; i++) {
		cin >> a[i];
		outfile << a[i] << " ";
	}
	outfile << endl;
	ifstream infile("f1.txt", ios::app);
	if(!infile) {
		cerr << "open error!" << endl;
		exit(1);
	}
	for (int i = 0; i < 10; i++) {
		infile >> b[i];
		cout << b[i]<<" ";
	}
	int max = b[0], min = b[0];
	double avg = b[0];//最大,最小,平均值
	for (int i = 1; i < 10; i++) {
		if (b[i] > max)
			max = b[i];
		if (b[i] < min)
			min = b[i];
		avg += b[i];
	}
	avg /= 10;
	outfile << "max: " << max << endl;
	outfile << " min: " << min << endl;
	outfile << " average: " << avg << endl;
	
	
	string line;
	char ch;
	ofstream outfile1("f2.txt",ios::out);
	if (!outfile1) {
		cerr << "open error!" << endl;
		exit(1);
	}
	ifstream infile1("f1.txt");
	if (!infile1) {
		cerr << "open error!" << endl;
		exit(1);
	}
	do{
		outfile1 << line << endl;;
		cout << "文件复制成功!" << endl;
	} while (getline(infile1, line));
	outfile.close();
	infile.close();
	outfile1.close();
}

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜜汁博哥

我是大学生,给钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值