C++ virtual Keyword

The virtual keyword in C++ is used to enable polymorphism in object-oriented programming. When a member function in a base class is declared as virtual, it allows derived classes to override that function and ensures that the correct function is called for an object, regardless of the type of reference or pointer used.

The virtual keyword is essential for achieving dynamic dispatch, where the function to be called is determined at runtime rather than compile time.


Syntax

</>
Copy
class Base {
public:
    virtual return_type function_name(parameters);
};
virtual
A keyword used to declare a function as virtual, enabling it to be overridden in derived classes.
return_type
The type of value the function returns (e.g., void, int).
function_name
The name of the virtual function.
parameters
The list of arguments that the function takes.

Examples for using visual Keyword

Example 1: Basic Virtual Function

This example demonstrates the use of a virtual function in a base class and its override in a derived class.

</>
Copy
#include <iostream>
using namespace std;

class Base {
public:
    virtual void show() {
        cout << "Base class show function" << endl;
    }
};

class Derived : public Base {
public:
    void show() override { // Override the base class function
        cout << "Derived class show function" << endl;
    }
};

int main() {
    Base* basePtr;          // Pointer to base class
    Derived derivedObj;     // Derived class object
    basePtr = &derivedObj;  // Assign derived class object to base pointer

    basePtr->show();        // Calls the derived class function due to virtual
    return 0;
}

Output:

Derived class show function

Explanation:

  1. The show function in the base class is declared as virtual.
  2. The derived class overrides the show function using the override specifier for clarity.
  3. In the main function, a pointer to the base class is used to reference an object of the derived class.
  4. When basePtr->show() is called, the derived class’s show function is invoked, demonstrating polymorphism.

Example 2: Virtual Function in a Hierarchy

This example demonstrates how virtual functions work in a class hierarchy with multiple derived classes.

</>
Copy
#include <iostream>
using namespace std;

class Base {
public:
    virtual void show() {
        cout << "Base class show function" << endl;
    }
};

class Derived1 : public Base {
public:
    void show() override {
        cout << "Derived1 class show function" << endl;
    }
};

class Derived2 : public Base {
public:
    void show() override {
        cout << "Derived2 class show function" << endl;
    }
};

int main() {
    Base* basePtr;
    Derived1 d1;
    Derived2 d2;

    basePtr = &d1;
    basePtr->show(); // Calls Derived1's show function

    basePtr = &d2;
    basePtr->show(); // Calls Derived2's show function

    return 0;
}

Output:

Derived1 class show function
Derived2 class show function

Explanation:

  1. The base class show function is declared as virtual.
  2. Both Derived1 and Derived2 override the show function.
  3. In the main function, the base class pointer basePtr is assigned objects of both Derived1 and Derived2 in sequence.
  4. The correct show function is invoked dynamically based on the type of object being pointed to, demonstrating polymorphism.

Key Points to Remember about the Virtual Keyword

  1. The virtual keyword enables runtime polymorphism by allowing functions to be overridden in derived classes.
  2. When a virtual function is called through a base class pointer or reference, the derived class’s version of the function is executed if it exists.
  3. Declaring a function as virtual ensures dynamic dispatch, meaning the function call is resolved at runtime.
  4. To prevent further overrides, you can declare a function as final in a derived class.
  5. If a base class declares a virtual function, but no override is provided in the derived class, the base class version is used by default.