纯虚函数
#include "iostream.h"
class shape
{
public:
virtual void draw()=0;
};
class circle:public shape
{
void draw()
{
cout<<"draw"<<endl;
}
};
shape* GetShape()
{
return new circle;
}
void main()
{
shape* pshape = GetShape();
pshape->draw();
}
本文通过一个具体的C++代码示例介绍了纯虚函数的概念及应用。定义了一个抽象基类shape,其中包含一个纯虚函数draw(),并在派生类circle中实现了该函数。通过指向基类的指针调用成员函数展示多态性。
990

被折叠的 条评论
为什么被折叠?



