#include<iostream>
class Shape { //抽象类
//包含纯虚函数的类叫抽象类
//不能使用抽象类创建对象
//如果类中所有的成员函数都是纯虚函数,那么该类就是纯抽象类--也叫接口类--纯属给子类做接口的类
public:
Shape(int x = 0, int y = 0) :m_x(x), m_y(y) { }
virtual void draw(void) = 0; //纯虚函数
//自定义了接口,没有实现体的虚函数叫纯虚函数
protected:
int m_x;
int m_y;
};
class Rect :public Shape {
public:
Rect(int x, int y, int w, int h) :Shape(x, y), m_w(w), m_h(h) {}
void draw(void) {
std::cout << "绘制矩形:" << m_x << ", " << m_y << ", " << m_w << ", " << m_h << std::endl;
}
private:
int m_w;
int m_h;
};
class Circle :public Shape {
public:
Circle(int x, int y, int r) :Shape(x, y), m_r(r) {}
void draw(void) {
std::cout << "绘制圆形:" << m_x << ", " << m_y << ", " << m_r << std::endl;
}
private:
int m_r;
};
void render(Shape* buf[]) {
for (int i = 0; buf[i] != NULL; ++i) {
buf[i]->draw();
}
}
int main()
{
Shape* buf[1024] = { NULL };
buf[0] = new Rect(100, 100, 200, 300);
buf[1] = new Circle(100, 100, 50);
buf[2] = new Rect(10, 80, 300, 30);
buf[3] = new Circle(10, 150, 500);
render(buf);
return 0;
}