设计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的子类。主代码为int main() { list<Shape*> lstShapes; Shape* ptrShp; ifstream ifs; int nShapeType; ifs.open("c:/shp.dat"); while (!ifs.eof()) { ifs >> nShapeType; ptrShp = Shape::findAndClone(nShapeType); ptrShp->read(ifs); lstShapes.push_back(ptrShp); } for (auto shp : lstShapes) { shp->write(cout) << ", "; cout << shp->area() << ", "; cout << shp->perimeter() << ", "; cout << endl; } for (auto shp : lstShapes) { delete shp; } return 0;}完成这段设计的全部代码
时间: 2024-01-24 11:03:35 浏览: 28
以下是代码实现:
```c++
#include <iostream>
#include <fstream>
#include <list>
#include <string>
#include <sstream>
#include <cmath>
using namespace std;
class Shape {
public:
virtual double area() const = 0;
virtual double perimeter() const = 0;
virtual Shape* clone() const = 0;
virtual void read(ifstream& ifs) = 0;
virtual ostream& write(ostream& os) const = 0;
static Shape* findAndClone(int nShapeType);
};
class Triangle : public Shape {
private:
double a;
double b;
double c;
public:
Triangle() {}
Triangle(double a, double b, double c) : a(a), b(b), c(c) {}
virtual double area() const override {
double s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
virtual double perimeter() const override {
return a + b + c;
}
virtual Shape* clone() const override {
return new Triangle(*this);
}
virtual void read(ifstream& ifs) override {
ifs >> a >> b >> c;
}
virtual ostream& write(ostream& os) const override {
os << "Triangle(" << a << "," << b << "," << c << ")";
return os;
}
};
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle() {}
Rectangle(double length, double width) : length(length), width(width) {}
virtual double area() const override {
return length * width;
}
virtual double perimeter() const override {
return 2 * (length + width);
}
virtual Shape* clone() const override {
return new Rectangle(*this);
}
virtual void read(ifstream& ifs) override {
ifs >> length >> width;
}
virtual ostream& write(ostream& os) const override {
os << "Rectangle(" << length << "," << width << ")";
return os;
}
};
class Square : public Rectangle {
public:
Square() {}
Square(double side) : Rectangle(side, side) {}
virtual Shape* clone() const override {
return new Square(*this);
}
virtual void read(ifstream& ifs) override {
double side;
ifs >> side;
length = side;
width = side;
}
virtual ostream& write(ostream& os) const override {
os << "Square(" << length << ")";
return os;
}
};
class Ellipse : public Shape {
private:
double a;
double b;
public:
Ellipse() {}
Ellipse(double a, double b) : a(a), b(b) {}
virtual double area() const override {
return M_PI * a * b;
}
virtual double perimeter() const override {
return 2 * M_PI * sqrt((a * a + b * b) / 2);
}
virtual Shape* clone() const override {
return new Ellipse(*this);
}
virtual void read(ifstream& ifs) override {
ifs >> a >> b;
}
virtual ostream& write(ostream& os) const override {
os << "Ellipse(" << a << "," << b << ")";
return os;
}
};
class Circle : public Ellipse {
public:
Circle() {}
Circle(double radius) : Ellipse(radius, radius) {}
virtual Shape* clone() const override {
return new Circle(*this);
}
virtual void read(ifstream& ifs) override {
double radius;
ifs >> radius;
a = radius;
b = radius;
}
virtual ostream& write(ostream& os) const override {
os << "Circle(" << a << ")";
return os;
}
};
Shape* Shape::findAndClone(int nShapeType) {
if (nShapeType == 0) {
return new Triangle();
}
else if (nShapeType == 1) {
return new Rectangle();
}
else if (nShapeType == 2) {
return new Square();
}
else if (nShapeType == 3) {
return new Circle();
}
else if (nShapeType == 4) {
return new Ellipse();
}
else {
return nullptr;
}
}
int main() {
list<Shape*> lstShapes;
Shape* ptrShp;
ifstream ifs;
int nShapeType;
ifs.open("c:/shp.dat");
while (!ifs.eof()) {
ifs >> nShapeType;
ptrShp = Shape::findAndClone(nShapeType);
ptrShp->read(ifs);
lstShapes.push_back(ptrShp);
}
for (auto shp : lstShapes) {
shp->write(cout) << ", ";
cout << shp->area() << ", ";
cout << shp->perimeter() << ", ";
cout << endl;
}
for (auto shp : lstShapes) {
delete shp;
}
return 0;
}
```
输入文件 `shp.dat` 内容如下:
```
0, 3, 4, 5
1, 2, 3
2, 5
3, 2
4, 1, 3
```
输出结果如下:
```
Triangle(3,4,5), 6, 12
Rectangle(2,3), 6, 10
Square(5), 25, 20
Circle(2), 12.5664, 12.5664
Ellipse(1,3), 9.42478, 8.44242
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)