Pure Virtual Functions

Advantages of Pure Virtual Functions

What are the advantages and disadvantages of using pure virtual functions?

Abstract art representing computer programming

Pure virtual functions have a few key use cases:

1. Enforce Interface Implementation

Pure virtual functions ensure that derived classes implement specific functions.

This enforces a contract, making it clear what functionality must be provided by any class that inherits from the base class.

2. Design Flexibility

They allow you to define a common interface for a set of classes without providing a specific implementation in the base class.

This gives derived classes the flexibility to implement the details.

3. Encapsulation of Common Functionality

Abstract classes can contain non-virtual functions that provide common functionality, while pure virtual functions define the interface.

This helps in keeping the shared code centralized and reduces duplication.

4. Polymorphism

Pure virtual functions enable polymorphic behavior, allowing you to write code that can operate on a base class reference or pointer but execute derived class methods.

Disadvantages of Pure Virtual Functions

  1. Increased Complexity: They can increase the complexity of the class hierarchy. Managing multiple derived classes that implement various interfaces can become challenging.
  2. Difficulty in Refactoring: Changing the interface of a pure virtual function requires changes in all derived classes, which can be cumbersome if there are many subclasses.
  3. Cannot Instantiate Base Class: You cannot instantiate an abstract class, which might be limiting if you want to create objects of the base class directly for some reason.

Example

Here’s an example to illustrate the use of pure virtual functions:

#include <iostream>
#include <string>

// Abstract base class with a pure virtual function
class Animal {
public:
  virtual void MakeSound() = 0; 
};

class Dog : public Animal {
public:
  void MakeSound() override {
    std::cout << "Bark!\n";
  }
};

class Cat : public Animal {
public:
  void MakeSound() override {
    std::cout << "Meow!\n";
  }
};

int main() {
  Dog dog;
  Cat cat;

  dog.MakeSound(); 
  cat.MakeSound(); 
}
Bark!
Meow!

In this example:

  • Animal is an abstract class with a pure virtual function MakeSound().
  • Dog and Cat provide implementations for the MakeSound() function, enforcing the contract.

Pure virtual functions are a powerful feature in C++ that help enforce design contracts, enable polymorphism, and provide design flexibility, despite their complexity.

This Question is from the Lesson:

Pure Virtual Functions

Learn how to create interfaces and abstract classes using pure virtual functions

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Pure Virtual Functions

Learn how to create interfaces and abstract classes using pure virtual functions

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved