面向对象程序设计-实验八 类的多态性

2022/6/10
1.声明一个Shape抽象类,在此基础上派生出Rectangle和Circle类,二者都有GetArea()函数计算对象的面积,GetPerim()函数计算对象的周长。要求输入若干个几何体的参数,分别按照面积和周长排序输出。
#include<iostream>
#include<string>
#include<vector>
#define pi 3.14
using namespace std;
class Shape {
public:
	virtual double GetArea() = 0;//面积
	virtual double GetPerim() = 0;//周长
	virtual string type() = 0;//类型
};
class Rectangle :public Shape {
public:
	Rectangle(int x, int y, string type) :a(x), b(y), typee(type) {}
	virtual double GetArea();
	virtual double GetPerim();
	virtual string type();
private:
	int a;//长
	int b;//宽
	string typee;//类型
};
double Rectangle::GetArea() {
	return a * b;
}
double Rectangle::GetPerim() {
	return 2 * (a + b);
}
string Rectangle::type() {
	return typee;
}
class Circle :public Shape {
public:
	Circle(int rr, string type):r(rr), typee(type) {}
	virtual double GetArea();
	virtual double GetPerim();
	virtual string type();
private:
	int r;//半径
	string typee;
};
double Circle::GetArea() {
	return pi * r * r;
}
double Circle::GetPerim() {
	return 2 * pi * r;
}
string Circle::type() {
	return typee;
}
int main() {
	int n = 0;
	int a = 0, b = 0, r = 0;
	string type;
	cout << "请输入几何体个数:";
	cin >> n;
	vector<Shape*> shape(n);
	for (int i = 0; i < n; i++) {
		cout << "请输入几何体类型:";
		cin >> type;
		if (type == "Rectangle") {
			cout << "请输入矩形长和宽:";
			cin >> a >> b;
			shape[i] = new Rectangle(a, b, type);
		}
		else if (type == "Circle") {
			cout << "请输入圆的半径:";
			cin >> r;
			shape[i] = new Circle(r, type);
		}

	}
	cout << endl;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n - i - 1; j++) {
			if (shape[j]->GetArea() > shape[j + 1]->GetArea()) {
				Shape* temp;
				temp = shape[j + 1];
				shape[j + 1] = shape[j];
				shape[j] = temp;
			}
		}
	}//按面积升序排序
	cout << "按面积升序输出:" << endl;
	for (int i = 0; i < n; i++) {
		cout << shape[i]->type() << " ";
		cout << shape[i]->GetArea() << endl;
	}
	cout << endl;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n - i - 1; j++) {
			if (shape[j]->GetPerim() > shape[j + 1]->GetPerim()) {
				Shape* temp;
				temp = shape[j + 1];
				shape[j + 1] = shape[j];
				shape[j] = temp;
			}
		}
	}//按周长升序排序
	cout << "按周长升序输出:" << endl;
	for (int i = 0; i < n; i++) {
		cout << shape[i]->type() << " ";
		cout << shape[i]->GetPerim() << endl;
	}
	cout << endl;
	for (int i = 0; i < n; i++) {
		delete shape[i];
	}//删除
	return 0;
}
2.某家具公司出售沙发和桌子,需要一个库存管理程序。将家具类作为抽象类,其派生类包括沙发类和桌子类。每件家具都有编号(如:A152)、名称(如:日式纯色沙发)和这种家具的库存量(如:12)。对于沙发,还有座位数(如:3)和材质(如:真皮)。对于桌子,还有长度(如:125cm)和宽度(如:65cm)。

例如:家具库存包括:
一种沙发,编号A152,名称日式纯色沙发,库存量12,3个座位,真皮材质;
一种桌子,编号C553,名称美式实木餐桌,库存量2,长110cm,宽70cm。

要求:
1)建立一个磁盘文件,从磁盘文件读取现有库存的所有信息;
2)家具的数量不定,可实现动态添加或减少;
3)增加或者减少一件指定编号的家具;
4)显示所有沙发的信息,以及沙发的总库存;
5)给定特定长度,查找符合此条件的桌子并显示。

