形状类的设计与实现(虚函数与抽象类)
Description
定义表示形状的抽象类及相应派生类,并实现相关操作符重载。
(1)定义表示形状的抽象类CShape:
添加double型保护数据成员:rectWidth和rectHeight,分别表示派生类形状对应外接矩形的宽度和高度;
添加公有成员函数void Show(),用于显示形状的宽度、高度和面积,输出格式为“W =宽度, H =高度, Area =面积”;
添加公有成员函数double Area(),用于计算形状面积,该函数定义为纯虚函数;
定义虚的析构函数;
重载比较操作符:==、>和<,用于比较两个形状面积的大小关系,返回值类型为bool,可以定义为成员函数或友元函数。
(2)从形状类CShape派生矩形类CRectangle:
定义带参构造函数;
重定义公有成员函数Area,计算矩形面积= rectWidth*rectHeight。
(3)从形状类CShape派生椭圆类CEllipse:
定义常静态数据成员PI并初始化为3.1416;
定义带参构造函数;
重定义公有成员函数Area,计算椭圆面积=PI*rectWidth*rectHeight/4.0。
(4)从形状类CShape派生菱形类CDiamond:
定义带参构造函数;
重定义公有成员函数Area,计算菱形面积=rectWidth*rectHeight/2.0。
在main函数中,首先根据输入的整数创建相应大小的CShape对象指针数组,再根据输入的对象类型和外接矩形的宽高动态创建相应类型对象,并关联到对象指针数组,输入信息格式如下:
3 //对象指针数组的元素个数
R 23 17 //对象类型、形状宽度、形状高度,R表示矩形对象
E 89 25 //对象类型、形状宽度、形状高度,E表示椭圆对象
D 17 29 //对象类型、形状宽度、形状高度,D表示菱形对象
接着通过调用Show成员函数输出所有对象信息。
然后输出面积相等的形状对象的信息(要求使用重载的运算符“==”来判断对象的面积是否相等),输出格式如下:
Area of Shape[i](形状类型) isequal to Shape[j](形状类型)
最后将所有形状对象按面积从大到小排序(要求使用重载的运算符“>”来判断对象的面积大小关系),并输出排序后的对象信息。
main函数参考测试代码如下:
int main()
{
int num;
int i, j;
vector <CShape *> sarr;
string str;
double w, h;
cin >> num;
for (i = 0; i < num; i++)
{
cin >> str >> w >> h;
if (str == "R")
{
sarr.push_back(new CRectangle(w, h));
}
else if (str == "E")
{
sarr.push_back(new CEllipse(w, h));
}
else if (str == "D")
{
sarr.push_back(new CDiamond(w, h));
}
else
{
cout << "Input error!" << endl;
}
}
num = sarr.size();
for (i = 0; i < num; i++)
{
sarr[i]->Show();
}
for (i = 0; i < num - 1; i++)
{
for (j = i+1; j < num; j++)
{
if ((*sarr[i]) == (*sarr[j]))
{
cout << "Area of Shape[" << i << "]" << GetType(sarr[i]) << " is equal to Shape[" << j << "]" << GetType(sarr[j]) << endl;
}
}
}
sort(sarr.begin(), sarr.end(), compare);
for (i = 0; i < num; i++)
{
sarr[i]->Show();
}
for (i = 0; i < num; i++)
{
delete sarr[i];
}
return 0;
}
Input
对象数目
对象类型对象外接矩形宽度对象外接矩形高度
Output
排序前的对象信息
面积相等的对象信息
排序后的对象信息
Sample Input 1
8 R 23 17 R 89 25 R 17 23 D 25 81 E 29 17 E 89 75 E 17 29 D 23 34
Sample Output 1
W = 23, H = 17, Area = 391 W = 89, H = 25, Area = 2225 W = 17, H = 23, Area = 391 W = 25, H = 81, Area = 1012.5 W = 29, H = 17, Area = 387.202 W = 89, H = 75, Area = 5242.55 W = 17, H = 29, Area = 387.202 W = 23, H = 34, Area = 391 Area of Shape[0](rectangle) is equal to Shape[2](rectangle) Area of Shape[0](rectangle) is equal to Shape[7](diamond) Area of Shape[2](rectangle) is equal to Shape[7](diamond) Area of Shape[4](ellipse) is equal to Shape[6](ellipse) W = 89, H = 75, Area = 5242.55 W = 89, H = 25, Area = 2225 W = 25, H = 81, Area = 1012.5 W = 23, H = 17, Area = 391 W = 17, H = 23, Area = 391 W = 23, H = 34, Area = 391 W = 29, H = 17, Area = 387.202 W = 17, H = 29, Area = 387.202
Sample Input 2
10 R 14.5 32 R 6 1309 E 45 32 D 25 81 R 25 44 E 60 42.5 E 100 100 D 23 34 D 32 61.4 D 44 50
Sample Output 2
W = 14.5, H = 32, Area = 464 W = 6, H = 1309, Area = 7854 W = 45, H = 32, Area = 1130.98 W = 25, H = 81, Area = 1012.5 W = 25, H = 44, Area = 1100 W = 60, H = 42.5, Area = 2002.77 W = 100, H = 100, Area = 7854 W = 23, H = 34, Area = 391 W = 32, H = 61.4, Area = 982.4 W = 44, H = 50, Area = 1100 Area of Shape[1](rectangle) is equal to Shape[6](ellipse) Area of Shape[4](rectangle) is equal to Shape[9](diamond) W = 6, H = 1309, Area = 7854 W = 100, H = 100, Area = 7854 W = 60, H = 42.5, Area = 2002.77 W = 45, H = 32, Area = 1130.98 W = 25, H = 44, Area = 1100 W = 44, H = 50, Area = 1100 W = 25, H = 81, Area = 1012.5 W = 32, H = 61.4, Area = 982.4 W = 14.5, H = 32, Area = 464 W = 23, H = 34, Area = 391
代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
class CShape {
protected:
double rectWidth;
double rectHeight;
public:
CShape(double w, double h) : rectWidth(w), rectHeight(h) {}
virtual ~CShape() {}
void Show() {
cout << "W = " << rectWidth << ", H = " << rectHeight << ", Area = " << Area() << endl;
}
virtual double Area() const = 0;
friend bool operator==(const CShape& a, const CShape& b);
friend bool operator<(const CShape& a, const CShape& b);
friend bool operator>(const CShape& a, const CShape& b);
};
class CRectangle : public CShape {
public:
CRectangle(double w, double h) : CShape(w, h) {}
double Area() const override {
return rectWidth * rectHeight;
}
};
class CEllipse : public CShape {
private:
static const double PI;
public:
CEllipse(double w, double h) : CShape(w, h) {}
double Area() const override {
return PI * rectWidth * rectHeight / 4.0;
}
};
class CDiamond : public CShape {
public:
CDiamond(double w, double h) : CShape(w, h) {}
double Area() const override {
return rectWidth * rectHeight / 2.0;
}
};
const double CEllipse::PI = 3.1416;
bool operator==(const CShape& a, const CShape& b) {
return fabs(a.Area() - b.Area()) < 0.0001;
}
bool operator<(const CShape& a, const CShape& b) {
return a.Area() < b.Area();
}
bool operator>(const CShape& a, const CShape& b) {
return a.Area() > b.Area();
}
bool compare(const CShape* a, const CShape* b) {
return *a > *b;
}
string GetType(const CShape* shape) {
if (dynamic_cast<const CRectangle*>(shape)) {
return "(rectangle)";
}
else if (dynamic_cast<const CEllipse*>(shape)) {
return "(ellipse)";
}
else if (dynamic_cast<const CDiamond*>(shape)) {
return "(diamond)";
}
return "";
}
int main()
{
int num;
int i, j;
vector <CShape *> sarr;
string str;
double w, h;
cin >> num;
for (i = 0; i < num; i++)
{
cin >> str >> w >> h;
if (str == "R")
{
sarr.push_back(new CRectangle(w, h));
}
else if (str == "E")
{
sarr.push_back(new CEllipse(w, h));
}
else if (str == "D")
{
sarr.push_back(new CDiamond(w, h));
}
else
{
cout << "Input error!" << endl;
}
}
num = sarr.size();
for (i = 0; i < num; i++)
{
sarr[i]->Show();
}
for (i = 0; i < num - 1; i++)
{
for (j = i+1; j < num; j++)
{
if ((*sarr[i]) == (*sarr[j]))
{
cout << "Area of Shape[" << i << "]" << GetType(sarr[i]) << " is equal to Shape[" << j << "]" << GetType(sarr[j]) << endl;
}
}
}
sort(sarr.begin(), sarr.end(), compare);
for (i = 0; i < num; i++)
{
sarr[i]->Show();
}
for (i = 0; i < num; i++)
{
delete sarr[i];
}
return 0;
}