Can virtual functions have default implementations in C++?

Is it possible to provide a default implementation for a virtual function in a base class?

Yes, virtual functions in C++ can have default implementations in the base class. This allows you to provide a fallback behavior for derived classes that don't override the virtual function.

When a virtual function has a default implementation in the base class, derived classes have the option to override it if they need specific behavior. If a derived class doesn't override the virtual function, the default implementation from the base class will be used.

Here's an example:

#include <iostream>

class Monster {
 public:
  virtual void attack() {
    std::cout << "Monster attacks!\n"; }  
};

class Dragon : public Monster {
 public:
  void attack() override {
    std::cout << "Dragon breathes fire!\n"; }
};

class Orc : public Monster {
  // Doesn't override attack()
};

int main() {
  Monster* monster = new Monster();
  Monster* dragon = new Dragon();
  Monster* orc = new Orc();

  monster->attack();
  dragon->attack();
  orc->attack();  

  delete monster;
  delete dragon;
  delete orc;
}
Monster attacks!
Dragon breathes fire!
Monster attacks!

In this example, the Monster class provides a default implementation for the attack() function. The Dragon class overrides the attack() function with its own implementation, while the Orc class doesn't override it.

When orc->attack() is called, since the Orc class doesn't have its own implementation, the default implementation from the Monster class is used.

Providing default implementations for virtual functions can be useful in several scenarios:

  1. It allows you to provide a fallback behavior for derived classes that don't need to override the function.
  2. It can serve as a base implementation that derived classes can extend or modify using the override keyword.
  3. It enables you to define a common behavior for all derived classes, which can be overridden if needed.

However, it's important to note that if a virtual function is intended to be purely abstract and must be implemented by derived classes, you should declare it as a pure virtual function instead of providing a default implementation.

Run-time Polymorphism

Learn how to write flexible and extensible C++ code using polymorphism, virtual functions, and dynamic casting

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Why should I use a virtual destructor in C++?
I noticed in the lesson that a virtual destructor was used in a base class. Why is this necessary?
What is the performance impact of using virtual functions in C++?
The lesson mentions that using virtual functions has performance implications. Can you explain what those are?
What are pure virtual functions in C++?
I came across the term "pure virtual function" while learning about polymorphism. What does it mean and how is it used?
What is the performance impact of using dynamic_cast in C++?
The lesson mentions using dynamic_cast for downcasting. Is there any significant performance overhead associated with dynamic_cast?
How do virtual functions work with multiple inheritance in C++?
If a class inherits from multiple base classes that have virtual functions, how are the virtual function calls resolved?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant