Introduction to pure virtual functions and abstract classes

Basic Concepts and Understanding

A pure virtual function is a virtual function declared in a base class (parent class), which is not defined in the base class, requiring any derived class (subclass) to have its own version.

An abstract base class means that it contains at least one pure virtual member function.

The description form of pure virtual function: virtual type function name (parameter table) = 0; 0
at this time means that the assignment of the pointer to the function body is 0;

class point{......};
class xxx;  //抽象类
{pointer center;
..........
public:
    virtual void rotate(int) = 0;//纯虚函数
    virtual void draw()=0;//纯虚函数                

xxx x;//纯虚函数不能建立对象
xxx *p;//可以声明抽象类的指针
xxx f();//抽象类是不能作为返回类型的
void g(xxx);//抽象类是不能作为参数的

虽然不能声明抽象类的实例,也不能将其作为参数类型,函数返回类型或显示转换类型,但是可以**声明抽象类的指针和引用**。

Classes derived from abstract classes must redefine pure virtual functions.
For C++, there is no interface like java.
C++ does not have an interface, so we can think of an abstract base class as an interface.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325485694&siteId=291194637