当前位置:网站首页>Western Agricultural University C plus
Western Agricultural University C plus
2022-07-22 06:23:00 【numb and dying】
problem I: oop Internship -9. Abstract classes and operator overloading
Title Description
Define abstract classes and corresponding derived classes that represent shapes , And implement relevant operator overloading .
(1) Define abstract classes that represent shapes Shape:
Add public member function double Area(), Used to calculate shape area ; Defined as a pure virtual function ;
Add public member function void Show(), Used to display shape information , Defined as a pure virtual function ;
Define virtual destructors ;
Overload the comparison operator :==、> and <, Used to compare the size relationship between the areas of two shapes , The return value type is bool, It can be defined as member function or friend function .
(2) From shape class Shape Derived rectangle class Rectangle:
add to double Type of protected data members :rectWidth and rectHeight, Respectively represent the width and height of the rectangle ;
Define a constructor with parameters ;
Redefining public member functions Show, Print the width and height of the rectangle , The output format is “W: Width ; H: Height ; Area: area ”;
Redefining public member functions Area, Calculate rectangular area .
(3) From shape class Shape Derived ellipse class Ellipse:
add to double Type of protected data members :rectWidth and rectHeight, Respectively represents the width and height of the rectangle circumscribed by the ellipse ;
Define a constructor with parameters ;
Redefining public member functions Show, Print the width and height of the rectangle surrounding the ellipse , The output format is “W: Width ; H: Height ; Area: area ”;
Redefining public member functions Area, Calculate the area of the ellipse .
stay main Function , First, create a corresponding size according to the input integer Shape Object pointer array , Then dynamically create objects of corresponding types according to the input object types and information , And associated to the object pointer array . The format of the input information is as follows :
3 // Number of elements of the object pointer array
R 23 17 // object type 、 Shape width 、 Shape height ,R Represents a rectangular object
R 89 25 // object type 、 Shape width 、 Shape height ,R Represents a rectangular object
E 17 29 // object type 、 Shape width 、 Shape height ,E Represents an elliptical object
Then by calling Show The member function outputs the information of all objects .
Then output the information of shape objects with equal area ( Overloaded operators are required “==” To determine whether the area of the object is equal ), The output format is as follows :
Area of Shape[i] is equal to Shape[j]
Finally, all shape objects are sorted by area from large to small ( Overloaded operators are required “>” To judge the size relationship of the area of the object ), And output the sorted object information .
Input
Number of objects
object type The width of the circumscribed rectangle of the object The height of the circumscribed rectangle of the object
Output
Object information before sorting
Object information with equal area
Sorted object information
Tips
#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;
}
边栏推荐
- 剑指 Offer II 015. 字符串中的所有变位词
- 64. Minimum path and
- LeetCode刷题:对称二叉树与二叉树的最大深度
- The orders in the same city are delivered in the same city, and the order explosion is still handy!
- Importance of data center operation and maintenance management skills
- Vector container member function reserve() and iterator failure
- 数据中心线缆管理
- 文档流,盒子模型,外边距及默认样式
- leetcode 1732. Find the highest altitude
- The mobile R & D platform EMAS 3.0 is newly upgraded. Welcome to Alibaba cloud's official website to search EMAS for experience
猜你喜欢
随机推荐
创建私有CA,我就用openSSL
leetcode 1732. Find the highest altitude
Horizontal layout, vertical layout, shadows and fillets
YOLOPose实战:手把手实现单阶段的人体姿态估计+代码解读
Compound modified chitosan hydrogel: preparation of acrylic acid grafting / polyvinyl alcohol / temperature sensitive icariin / aldehyde imine chitosan hydrogel
这次和GrowingIO工程师一起搞事情 | StartDT Hackathon
leetcode 376.摆动序列
国产统信UOS系统运行小程序的探索
The mobile R & D platform EMAS 3.0 is newly upgraded. Welcome to Alibaba cloud's official website to search EMAS for experience
列表标签和超链接
leetcode 1582. Special position in binary matrix
动画,及动画的基本使用
Dom4j解析XML(详解)
机房可视化管理标签自动生成
leetcode 376. Wobble sequence
Western Agricultural University C plus
西农大 C plus
第3集 vmware虚拟机安装最牛B教程(14天)
小道仙博客【开源个人博客】
数据中心运维管理技能的重要性