下面是实现题目要求的代码:
```c++
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
using namespace std;
class Shape
public:
virtual double getArea() = 0;
class Circle : public Shape
public:
Circle(double r) : radius(r) {}
Circle(const Circle& c) : radius(c.radius) {}
double getArea() { return PI * radius * radius; }
private:
double radius;
static const float PI;
const float Circle::PI = 3.14;
class Rectangle : public Shape
public:
Rectangle(double l, double w) : length(l), width(w) {}
Rectangle(const Rectangle& r) : length(r.length), width(r.width) {}
double getArea() { return length * width; }
private:
double length;
double width;
int main()
Shape* shapes[2];
ifstream fin("data.txt");
if (!fin.is_open())
cout << "Failed to open file!" << endl;
return 0;
char shapeType[20];
double radius, length, width;
for (int i = 0; i < 2; i++)
fin >> shapeType;
if (strcmp(shapeType, "Circle:") == 0)
fin.ignore(256, '=');
fin >> radius;
shapes[i] = new Circle(radius);
else if (strcmp(shapeType, "Rectangle:") == 0)
fin.ignore(256, '=');
fin >> length;
fin.ignore(256, '=');
fin >> width;
shapes[i] = new Rectangle(length, width);
cout << "Invalid shape type!" << endl;
return 0;
double totalArea = 0.0;
for (int i = 0; i < 2; i++)
totalArea += shapes[i]->getArea();
delete shapes[i];
fin.close();
cout.setf(ios::fixed);
cout.precision(4);
cout << "Total area: " << totalArea << endl;
return 0;