Chapter 27. (Im)pure Virtual Functions

Difficulty: 7

Does it ever make sense to make a function pure virtual, but still provide a body?

  1. What is a pure virtual function? Give an example.

  2. Why might you declare a pure virtual function and also write a definition (body)? Give as many possible reasons or situations as you can.

Solution

Solution
  • 1. What is a pure virtual function? Give an example.

A pure virtual function is a virtual function you want to force concrete derived classes to override. If a class has any non-overridden pure virtuals, it is an “abstract” class, and you can't create objects of that type.

 // Example 27-1 // class AbstractClass { // declare ...

Get More Exceptional C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.