.设计Shape类及其派生类 实现功能 Area-面积 Perimeter-周长 operator<<序列化 operator>>读取文件 支持从数据中读取一行行的数据,创建形状图像,追加到对象列表中,有shape类为父类,rectangle,triangle,ellispe为继承类,ellispe下有circle类,长方形类下有正方形类的代码
时间: 2024-02-13 15:03:15 浏览: 126
java代码-使用java解决形状类,正方形类,长方形类,圆类,周长面积的源代码
好的,以下是我为您编写的代码:
```c++
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <cmath>
using namespace std;
class Shape {
public:
virtual double area() const = 0;
virtual double perimeter() const = 0;
virtual string to_string() const = 0;
virtual void serialize(ofstream& file) const = 0;
};
class Rectangle : public Shape {
private:
double width, height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
double area() const override { return width * height; }
double perimeter() const override { return 2 * (width + height); }
string to_string() const override {
stringstream ss;
ss << "Rectangle, width = " << width << ", height = " << height;
return ss.str();
}
void serialize(ofstream& file) const override {
file << "Rectangle " << width << " " << height << "\n";
}
};
class Square : public Rectangle {
public:
Square(double s) : Rectangle(s, s) {}
string to_string() const override {
stringstream ss;
ss << "Square, side = " << Rectangle::width;
return ss.str();
}
void serialize(ofstream& file) const override {
file << "Square " << Rectangle::width << "\n";
}
};
class Triangle : public Shape {
private:
double a, b, c;
public:
Triangle(double aa, double bb, double cc) : a(aa), b(bb), c(cc) {}
double area() const override {
double s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
double perimeter() const override { return a + b + c; }
string to_string() const override {
stringstream ss;
ss << "Triangle, sides = " << a << ", " << b << ", " << c;
return ss.str();
}
void serialize(ofstream& file) const override {
file << "Triangle " << a << " " << b << " " << c << "\n";
}
};
class Ellipse : public Shape {
protected:
double major_axis, minor_axis;
public:
Ellipse(double major, double minor) : major_axis(major), minor_axis(minor) {}
double area() const override { return M_PI * major_axis * minor_axis; }
double perimeter() const override {
return 2 * M_PI * sqrt((major_axis * major_axis + minor_axis * minor_axis) / 2);
}
string to_string() const override {
stringstream ss;
ss << "Ellipse, major axis = " << major_axis << ", minor axis = " << minor_axis;
return ss.str();
}
void serialize(ofstream& file) const override {
file << "Ellipse " << major_axis << " " << minor_axis << "\n";
}
};
class Circle : public Ellipse {
public:
Circle(double r) : Ellipse(r, r) {}
string to_string() const override {
stringstream ss;
ss << "Circle, radius = " << major_axis;
return ss.str();
}
void serialize(ofstream& file) const override {
file << "Circle " << major_axis << "\n";
}
};
int main() {
vector<Shape*> shapes;
shapes.push_back(new Rectangle(3, 4));
shapes.push_back(new Square(5));
shapes.push_back(new Triangle(3, 4, 5));
shapes.push_back(new Ellipse(2, 3));
shapes.push_back(new Circle(4));
// Serialize shapes to file
ofstream file("shapes.txt");
for (Shape* shape : shapes) {
shape->serialize(file);
}
file.close();
// Read shapes from file
ifstream infile("shapes.txt");
string line;
while (getline(infile, line)) {
stringstream ss(line);
string type;
ss >> type;
if (type == "Rectangle") {
double width, height;
ss >> width >> height;
shapes.push_back(new Rectangle(width, height));
}
else if (type == "Square") {
double side;
ss >> side;
shapes.push_back(new Square(side));
}
else if (type == "Triangle") {
double a, b, c;
ss >> a >> b >> c;
shapes.push_back(new Triangle(a, b, c));
}
else if (type == "Ellipse") {
double major, minor;
ss >> major >> minor;
shapes.push_back(new Ellipse(major, minor));
}
else if (type == "Circle") {
double radius;
ss >> radius;
shapes.push_back(new Circle(radius));
}
}
infile.close();
// Print results
for (Shape* shape : shapes) {
cout << shape->to_string() << endl;
cout << "Area: " << shape->area() << endl;
cout << "Perimeter: " << shape->perimeter() << endl;
}
// Deallocate memory
for (Shape* shape : shapes) {
delete shape;
}
return 0;
}
```
在这个代码中,我们定义了一个 `Shape` 类作为基类,并且它包含了三个纯虚函数 `area()`、`perimeter()`、`to_string()` 和一个纯虚函数 `serialize()`。然后我们定义了 `Rectangle`、`Square`、`Triangle`、`Ellipse` 和 `Circle` 作为 `Shape` 的派生类,它们都重载了基类的纯虚函数。在 `Rectangle` 和 `Square` 中,我们使用了基类的构造函数,并重载了部分函数;在 `Triangle`、`Ellipse` 和 `Circle` 中,我们定义了新的变量,并重载了所有函数。
我们还实现了 `serialize()` 函数,它将每个形状对象写入文件中。然后我们实现了 `operator>>` 函数,它从文件中读取每个形状对象的信息,并将它们存储在一个向量中。
最后,我们遍历了形状对象的向量,并输出了每个对象的信息、面积和周长,并释放了内存。
希望这个例子对您有帮助!
阅读全文