可以帮忙看看应该怎么改嘛
这是cpp文件
#include "drawable.h"
#include <iostream>
#include <cmath>
using namespace std;
#define M_PI 3.14159265358979323846
// Rectangle implementation
Rectangle::Rectangle(double l, double w) : length(l), width(w) {}
double Rectangle::getArea() const {
return length * width;
}
void Rectangle::draw() const {
cout << "Drawing a rectangle " << endl;
for (int i = 1; i <= length; ++i) {
for (int j = 1; j <= width; ++j) {
if (i == 1 || i == length || j == 1 || j == width) {
std::cout << "*";
}
else {
std::cout << " ";
}
}
std::cout << std::endl;
}
}
// Circle implementation
Circle::Circle(double r) : radius(r) {}
double Circle::getArea() const {
return M_PI * pow(radius, 2);
}
void Circle::draw() const {
cout << "Drawing a circle with radius " << endl;
cout << " ***** " << endl;
cout << " * * " << endl;
cout << "* * " << endl;
cout << " * * " << endl;
cout << " ***** " << endl;
}
// BMW implementation
void BMW::drive() const {
cout << "Driving a BMW" << endl;
}
void BMW::draw() const {
cout << "Drawing a BMW" << endl;
std::cout << " ______________" << std::endl;
std::cout << " __/ \\_" << std::endl;
std::cout << "/ |" << std::endl;
std::cout << "| \\_" << std::endl;
std::cout << "\\__ BMW _/" << std::endl;
std::cout << " \\___ ___ __/" << std::endl;
std::cout << " \\__/ \\__/" << std::endl;
}
// Mazda implementation
void Mazda::drive() const {
cout << "Driving a Mazda" << endl;
}
void Mazda::draw() const {
cout << "Drawing a Mazda" << endl;
std::cout << " ______________" << std::endl;
std::cout << " __/ \\__" << std::endl;
std::cout << "/ |" << std::endl;
std::cout << "| |" << std::endl;
std::cout << "\\__ Mazda_/" << std::endl;
std::cout << " \\___ ___ __/" << std::endl;
std::cout << " \\__/ \\__/" << std::endl;
}
// Draw function for Shapes and Vehicles
void draw(const Shape& shape) {
shape.draw();
}
void draw(const Vehicle& vehicle) {
vehicle.draw();
}
// Drive function for Vehicles
void drive(const Vehicle& vehicle) {
vehicle.drive();
}
// getTotalArea function
double getTotalArea(Shape** drawable, int size) {
double totalArea = 0.0;
for (int i = 0; i < size; i++) {
totalArea += drawable[i]->getArea();
}
return totalArea;
}
这是h文件
#pragma once
class Drawable {
public:
virtual double getArea() const = 0;
virtual void draw() const = 0;
virtual void drive() const = 0;
};
class Shape : Drawable
{
public:
virtual double getArea() const = 0;
virtual void draw() const = 0;
};
class Rectangle : public Shape {
private:
double length, width;
public:
Rectangle(double l, double w);
double getArea() const override;
void draw() const override;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r);
double getArea() const override;
void draw() const override;
};
class Vehicle : Drawable {
public:
virtual void drive() const = 0;
virtual void draw() const = 0;
};
class BMW : public Vehicle {
public:
void drive() const override;
void draw() const override;
};
class Mazda : public Vehicle {
public:
void drive() const override;
void draw() const override;
};
void draw(const Shape& shape);
void draw(const Vehicle& vehicle);
double getTotalArea(Shape** shapes, int size);
void drive(const Vehicle& vehicle);
这是mian文件
#include <iostream>
#include <cmath>
#include "drawable.h"
using namespace std;
int main() {
// Create two Rectangles and two Circles with different areas
Rectangle rectangle1(5, 10);
Rectangle rectangle2(3, 7);
Circle circle1(2);
Circle circle2(4);
// Draw each shape
draw(rectangle1);
draw(rectangle2);
draw(circle1);
draw(circle2);
// Get total area of all shapes
Shape* shapes[] = { &rectangle1, &rectangle2, &circle1, &circle2 };
int size = sizeof(shapes) / sizeof(shapes[0]);
double totalArea = getTotalArea(shapes, size);
cout << "The total area of all shapes is " << totalArea << endl;
// Create a BMW and a Mazda
BMW bmw;
Mazda mazda;
// Draw each vehicle
draw(bmw);
draw(mazda);
// Drive each vehicle
drive(bmw);
drive(mazda);
return 0;
}