c-plus-plus
  1. c-plus-plus-virtual-function

C++ Polymorphism Virtual Function

Polymorphism is one of the fundamental concepts in object-oriented programming. In C++, Polymorphism can be achieved through the use of virtual functions. In this tutorial, we will learn how to use virtual functions to implement Polymorphism in C++ programming.

Syntax

To declare a virtual function in C++, use the virtual keyword followed by the function’s return type and name as shown below:

virtual returnType functionName(parameters);

Example

Consider the following example that demonstrates the use of virtual functions in C++:

#include <iostream>
using namespace std;

class Shape {
    public:
        virtual void draw() {
            cout << "Drawing a Shape." << endl;
        }
};

class Circle : public Shape {
    public:
        void draw() {
            cout << "Drawing a Circle." << endl;
        }
};

class Square : public Shape {
    public:
        void draw() {
            cout << "Drawing a Square." << endl;
        }
};

int main() {
    Shape* s;
    Circle c;
    Square sq;

    s = &c;
    s->draw();

    s = &sq;
    s->draw();

    return 0;
}

Output

The output of the above example will be:

Drawing a Circle.
Drawing a Square.

Explanation

In the above example, we have declared a base class Shape with a virtual function named draw. The class Shape has two derived classes, Circle and Square. Both of these derived classes have their own implementation of the draw function.

In the main function, we have created two objects of derived classes, Circle and Square, and a pointer of base class Shape. We have assigned the address of Circle object to the pointer s and called its draw function using the pointer s. Similarly, we have assigned the address of Square object to the pointer s and called its draw function using the pointer s. Since the draw function is declared as virtual in the base class, the appropriate function corresponding to the type of the object is called.

Use

Virtual functions are mainly used to implement Polymorphism in C++. By using virtual functions, we can define a common interface for a set of related classes and their corresponding functions. This allows us to treat objects of different classes as if they are of the same type, thereby enabling run-time polymorphism.

Important Points

  • In C++, to declare a virtual function, use the virtual keyword followed by the function’s return type and name.
  • The class that contains the virtual function is known as the base class or parent class.
  • The classes that derive from the base class or parent class and implement the virtual function are known as derived classes or child classes.
  • If a virtual function is not overridden in the derived class, the base class implementation of that function will be called.
  • Virtual functions are used to implement Polymorphism, which allows objects of different classes to be treated as if they are of the same type.

Summary

Polymorphism is a fundamental concept in object-oriented programming, and C++ provides support for Polymorphism through virtual functions. In this tutorial, we learned how to use virtual functions to implement Polymorphism in C++ programming. We also learned about the syntax and important points related to virtual functions. By using virtual functions, we can define a common interface for a set of related classes, which enables run-time Polymorphism.

Published on: