设计Shape类及其派生类 实现功能 Area - 面积 Perimeter - 周长 operator<< 序列化 operator>> 读取文件 支持从数据中读取一行行的数据,创建形状图像,追加到对象列表中 类的关系图如下 main() 代码 //文件如下所示: 0, 3, 4, 5 //0-Triangle, 3,4,5 - 边长 1, 2, 3 //1-Rectangle, 2,3-长、宽 2, 5 //2-Square, 5-半径 3, 2 //3-Circle, 2-半径 4, 1, 3 //4-Ellipse, 1,3-长、短轴长 建立一个shape的父类,triangle,rectangle,ellipse的子类,在rectangle下又有square的子类,在ellipse下有circle的子类的完整代码
时间: 2024-02-18 11:06:22 浏览: 63
java代码-9、编写一个类,类名为Rectangle(矩形), * 它有两个整型的变量width(宽)和height(高); * 有一个方法area(),没有参数,返回类型为double,功能是求矩形的面积; * 还有另一个方法为perimeter()没有参数,返回类型为double,功能是求矩形的周长,
下面是实现要求的完整代码:
```cpp
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
#include <cmath>
using namespace std;
class Shape {
public:
virtual double area() = 0;
virtual double perimeter() = 0;
virtual void serialize(ostream& os) = 0;
virtual void deserialize(istream& is) = 0;
virtual ~Shape() {}
};
class Triangle : public Shape {
public:
Triangle(double a, double b, double c) {
this->a = a;
this->b = b;
this->c = c;
}
double area() override {
double p = (a + b + c) / 2;
return sqrt(p * (p - a) * (p - b) * (p - c));
}
double perimeter() override {
return a + b + c;
}
void serialize(ostream& os) override {
os << "0 " << a << " " << b << " " << c << endl;
}
void deserialize(istream& is) override {
is >> a >> b >> c;
}
private:
double a, b, c;
};
class Rectangle : public Shape {
public:
Rectangle(double w, double h) {
this->w = w;
this->h = h;
}
double area() override {
return w * h;
}
double perimeter() override {
return 2 * (w + h);
}
void serialize(ostream& os) override {
os << "1 " << w << " " << h << endl;
}
void deserialize(istream& is) override {
is >> w >> h;
}
private:
double w, h;
};
class Square : public Rectangle {
public:
Square(double a) : Rectangle(a, a) {}
void serialize(ostream& os) override {
os << "2 " << w << endl;
}
void deserialize(istream& is) override {
is >> w;
h = w;
}
};
class Circle : public Shape {
public:
Circle(double r) {
this->r = r;
}
double area() override {
return M_PI * r * r;
}
double perimeter() override {
return 2 * M_PI * r;
}
void serialize(ostream& os) override {
os << "3 " << r << endl;
}
void deserialize(istream& is) override {
is >> r;
}
private:
double r;
};
class Ellipse : public Shape {
public:
Ellipse(double a, double b) {
this->a = a;
this->b = b;
}
double area() override {
return M_PI * a * b;
}
double perimeter() override {
return 2 * M_PI * sqrt((a * a + b * b) / 2);
}
void serialize(ostream& os) override {
os << "4 " << a << " " << b << endl;
}
void deserialize(istream& is) override {
is >> a >> b;
}
private:
double a, b;
};
vector<Shape*> readShapesFromFile(const char* filename) {
vector<Shape*> shapes;
ifstream infile(filename);
string line;
while (getline(infile, line)) {
stringstream ss(line);
int type;
ss >> type;
Shape* shape;
if (type == 0) {
double a, b, c;
ss >> a >> b >> c;
shape = new Triangle(a, b, c);
} else if (type == 1) {
double w, h;
ss >> w >> h;
shape = new Rectangle(w, h);
} else if (type == 2) {
double a;
ss >> a;
shape = new Square(a);
} else if (type == 3) {
double r;
ss >> r;
shape = new Circle(r);
} else if (type == 4) {
double a, b;
ss >> a >> b;
shape = new Ellipse(a, b);
} else {
continue;
}
shapes.push_back(shape);
}
return shapes;
}
int main() {
vector<Shape*> shapes = readShapesFromFile("shapes.txt");
for (Shape* shape : shapes) {
cout << "Area: " << shape->area() << endl;
cout << "Perimeter: " << shape->perimeter() << endl;
shape->serialize(cout);
delete shape;
}
return 0;
}
```
其中,`Shape` 是所有形状的父类,包含了三个纯虚函数分别计算面积、周长和序列化输出。`Triangle`、`Rectangle`、`Square`、`Circle` 和 `Ellipse` 是不同形状的子类,实现了对应的计算和序列化方法。`Rectangle` 和 `Ellipse` 分别有 `Square` 和 `Circle` 的子类。`readShapesFromFile` 函数用来从文件中读取形状数据,创建对应的对象并返回。在 `main` 函数中,我们通过 `readShapesFromFile` 函数读取形状数据,计算并输出对应的面积、周长和序列化结果,最后销毁对象。
阅读全文