#include<iostream>
#include<string>
using namespace std;
class DrawCircle
{
public:
// int radius,x,y; /*定义参数为int radius, int x, int y*/
virtual void drawCircle(int radius, int x, int y) = 0; //(1)
virtual ~DrawCircle()
{
}
};
class RedCircle : public DrawCircle
{
/*绘制红色圆形*/
public:
void drawCircle(int radius, int x, int y)
{
cout << "DrawingCircle: red, radius: " <<radius <<" x: "<< x <<" y: "<< y <<endl;
}
};
class GreenCircle : public DrawCircle
{
/*绘制绿色圆形*/
public:
void drawCircle(int radius, int x, int y)
{
cout << "DrawingCircle: green, radius: " <<radius <<" x: "<< x <<" y: "<< y <<endl;
}
};
class Shape
{
protected:
DrawCircle* drawCircle; //(2)
public:
Shape(DrawCircle* drawCircle)
{
this->drawCircle = drawCircle;
}
virtual ~Shape()
{
}
public:
virtual void draw() = 0;
};
class Circle : public Shape
{
private:
int x, y, radius;
public:
// Circle(int x,int y, int radius, DrawCircle* drawCircle):Shape(drawCircle)
Circle(int x,int y, int radius, DrawCircle* drawCircle):Shape(drawCircle) //(3)
{
this->x = x;
this->y = y;
this->radius = radius;
}
public:
void draw()
{
drawCircle->drawCircle(radius, x, y); //(4)
}
};
int main()
{
Shape *redCircle = new Circle(100,100,20,(new RedCircle())); //(5)
Shape *greenCircle = new Circle(100,100,20,(new GreenCircle())); //(6)
redCircle->draw();
greenCircle->draw();
return 0;
}
答案:
(1) virtual void drawCircle(int radius, int x, int y) = 0;
(2) DrawCircle* drawCircle;
(3) :Shape(drawCircle)
(4) drawCircle(radius, x, y);
(5) new RedCircle()
(6) new GreenCircle()