引言:
#include <IOStream>
using namespace std;
class Shape {
public:
virtual void draw() {
cout << "Drawing a shape" << endl;
}
};
class Rectangle : public Shape {
public:
void draw() {
cout << "Drawing a rectangle" << endl;
}
};
class Circle : public Shape {
public:
void draw() {
cout << "Drawing a circle" << endl;
}
};
class Ellipse : public Shape {
public:
void draw() {
cout << "Drawing an ellipse" << endl;
}
};
int mAIn() {
Shape* shapes[3];
shapes[0] = new Rectangle();
shapes[1] = new Circle();
shapes[2] = new Ellipse();
for (int i = 0; i < 3; i++) {
shapes[i]->draw();
}
return 0;
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() = 0;
};
class Rectangle : public Shape {
public:
void draw() {
cout << "Drawing a rectangle" << endl;
}
};
class Circle : public Shape {
public:
void draw() {
cout << "Drawing a circle" << endl;
}
};
class Ellipse : public Shape {
public:
void draw() {
cout << "Drawing an ellipse" << endl;
}
};
int main() {
Shape* shapes[3];
shapes[0] = new Rectangle();
shapes[1] = new Circle();
shapes[2] = new Ellipse();
for (int i = 0; i < 3; i++) {
shapes[i]->draw();
}
return 0;
}