一个派生类序列化的问题

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是我为您编写的代码: ```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>>` 函数,它从文件中读取每个形状对象的信息,并将它们存储在一个向量中。 最后,我们遍历了形状对象的向量,并输出了每个对象的信息、面积和周长,并释放了内存。 希望这个例子对您有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值