Sobes.tech
Junior — Middle

What happens when a function declared as pure virtual is called, and how does it affect program execution?

sobes.tech AI

Answer from AI

A pure virtual function in C++ is a function declared with = 0 in a base class, for example:

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

It has no implementation in the base class and requires derived classes to override it.

If a pure virtual function is called directly by mistake (for example, through a pointer or reference to a base class object that has no implementation), the program's behavior is undefined. Usually, the function __cxa_pure_virtual is called, and the program terminates abnormally.

Thus, a pure virtual function is used to create abstract classes and interfaces, and calling it without an implementation results in a runtime error.