假设我有一个抽象基类称为Base
和它继承调用Rectangle
其他类(W/C具有X,Y,W,H的属性)C++继承功能默认操作
//Base.h
class Base abstract : public Rectangle
{
public:
Base();
void Show()
{
if (!visible) return;
//draw the stuff here.
}
virtual void PerformTask() = 0;
protected:
bool visible;
bool enable;
//other member variables
};
对于所有继承此Base
的类,它必须实现这个短期操作第一:
void OtherClass1::PerformTask()
{
if (!enable) return; // <- this one I am referring to.
//else, proceed with the overriden operation
//...
}
在
PerformTask()
,可以把它做一个默认操作,因为我不会在所有执行重新键入它,但是,在同一时间,被覆盖并且short operation
被首先执行并保留?
然后不要让'Base :: PerformTask'纯虚拟。 – 0x499602D2 2013-04-21 16:25:22