Skip to content

Last updated:2026-10-12

C++ Pure Virtual Functions and Abstract Classes

About the Author

WeChat official account:

wechat

1. Pure Virtual Functions and Abstract Classes

A pure virtual function (or abstract function) in C++ is a virtual function that we do not implement! We only declare it! A pure virtual function is declared by assigning 0 in its declaration!

// 抽象类
Class A {
public: 
    virtual void show() = 0; // 纯虚函数
    /* Other members */
}; 

  • Pure virtual function: a virtual function without a function body
  • Abstract class: a class that contains a pure virtual function

An abstract class can only be used as a base class to derive new classes; you cannot create objects of an abstract class. Pointers and references of an abstract class -> objects of classes derived from that abstract class!

Code examples: abstract_base.h, pure_virtual.cpp

2. Implementing an Abstract Class

Inside an abstract class: a pure virtual function may be called from within a member function, but pure virtual functions cannot be used inside constructors/destructors.

If a class is derived from an abstract class, it must implement all the pure virtual functions of the base class before it becomes a non-abstract class.

// A为抽象类
class A {
public:
    virtual void f() = 0;  // 纯虚函数
    void g(){ this->f(); }
    A(){}  // 构造函数
};

class B : public A{
public:
    void f(){ cout<<"B:f()"<<endl;}  // 实现了抽象类的纯虚函数
};

Code example: abstract.cpp

3. Important Points

  • A pure virtual function makes a class abstract

    // 抽象类至少包含一个纯虚函数
    class Base{
    public: 
        virtual void show() = 0; // 纯虚函数
        int getX() { return x; } // 普通成员函数
    
    private:
         int x; 
    }; 
    

  • Pointers and references of abstract class type

    class Derived : public Base { 
    public: 
        void show() { cout << "In Derived \n"; } // 实现抽象类的纯虚函数
        Derived(){} // 构造函数
    }; 
    
    int main(void) 
    { 
        //Base b;  // error! 不能创建抽象类的对象
        //Base *b = new Base(); error!
    
        Base *bp = new Derived(); // 抽象类的指针和引用 -> 由抽象类派生出来的类的对象
        bp->show();
        return 0; 
    }
    

  • If we do not override the pure virtual function in the derived class, the derived class also becomes abstract

    // Derived为抽象类
    class Derived: public Base 
    { 
    public: 
    //    void show() {}
    }; 
    

  • An abstract class can have constructors

    // 抽象类
    class Base { 
        protected: 
            int x; 
        public: 
            virtual void fun() = 0; 
            Base(int i) { x = i; }  // 构造函数
    }; 
    // 派生类
    class Derived: public Base 
    { 
        int y; 
    public: 
        Derived(int i, int j) : Base(i) { y = j; } // 构造函数
        void fun() { cout << "x = " << x << ", y = " << y; }
    }; 
    

  • Constructors cannot be virtual, while destructors can be virtual destructors

    // 抽象类
    class Base  {
    public:
        Base(){ cout << "Constructor: Base" << endl; }
        virtual ~Base(){ cout << "Destructor : Base" << endl; }
    
        virtual void func() = 0;
    };
    
    class Derived: public Base {
    public:
        Derived(){ cout << "Constructor: Derived" << endl; }
        ~Derived(){ cout << "Destructor : Derived" << endl;}
    
        void func(){cout << "In Derived.func()." << endl;}
    };
    

    When a base class pointer points to a derived class object and the object is deleted, we may want the appropriate destructor to be called. If the destructor is not virtual, only the base class destructor gets called.

4. Complete Example

An abstract class is inherited and implemented by its derived classes!

Code example: derived_full.cpp

Comments