Sobes.tech
Junior — Middle

What is meant by a virtual function that has no implementation in the base class?

sobes.tech AI

Answer from AI

In C++, a virtual function without an implementation in the base class is called a pure virtual function. It is declared using the syntax = 0 in the function declaration:

class Base {
public:
    virtual void foo() = 0; // pure virtual function
};

This means that the base class does not provide an implementation for this function, and any derived class must implement it, otherwise it also becomes abstract.

Such a function is used to define an interface that inheriting classes must implement. Classes with at least one pure virtual function are called abstract and cannot be instantiated directly.

Example:

class Shape {
public:
    virtual void draw() = 0; // pure virtual function
};

class Circle : public Shape {
public:
    void draw() override {
        // implementation of circle drawing
    }
};

Here, Shape is an abstract class defining an interface, and Circle implements specific behavior.

What is meant by a virtual function that has no… - sobes.tech