纯虚函数

发布时间 2023-06-15 13:39:48作者: 小孟快点跑哇

 

您可能想要在基类中定义虚函数,以便在派生类中重新定义该函数更好地适用于对象,但是您在基类中又不能对虚函数给出有意义的实现,这个时候就会用到纯虚函数。

我们可以把基类中的虚函数 area() 改写如下:

class Shape {

protected:

int width, height;

public:

Shape( int a=0, int b=0)

{

width = a;

height = b;

}

// pure virtual function

virtual int area() = 0;

};

= 0 告诉编译器,函数没有主体,上面的虚函数是纯虚函数。

https://www.121mu.com/mnst202/