2021-05-30

实验十:
实验目的
学习为什么要使用抽象类;
学习通过继承,实现代码重用的机制和方法;
学习如何声明函数为纯虚函数;
学习如何利用纯虚函数,编写一般成员函数。
实验内容
(1)问题描述
有一家叫“周大框”的公司,用金属线为客户定制各种图案的框架。目前,该公司只生产圆(Circle)、长方形(Rectangle)和直角三角形(Right Triangle)等三种框架。
店铺负责接受客户的订单。客户除了选择图案的种类外,还要给出图案的大小,其中包括:圆形的半径;长方形的长、宽;直角三角形的两个直角边的长度。单位为厘米。
(2)问题要求
设计类的层次。设计一个Shape抽象类;并设计Shape类的派生类:圆Circle类、长方形Rectangle类、直角三角形RightTriangle类。并且周长(Circumference)不能作为类的成员变量,而是通过调用对象的getCircumference成员函数求得,精确到小数点后2位。请考虑如何通过继承,尽可能多的复用代码。
选择框架。设计一个函数,生成框架产品的目录清单。用户可以重复选择多种框架图案,并给出其大小参数,直至输入-1后,选择产品结束。
框架排序。将所选框架保存在一个动态数组中(课本第9章9.2.2小节中介绍的直接访问群体——数组类,Array.h的源代码在网盘上)。在此过程中,使用插入排序算法,将所选的多种产品按周长从大到小进行排序。要求对大于(>)运算符进行重载,并在排序中使用。
(3)结果输出
按照周长从大到小的顺序,输出以下信息。
序号.框架名称,参数,周长。例如:
1.圆,半径5,周长31.40
2.三角形,直角边3,直角边4,周长12

#include"array.h"
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
class Shape  //抽象类
{
public:
	virtual double get_circumference()const = 0;
	virtual void print()const = 0;
};
class Circle :public Shape
{
public:
	Circle(double d = 0) :d(d) {}
	double get_circumference()const { return 3.14*d * 2; }
	void print()const {
		cout << "圆,半径 " << d << ",周长 " << get_circumference()
			<< endl;
	}
private:
	double d;
};
class Rectangle :public Shape
{
public:
	Rectangle(double aa = 0, double bb = 0) :a(aa), b(bb) { }
	double get_circumference() const { return (a + b + a + b); }
	void print()const {
		cout << "长方形,一条边 " << this->a << ",一条边 " << b << ",周长 " << get_circumference()
			<< endl;
	}
private:
	double a, b;  //b一直被初始化为0;
};
class Right_triangle :public Shape
{
public:
	Right_triangle(double aa=0 , double bb=0) :a(aa), b(bb) {}
	double get_circumference() const { return (a + b + sqrt(a*a + b * b)); }
	void print()const {
		cout << "三角形,直角边 " << a << ",直角边 " << b << ",周长 " << get_circumference()
			<< endl;
	}
private:
	double a;
	double b;
};
void importt()
{
	cout << setfill('-') << setw(15) <<" "<< endl;
	cout << "请输入 " << endl;
	cout << "0:圆" << endl;
	cout << "1:长方形" << endl;
	cout << "2:直角三角形 " << endl;
	cout << "-1:完成订单" << endl;
	cout << setfill('-') << setw(15) << " " << endl;
}
int main()
{
	int button=1;
	Array <Shape *>arrayy;
	int i = 1;
	double d = 0.0, a = 0.0, b = 0.0;
	//输入 存储
	while (button != -1)
	{
		importt();
		cin >> button;
		switch (button)
		{
		case 0:cout << "请输入圆的参数" << endl; cin >> d; arrayy[i] = new Circle(d); ++i; break;
		case 1:cout << "请输入长方体的参数" << endl; cin >> a >> b; arrayy[i] =new Rectangle (a,b); ++i; break;
		case 2:cout << "请输入三角形的参数" << endl; cin >> a >> b; arrayy[i] = new Right_triangle (a,b); ++i; break;
		case -1:break;
		default:cout << "输入错误,请重新输入。" << endl;
		}
	}
	int num = i;
	int m;
	//排序
	for (int j =2; j < num; j++)
	{
		arrayy[0] = arrayy[j];   
		m = j-1;
		while (m)
		{
			if ((*arrayy[0]).get_circumference() > (*arrayy[m]).get_circumference())
			{
				arrayy[m+1] = arrayy[m];
				arrayy[m] = arrayy[0];
			}
			m--;
		}
	}
	//输出
	for (int i = 1; i < num; i++)  
	{
		(*arrayy[i]).print();
	}
	for (int i = 1; i < num; i++)
	{
		delete arrayy[i];
	}
	return 0;
}

作业多多,精力满满,加油。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值