Home C++ Tutorial C++ Virtual Functions

C++ Virtual Functions

by Anup Maurya
25 minutes read

Virtual functions are an essential part of object-oriented programming in C++. They enable a derived class to override the implementation of a function defined in its base class. In this tutorial, we will explain the concept of virtual functions and demonstrate how to use them in C++.

What are virtual functions?

A virtual function is a member function of a base class that can be overridden in a derived class. When a derived class defines a function with the same signature as a virtual function in its base class, the derived class’s function is called instead of the base class’s function. This process is called dynamic binding or late binding, and it allows the program to determine at runtime which function to call based on the type of the object.

Here’s an example to illustrate the concept of virtual functions

class Animal {
public:
    virtual void makeSound() {
        std::cout << "This animal makes no sound." << std::endl;
    }
};

class Dog : public Animal {
public:
    void makeSound() override {
        std::cout << "Woof!" << std::endl;
    }
};

int main() {
    Animal* animalPtr = new Dog;
    animalPtr->makeSound(); // Output: Woof!
    delete animalPtr;
    return 0;
}

In this example, the Animal class has a virtual function called makeSound(), which is overridden by the Dog class. In the main() function, we create a pointer to an Animal object and initialize it to a Dog object. When we call the makeSound() function through the pointer, the Dog class’s implementation is called, even though the pointer is of type Animal*.

Syntax of virtual functions

To declare a function as virtual in a base class, we use the virtual keyword in front of the function declaration. Here’s an example,

class Animal {
public:
    virtual void makeSound();
};

To override a virtual function in a derived class, we use the override keyword after the function declaration. Here’s an example,

class Dog : public Animal {
public:
    void makeSound() override;
};

The override keyword is optional, but it’s recommended to use it to ensure that the function is actually overriding a virtual function in the base class. If the function signature doesn’t match any virtual function in the base class, the compiler will generate an error.

Pure virtual functions

A pure virtual function is a virtual function that has no implementation in the base class. It’s declared using the = 0 syntax after the function declaration. Here’s an example,

class Animal {
public:
    virtual void makeSound() = 0;
};

A class that contains at least one pure virtual function is called an abstract class. An abstract class cannot be instantiated because it has one or more functions that have no implementation. However, it can be used as a base class for derived classes, and the derived classes must provide an implementation for all pure virtual functions in the base class.

Final thoughts

Virtual functions are a powerful feature of C++ that enables polymorphism and dynamic binding. They allow us to write code that’s more flexible and maintainable, and they are widely used in real-world software development. When designing a class hierarchy, it’s essential to carefully consider which functions should be virtual and which should not, and to make sure that the functions are correctly overridden in the derived classes.

related posts

Leave a Comment

Enable Notifications OK No thanks