Published on

C++ Virtual Functions

Why do we need virtual functions in C++?

Let's say you have these two classes:

class Animal {
    public:
        void eat(){ 
            cout << "I am eating generic food.";
        }
};

class Cat : public Animal {
    public:
        void eat(){
            cout << "I am eating a rat.";
        }
}

In your main function:

Animal* animal = new Animal;
Cat* cat = new Cat;

animal->eat();  // I'm eating generic food
cat->eat();     // I'm eating a rat

Let's change it a little now so that eat() is called via an intermediate function

void func(Animal *xyz) {
    xyz->eat();
}

Now our main function is:

Animal* animal = new Animal;
Cat* cat = new Cat;

func(animal);   // I'm eating generic food
func(cat);      // I'm eating generic food

The solution is to make eat() from the Animal class a virtual function.

references: https://stackoverflow.com/questions/2391679/why-do-we-need-virtual-functions-in-c