C++实验10 多态:抽象类

实验十 多态:抽象类

1 实验目的

学习为什么要使用抽象类;
学习通过继承,实现代码重用的机制和方法;
学习如何声明函数为纯虚函数;
学习如何利用纯虚函数,编写一般成员函数。

2 实验内容

(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

源代码

Frame.cpp
#include <iostream>
#include <string>
#include <cmath>
#include "Array.h"

using namespace std;

class Shape {
public:
	virtual double getCircumference() = 0;
	virtual void showInfo() = 0;

	virtual ~Shape() { }
	bool operator >(Shape & shape);

};

bool Shape::operator >(Shape & shape) {
	return (getCircumference() > shape.getCircumference());
}


class Circle: public Shape {
public:
	Circle(double radius) :
			radius(radius) {
	}

	static Shape * createCircle() {
		cout << "请输入圆形的半径:";
		double radius;
		cin >> radius;
		return new Circle(radius);
	}

	double getCircumference() {
		return 2 * radius * PI;
	}

	void showInfo() {
		cout << "圆形,半径" << radius << ",周长" << getCircumference() << endl;
	}

private:
	double radius;
	static const double PI = 3.1415926;
};

class Rectangle: public Shape {
public:
	Rectangle(double length, double width) :
			length(length), width(width) {
	}

	static Shape * createRectangle() {
		cout << "请输入长方形的长和宽:";
		double length, width;
		cin >> length >> width;
		return new Rectangle(length, width);
	}

	double getCircumference() {
		return 2 * (length + width);
	}

	void showInfo() {
		cout << "长方形,长" << length << ",宽" << width << ",周长" << getCircumference() << endl;
	}

private:
	double length;
	double width;
};

class RightTriangle: public Shape {
public:
	RightTriangle(double edge1, double edge2) :
		edge1(edge1), edge2(edge2) {
	}

	static Shape * createRightTriangle() {
		cout << "请输入直角三角形的两条边长:";
		double edge1, edge2;
		cin >> edge1 >> edge2;
		return new RightTriangle(edge1, edge2);
	}

	double getCircumference() {
		return edge1 + edge2 + sqrt(edge1 * edge1 + edge2 * edge2);
	}

	void showInfo() {
		cout << "直角三角形,直角边" << edge1 << ",直角边" << edge2 << ",周长" << getCircumference() << endl;
	}

private:
	double edge1;
	double edge2;
};

void insertSort(Array<Shape *> &shapes, int index, Shape * ptr) {// 将新创建的形状ptr插入到动态数组shapes中
	int size = shapes.getSize();
	int boundary = 0;

	for (; boundary < index; boundary++) {
		if (*shapes[boundary] > *ptr)
			break;
	}

	for (int i = size - 1; i > boundary; i--)
		shapes[i] = shapes[i-1];
	shapes[boundary] = ptr;
}

int main () {
	cout << "输入框架图案的编号,创建新的框架,键入-1输入结束" << endl
			<< "  1  圆形" << endl
			<< "  2  长方形" << endl
			<< "  3  直角三角形" << endl;

	int preSize = 2;// 动态数组的初始大小
	Array<Shape *> shapes(preSize);
	for (int i = 0; i < preSize; i++)	// 要将shapes中的指针元素初始化为NULL,否则有可能是“野指针”
		shapes[i] = NULL;

	int index = 0;
	int option = 0;
	cin >> option;

	while (option != -1) {
		Shape *ptr = NULL;
		switch(option) {
		case 1:
			ptr = Circle::createCircle();
			break;
		case 2:
			ptr = Rectangle::createRectangle();
			break;
		case 3:
			ptr = RightTriangle::createRightTriangle();
			break;
		default:
			cout << "错误的选项,请重新输入" << endl;
			break;
		}

		insertSort(shapes, index, ptr);// 执行插入排序
		index++;

		if (index >= preSize) {// 调整动态数组的大小
			preSize *= 2;
			shapes.resize(preSize);
		}

		cin >> option;// 接收新的输入选项
	}

	for (int i = 0; i < index; i++)
			shapes[i]->showInfo();

	return 0;
}

Array.h
//Array.h
#ifndef ARRAY_H
#define ARRAY_H

#include <cassert>
using namespace std;

//数组类模板定义
template <class T> 
class Array {
private:
	T* list;	//T类型指针,用于存放动态分配的数组内存首地址
	int size;	//数组大小(元素个数)
public:
	Array(int sz = 50);			//构造函数
	Array(const Array<T> &a);	//拷贝构造函数
	~Array();	//析构函数
	Array<T> & operator = (const Array<T> &rhs); //重载"="使数组对象可以整体赋值
	T & operator [] (int i);	//重载"[]",使Array对象可以起到C++普通数组的作用
	const T & operator [] (int i) const;	//"[]"运算符的const版本
	operator T * ();	//重载到T*类型的转换,使Array对象可以起到C++普通数组的作用
	operator const T * () const;	//到T*类型转换操作符的const版本
	int getSize() const;	//取数组的大小
	void resize(int sz);	//修改数组的大小
};

//构造函数
template <class T>
Array<T>::Array(int sz) {
	size = sz;	// 将元素个数赋值给变量size
	list = new T [size];	//动态分配size个T类型的元素空间
}

//析构函数
template <class T>
Array<T>::~Array() {
	delete [] list;
}

//拷贝构造函数
template <class T>
Array<T>::Array(const Array<T> &a) {
	//从对象x取得数组大小,并赋值给当前对象的成员
	size = a.size;
	//为对象申请内存并进行出错检查
	list = new T[size];	// 动态分配n个T类型的元素空间
	//从对象X复制数组元素到本对象  
	for (int i = 0; i < size; i++)
		list[i] = a.list[i];
}

//重载"="运算符,将对象rhs赋值给本对象。实现对象之间的整体赋值
template <class T>
Array<T> &Array<T>::operator = (const Array<T>& rhs) {
	if (&rhs != this) {
		//如果本对象中数组大小与rhs不同,则删除数组原有内存,然后重新分配
		if (size != rhs.size) {
			delete [] list;		//删除数组原有内存
			size = rhs.size;	//设置本对象的数组大小
			list = new T[size];	//重新分配n个元素的内存
		}
		//从对象X复制数组元素到本对象  
		for (int i = 0; i < size; i++)
			list[i] = rhs.list[i];
	}
	return *this;	//返回当前对象的引用
}

//重载下标运算符,实现与普通数组一样通过下标访问元素,并且具有越界检查功能
template <class T>
T &Array<T>::operator[] (int n) {
	return list[n];				//返回下标为n的数组元素
}

template <class T>
const T &Array<T>::operator[] (int n) const {
	return list[n];			//返回下标为n的数组元素
}

//重载指针转换运算符,将Array类的对象名转换为T类型的指针,
//指向当前对象中的私有数组。
//因而可以象使用普通数组首地址一样使用Array类的对象名
template <class T>
Array<T>::operator T * () {
	return list;	//返回当前对象中私有数组的首地址
}

template <class T>
Array<T>::operator const T * () const {
	return list;	//返回当前对象中私有数组的首地址
}

//取当前数组的大小
template <class T>
int Array<T>::getSize() const {
	return size;
}

// 将数组大小修改为sz
template <class T>
void Array<T>::resize(int sz) {
	if (sz == size)	//如果指定的大小与原有大小一样,什么也不做
		return;
	T* newList = new T [sz];	//申请新的数组内存
	int n = (sz < size) ? sz : size;	//将sz与size中较小的一个赋值给n
	//将原有数组中前n个元素复制到新数组中
	for (int i = 0; i < n; i++)
		newList[i] = list[i];
	delete[] list;		//删除原数组
	list = newList;	// 使list指向新数组
	size = sz;	//更新size
}
#endif  //ARRAY_H

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值