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

40 lines
641 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 创建一个基类 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;
}