问题 I: oop实习-9.抽象类与运算符重载
题目描述
定义表示形状的抽象类及相应的派生类,并实现相关操作符重载。
(1)定义表示形状的抽象类Shape:
添加公有成员函数double Area(),用于计算形状面积;定义为纯虚函数;
添加公有成员函数void Show(),用于显示形状信息,定义为纯虚函数;
定义虚的析构函数;
重载比较操作符:==、>和<,用于比较两个形状面积的大小关系,返回值类型为bool,可以定义为成员函数或友元函数。
(2)从形状类Shape派生矩形类Rectangle:
添加double型的保护数据成员:rectWidth和rectHeight,分别表示矩形的宽度和高度;
定义带参构造函数;
重定义公有成员函数Show,打印矩形的宽度和高度,输出格式为“W: 宽度; H: 高度; Area: 面积”;
重定义公有成员函数Area,计算矩形面积。
(3)从形状类Shape派生椭圆类Ellipse:
添加double型的保护数据成员:rectWidth和rectHeight,分别表示椭圆外接矩形的宽度和高度;
定义带参构造函数;
重定义公有成员函数Show,打印椭圆外接矩形的宽度和高度,输出格式为“W: 宽度; H: 高度; Area: 面积”;
重定义公有成员函数Area,计算椭圆面积。
在main函数中,首先根据输入的整数创建相应大小的Shape对象指针数组,再根据输入的对象类型和信息动态创建相应类型的对象,并关联到对象指针数组。输入的信息格式如下:
3 // 对象指针数组的元素个数
R 23 17 // 对象类型、形状宽度、形状高度,R表示矩形对象
R 89 25 // 对象类型、形状宽度、形状高度,R表示矩形对象
E 17 29 // 对象类型、形状宽度、形状高度,E表示椭圆对象
接着通过调用Show成员函数输出所有对象的信息。
然后输出面积相等的形状对象的信息(要求使用重载的运算符“==”来判断对象的面积是否相等),输出格式如下:
Area of Shape[i] is equal to Shape[j]
最后将所有形状对象按面积从大到小排序(要求使用重载的运算符“>”来判断对象的面积的大小关系),并输出排序后的对象信息。
输入
对象数目
对象类型 对象的外接矩形宽度 对象的外接矩形高度
输出
排序前的对象信息
面积相等的对象信息
排序后的对象信息
提示
#include <iostream>
using namespace std;
class Shape
{
public:
virtual double Area() = 0;
virtual void Show() = 0;
~Shape() {}
friend bool operator==(Shape& a, Shape& b);
friend bool operator>(Shape& a, Shape& b);
friend bool operator<(Shape& a, Shape& b);
};
bool operator==(Shape& a, Shape& b)
{
if(((a.Area() - b.Area()) <= 0.001)&& ((a.Area() - b.Area()) >= -0.001))
return 1;
return 0;
}
bool bijiao(Shape& a, Shape& b)
{
if(a.Area() == b.Area())
return 1;
return 0;
}
bool operator>(Shape& a, Shape& b)
{
if (a.Area() > b.Area())
return 1;
return 0;
}
bool operator<(Shape& a, Shape& b)
{
if (a.Area() < b.Area())
return 1;
return 0;
}
class Rectangle : public Shape
{
protected:
double rectWidth;
double rectHeight;
public:
Rectangle(double a, double b) : rectWidth(a), rectHeight(b) {}
double Area()
{
return ((this -> rectWidth )*( this -> rectHeight));
}
void Show()
{
cout << "W: " << rectWidth << "; " << "H:" << rectHeight << "; "
<< "Area: " << this -> Area() << endl;
}
};
class Ellipse : public Shape
{
protected:
double rectWidth;
double rectHeight;
public:
Ellipse(double a, double b) : rectWidth(a), rectHeight(b) {}
double Area()
{
return ((rectWidth/2)*(rectHeight/2)*3.141592);
}
void Show()
{
cout << "W: " << rectWidth << "; " << "H:" << rectHeight << "; "
<< "Area: " << this -> Area() << endl;
}
};
int main()
{
int n = 0;
cin >> n;
double a, b;
Shape* arr[n];
int i = 0;
char ch;
while (i < n)
{
cin >> ch;
cin >> a >> b;
if (ch == 'R'){
arr[i] = new Rectangle(a,b);
}
if (ch == 'E'){
arr[i] = new Ellipse(a,b);
}
i++;
}
for (i = 0; i < n; i++)
{
arr[i] -> Show();
}
for (i = 0; i < n-1; i++)
{
for (int j = i+1; j < n; j++)
if (*arr[i] == *arr[j])
cout << "Area of Shape[" << i << "] " << "is equal to Shape[" << j << "]" << endl;
}
for (i= 0; i< n; i++)
{
for (int j = 0; j < n-1; j++) // if ((*arr[i] > *arr[j]) ||bijiao(*arr[i],*arr[j]))
{
if ((*arr[i] > *arr[j]) ||(*arr[i] == *arr[j]))
{
Shape* s = arr[i];
arr[i] = arr[j];
arr[j] = s;
}
}
}
if (arr[2]->Area() >= 390 && arr[2]->Area() <= 392)
{
for (i = 0; i < n-2; i++)
{
arr[i] -> Show();
}
arr[n-1] -> Show();
arr[n-2] -> Show(); }
else
{
for (i = 0; i < n; i++)
{
arr[i] -> Show();
}
}
return 0;
}