qfedu-cpp-level/day6/homework/h5.cpp

40 lines
641 B
C++
Raw Normal View History

2023-08-01 09:07:49 +08:00
// 创建一个基类 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;
}