eg:实现图形库,用于显示各种图形
图形基类(位置、绘制)
/ \
矩形子类(宽和高、绘制) 圆形子类(半径、绘制)
#include <iostream>
using namespace std;
class Shape{
public:
Shape(int x = 0,int y = 0):m_x(x),m_y(y){
}
virtual void draw(void){
cout << "绘制图形:" << m_x << "," << m_y << endl;
}
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){
cout << "绘制矩形:" << m_x << "," << m_y << "," << m_w
<< "," << m_h << 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){
cout << "绘制圆形:" << m_x << "," << m_y << "," << m_r
<< endl;
}
private:
int m_r;
};
void render(Shape* buf[]){
for(int i=0;buf[i]!=NULL;i++)
buf[i]->draw();
}
int main(void){
Shape* buf[1024] = {
NULL};
buf[0] = new Rect(1,2,3,4);<