请运用c++空间形状抽象类型的封装来完成纯虚函数计算面积、纯虚函数判断形状的类型、分别计算形状的重心、垂心、外心、内心的纯虚函数、纯虚函数计算点和形状的关系:外,内,边上。请将头文件和cpp文件区分出来,并给出相应的方法
时间: 2024-02-07 09:01:53 浏览: 80
头文件 Shape.h:
```c++
#ifndef SHAPE_H
#define SHAPE_H
class Shape {
public:
virtual double area() const = 0;
virtual std::string type() const = 0;
virtual std::pair<double, double> centroid() const = 0;
virtual std::pair<double, double> orthocenter() const = 0;
virtual std::pair<double, double> circumcenter() const = 0;
virtual std::pair<double, double> incenter() const = 0;
virtual std::string pointRelation(double x, double y) const = 0;
};
#endif // SHAPE_H
```
cpp文件 Shape.cpp:
```c++
#include "Shape.h"
std::string Shape::pointRelation(double x, double y) const {
if (area() == 0) {
return "Inside";
}
double s1 = (x - centroid().first) * (orthocenter().first - centroid().first) + (y - centroid().second) * (orthocenter().second - centroid().second);
double s2 = (x - incenter().first) * (circumcenter().first - incenter().first) + (y - incenter().second) * (circumcenter().second - incenter().second);
if (s1 * s2 <= 0) {
return "Outside";
}
double dis = (x - circumcenter().first) * (x - circumcenter().first) + (y - circumcenter().second) * (y - circumcenter().second);
if (dis < (circumcenter().first - incenter().first) * (circumcenter().first - incenter().first) + (circumcenter().second - incenter().second) * (circumcenter().second - incenter().second)) {
return "Inside";
} else if (dis > (circumcenter().first - orthocenter().first) * (circumcenter().first - orthocenter().first) + (circumcenter().second - orthocenter().second) * (circumcenter().second - orthocenter().second)) {
return "Outside";
} else {
return "On the boundary";
}
}
```
其中包括了计算面积、判断形状类型、计算重心、垂心、外心、内心以及点和形状之间的关系。由于这些函数都是纯虚函数,因此需要在继承 Shape 类的具体形状类中进行实现。
阅读全文