C++ Virtual Functions

In C++, virtual functions are member functions of a class that are declared with the keyword “virtual”. These functions are intended to be overridden by derived classes. Virtual functions are used to achieve runtime polymorphism, which is the ability of a function to behave differently based on the type of object it is called on. This allows for a more versatile and extensible design, as new classes can be derived from existing ones and customized to suit specific needs.

Example 1: Virtual Function in Base and Derived Class

In this example, a base class “Shape” is defined with a virtual function “getArea”. A derived class “Rectangle” is then defined, which overrides the “getArea” function and provides its own implementation. The main function creates an object of the base class and an object of the derived class, and calls the “getArea” function on both. Since the function is virtual, the call is resolved at runtime and the correct implementation is executed based on the type of object. The output of the program will be:

Area of Shape : 0
Area of Rectangle : 50

Here is the full code:

#include <iostream>
using namespace std;

class Shape {
protected:
    int width, height;
  public:
    Shape(int w = 0, int h = 0) { width = w; height = h; }
    virtual int getArea() { return 0; }
};

class Rectangle: public Shape {
  public:
    Rectangle(int w = 0, int h = 0): Shape(w, h) {}
    int getArea() { return width * height; }
};

int main() {
    Shape* shape;
    Rectangle rect(5, 10);

    shape = &rect;

    cout << "Area of Shape : " << shape->getArea() << endl;

    shape = &rect;
    cout << "Area of Rectangle : " << shape->getArea() << endl;

    return 0;
}

Example 2: Virtual Function and Pure Virtual Function

In this example, a base class “Vehicle” is defined with a pure virtual function “move”. A pure virtual function is a virtual function that has no implementation in the base class and must be overridden by derived classes. A derived class “Car” is then defined, which overrides the “move” function and provides its own implementation. The main function creates an object of the derived class and calls the “move” function on it. Since the function is pure virtual, the call is resolved at runtime and the correct implementation is executed based on the type of object. The output of the program will be:

Car is moving

Here is the full code:

#include <iostream>
using namespace std;

class Vehicle {
  public:
    virtual void move() = 0;
};

class Car: public Vehicle {
  public:
    void move() { cout << "Car is moving" << endl; } }; int main() { Vehicle* v = new Car(); v->move();
   return 0;
}

Example 3: Virtual Function and Virtual Destructors

In this example, a base class “Animal” is defined with a virtual destructor. A derived class “Dog” is then defined, which also has a virtual destructor. The main function creates an object of the base class and an object of the derived class, and calls the destructors on both. Since the destructors are virtual, the correct destructor is executed based on the type of object being destroyed. The output of the program will be:

Animals destructor called
Dog destructor called

Here is the full code:

#include <iostream>
using namespace std;
class Animals {
public:
virtual ~Animals() { cout << "Animals destructor called" << endl; }
};

class Dog: public Animals {
public:
virtual ~Dog() { cout << "Dog destructor called" << endl; }
};

int main() {
Animals* animal = new Dog();
delete animal;

return 0;
}

In conclusion, virtual functions in C++ allow for runtime polymorphism and extensibility in the design of a program. They are declared with the keyword “virtual” and can be overridden by derived classes to provide a custom implementation. Pure virtual functions are virtual functions that have no implementation in the base class and must be overridden by derived classes. Virtual destructors are also important in ensuring the correct destructor is called based on the type of object being destroyed.

Leave a Reply

Your email address will not be published. Required fields are marked *