#include<iostream>
#include<fstream>
#include<iomanip>
#include<vector>
using namespace std;
class furniture {
public:
	virtual void change(string t, string x) = 0;//库存增减
	virtual void display(){}//输出
	virtual int search(int l) { return 0; }//查找
	string num;//编号
	string name;//名称
	int count;//库存
	int key;//类型
};//家具类
class sofa :public furniture{
public:
	sofa(string nu, string na, int c, int s, string m) {
		key = 1;
		num = nu;
		name = na;
		count = c;
		seats = s;
		material = m;
	}//
	virtual void change(string t, string x);
	virtual void display();
private:
	int seats;//座位数
	string material;//材质
};//沙发类
void sofa::change(string t, string x) {
	if (num == x) {
		if (t == "add") {
			count = count + 1;
		}
		else if(t == "minus") {
			count = count - 1;
		}
	}
}
void sofa::display() {
	cout << num << setw(13) << name << setw(4) << count << setw(4) << seats << setw(10) << material << endl;
}
class table :public furniture {
public:
	table(string nu, string na, int c, int l, int w) {
		key = 2;
		num = nu;
		name = na;
		count = c;
		length = l;
		width = w;
	}
	virtual void change(string t, string x);
	virtual void display();
	virtual int search(int l);
private:
	int length;//长度
	int width;//宽度
};//桌子类
void table::change(string t, string x) {
	if (num == x) {
		if (t == "add") {
			count = count + 1;
		}
		else if (t == "minus") {
			count = count - 1;
		}
	}
}
void table::display() {
	cout << num << setw(13) << name << setw(4) << count << setw(6) << length << setw(6) << width << endl;
}
int table::search(int l) {
	if (length == l) {
		return 1;
	}
	else {
		return 0;
	}
}
int f(string type) {
	if (type == "sofa") {
		return 1;
	}
	else if (type == "table") {
		return 2;
	}
	else {
		return 0;
	}
}//判断类型
int main() {
	int n = 0;
	string type, num, name, material;
	int count, seats, length, width;
	vector<furniture*> furnituree(100);
	ifstream fin("家具库存管理.txt");
	fin.seekg(0, ios::beg);//定位指针
	fin >> n;
	for (int i = 0; i < n; i++) {
		fin >> type;
		fin >> num;
		fin >> name;
		fin >> count;
		switch (f(type)) {
		case 1:
			fin >> seats;
			fin >> material;
			furnituree[i]=new sofa(num, name, count, seats, material);
			break;
		case 2:
			fin >> length >> width;
			furnituree[i] = new table(num, name, count, length, width);
			break;
		}
	}//从文件读取信息
	fin.close();
	string cal;
	string x;
	cout << "增加或者减少一件指定编号的家具" << endl;
	cout << "输入操作指令:";
	cin >> cal;
	cout << "输入编号:";
	cin >> x;
	for (int i = 0; i < n; i++) {
		furnituree[i]->change(cal, x);
	}//增加或者减少一件指定编号的家具
	int sum = 0;
	cout << "编号" << " 名称          " << "库存 " << "座位数 " << "材质  " << endl;
	for (int i = 0; i < n; i++) {
		if (furnituree[i]->key == 1) {
			furnituree[i]->display();
			sum = sum + furnituree[i]->count;
		}
	}//输出沙发信息
	cout << "沙发总库存为:" << sum << endl;
	int l = 0;
	int flag = 1;
	cout << "输入查找桌子的长度:";
	cin >> l;
	for (int i = 0; i < n; i++) {
		if (furnituree[i]->key == 2){
			if (furnituree[i]->search(l) == 1) {
				if (flag == 1) {
					cout << "编号" << " 名称          " << "库存 " << "长度   " << "宽度  " << endl;
					flag = 0;
				}
				furnituree[i]->display();
			}
		}
	}//输出查找到的桌子信息
	if (flag == 1) {
		cout << "未找到符合条件的桌子" << endl;
	}
	return 0;
}

在这里插入图片描述

实验总结
  1. 注意虚函数的使用
  2. 注意基类和派生类中指针的使用
  3. 注意文件读写时候编码类型
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

闻闻闻闻笛声

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值