C++ 如何向派生类添加功能?
我有一个基类C++ 如何向派生类添加功能?,c++,oop,inheritance,C++,Oop,Inheritance,我有一个基类圆圈(简单地画圆圈)和两个派生类:笑脸和皱眉,添加了两个不同的特征,分别是“嘴表情”和“眼睛”。这两个类添加了新功能,覆盖了函数draw_lines()1,其原始函数位于Circle中 // class Smiley class Smiley: public Circle{ public: Smiley(Point c, int rr) : Circle(c,rr), r(rr) { } void draw_lines() const; private: int
圆圈
(简单地画圆圈)和两个派生类:笑脸
和皱眉
,添加了两个不同的特征,分别是“嘴表情”和“眼睛”。这两个类添加了新功能,覆盖了函数draw_lines()
1,其原始函数位于Circle
中
// class Smiley
class Smiley: public Circle{
public:
Smiley(Point c, int rr) : Circle(c,rr), r(rr) { }
void draw_lines() const;
private:
int r;
};
void Smiley::draw_lines() const{
if (color().visibility())
// add the mouth
fl_arc(point(0).x + r/2, point(0).y + r/2, r, r, 180, 360);
// add the eyes
fl_arc(point(0).x + r/2, point(0).y + r/4, r/2, r/2, 0, 360);
fl_arc(point(0).x + 3/2 * r, point(0).y + r/4, r/2, r/2, 0, 360);
// the below is the body of the original function in Circle
if (color().visibility())
fl_arc(point(0).x,point(0).y,r+r,r+r,0,360);
}
// class Frowney
class Frowny: public Circle{
public:
Frowny(Point c, int rr) : Circle(c,rr), r(rr) { }
void draw_lines() const;
private:
int r;
};
void Frowny::draw_lines() const{
if (color().visibility())
// add the mouth
fl_arc(point(0).x + r/2, point(0).y + r, r, r, 0, 180);
// add the eyes
fl_line(point(0).x + r/2 - 5, point(0).y + r/2, point(0).x + r, point(0).y + r/4);
fl_line(point(0).x + r + 5, point(0).y + r/4, point(0).x + 1.5*r, point(0).y + r/2);
if (color().visibility())
fl_arc(point(0).x,point(0).y,r+r,r+r,0,360);
}
现在,应该从类Smiley
和crumpy
派生出两个新类,分别为它们添加特定的单独特性
// class SmileHat
class SmileHat: public Smiley{
public:
SmileHat(Point c, int rr) : Smiley(c, rr), r(rr), center(c) { }
void draw_line() const;
private:
int r;
Point center;
};
类似地,皱眉也是一样,只是帽子的形状不同
问题:
向每个类添加新功能的标准做法是什么Smiley
和Frawney
:
1.重写已经存在(继承)的函数,draw_lines()
(再次)并包含新功能
void SmileHat::draw_line() const{
if (color().visibility())
// add triangle hat
fl_line(center.x - r, center.y + r/2, center.x , center.y + r);
fl_line(center.x + r, center.y + r/2, center.x, center.y + r);
// body of the function override in Smiley
if (color().visibility())
// add the mouth
fl_arc(point(0).x + r/2, point(0).y + r/2, r, r, 180, 360);
// add the eyes
fl_arc(point(0).x + r/2, point(0).y + r/4, r/2, r/2, 0, 360);
fl_arc(point(0).x + 3/2 * r, point(0).y + r/4, r/2, r/2, 0, 360);
// the below is the body of the original function in Circle
if (color().visibility())
fl_arc(point(0).x,point(0).y,r+r,r+r,0,360);
}
2.为新类创建新成员函数并添加新功能
void add_hat() const{
// draw hat
}
3.还有其他更有效的方法吗
注:
我问的问题是考虑到实现继承的概念,我似乎没有利用实现继承,通过连续/重复地重写同一个函数并扩展它,即在每个派生类中都有一个来自基类的显式代码副本,只需添加一个小部分
一,。函数
draw_line()
是类Circle
中的virtual
函数。这就是为什么可以将其覆盖到派生类
可以找到所有其他要编译的文件:。可以找到FLTK。绘制线()方法应声明为虚拟:
virtual void Smiley::draw_lines() const {
//draw smiley implementation here
}
重写draw_lines()并调用base方法绘制笑脸:
virtual void SmileHat::draw_lines() const{
Smiley::draw_lines(); //call base method to draw smiley
//do the hat drawing here
}
要扩展类并添加新功能,可以: 1.重写现有(继承的)函数(在派生类中),添加表示新功能的新代码。就我而言:
void SmileHat::draw_line() const{
// existing old code
Smiley::draw_line();
// newly added feature
if (color().visibility())
// add triangle hat
fl_line(center.x - r, center.y + r/2, center.x , center.y + r);
fl_line(center.x + r, center.y + r/2, center.x, center.y + r);
}
2.定义一个包含要扩展的基本特性的装饰器类,然后从中派生不同的类,每个类向基本特性添加一个特定的扩展。在我的例子中,类层次结构如下所示:
class Circle
|
|
- - - -> class CircleDecorator <- - - -
| | | |
| | | |
class withSmile class withHat1 class withHat2 class withFrown
类圆
|
|
--->class CircleDecorator#1会导致一些不必要的重复。您可以重写方法并在重写的方法内调用基方法以避免重复。继承不是我的选择,但您可以查看draw_lines
声明中的virtual
属性,但不在定义中。(除非它们是相同的,这不在问题中。)我在代码中没有看到任何虚拟方法,但如果你弄明白了就可以了。