40 lines
641 B
C++
40 lines
641 B
C++
// 创建一个基类 Shape,其中包含一个纯虚函数 draw() ,用于绘制形状。从 Shape 派生出 Rectangle 类和 Circle 类,分别实现不同形状的绘制方法
|
||
#include <iostream>
|
||
#include <cstring>
|
||
#include <cstdlib>
|
||
|
||
using namespace std;
|
||
|
||
class Shape
|
||
{
|
||
public:
|
||
virtual void draw() = 0;
|
||
};
|
||
|
||
class Rectangle : public Shape
|
||
{
|
||
public:
|
||
void draw()
|
||
{
|
||
cout << "Rectangle" << endl;
|
||
}
|
||
};
|
||
|
||
class Circle : public Shape
|
||
{
|
||
public:
|
||
void draw()
|
||
{
|
||
cout << "Circle" << endl;
|
||
}
|
||
};
|
||
|
||
int main()
|
||
{
|
||
Shape *p = new Rectangle;
|
||
p->draw();
|
||
p = new Circle;
|
||
p->draw();
|
||
return 0;
|
||
}
|