Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Function Overriding in C++

C++ Function Overriding

In Function Overriding

  • A function defined in the base class is also defined in the derived class with the same signature.
  • It allows the programmer to have a new or specific implementation for derived class objects
  • While at the same time, inheriting data members and other functions from the base class.

Function overriding is a run time phenomenon. 

Let us see with an example on how this works.

Function Overriding in C++

Generic Example

Below is not an example of function overriding but, priority of implementation.

  1. Both attack & defend function are in parent class (Pokemon) & child class (Charizard)
  2. The execution is of child class as object created is of child class will have higher priority.
#include <iostream>
using namespace std;

class Pokemon
{
private:
int weight, height;

public:
void attack()
{
cout<<"Pokemon has attacked" << endl;
}

void defend()
{
cout<<"Pokemon has defended" << endl;
}
};

class Charizard : public Pokemon
{
public:
void attack()
{
cout<<"Charizard attacks and spits fire" << endl;
}

void defend()
{
cout<<"Charizard defends & flies" << endl;
}
};

int main()
{
Charizard charizard;
charizard.attack();
charizard.defend();

return 0;
}

Output –

Charizard attacks and spits fire
Charizard defends & flies

Working with Pointers

Now imagine the above example but in the following scenario –

base *bptr; 
derived d;
bptr = &d;

// for our use case it will be
Pokemon *pokePtr;
Charizard charizard;
pokePtr = &charizard;

Let’s see the function first and then we will comment on what happened and why ?

#include <iostream>
using namespace std;

class Pokemon
{
private:
int weight, height;

public:
void attack()
{
cout<<"Pokemon has attacked" << endl;
}

void defend()
{
cout<<"Pokemon has defended" << endl;
}
};

class Charizard : public Pokemon
{
public:
void attack()
{
cout<<"Charizard attacks and spits fire" << endl;
}

void defend()
{
cout<<"Charizard defends & flies" << endl;
}
};

int main()
{
Pokemon *pokePtr;
Charizard charizard;
pokePtr = &charizard;

//static resolution(Early binding) happens at compile time itself
pokePtr->attack();
pokePtr->defend();

return 0;
}

What exactly happened?

Output –

Pokemon has attacked 
Pokemon has defended

The parent class functions have been called. However, we were expecting the follows –

  • Even though the pointer is of parent class
  • But, the address its pointed to is of child class right?
  • Address is prevalent always, so child class functions must have had been called

But the above didn’t happen why ?

  • The reason is something called as static resolution, the compiler
  • The compiler sets the default function to be called at compile time itself .
  • Compiler chooses base class object and thus the derived class functions can never be called using pointed references between base and derived classes.
  • This is one of many examples of early binding.

How to solve the above issue?

We got backstabbed by the compiler right? It did static resolution at compile time i.e. early binding.

Now, we can force compiler to do late binding at Run time by using compile time polymorphism using virtual keyword.

Function Overriding in C++

Function Overriding using Virtual Keyword

We can force the compiler to override the base class(Parent) function and implement derived class(Child) functions using virtual keyword.

This is a classic example of forcing runtime polymorphism.

  • All you need to do it is add virtual keyword before function name in base class
#include <iostream>
using namespace std;

class Pokemon
{
private:
int weight, height;

//adding virtual keyword below
public:
virtual void attack()
{
cout<<"Pokemon has attacked" << endl;
}

void defend()
{
cout<<"Pokemon has defended" << endl;
}
};

class Charizard : public Pokemon
{
public:
void attack()
{
cout<<"Charizard attacks and spits fire" << endl;
}

void defend()
{
cout<<"Charizard defends & flies" << endl;
}
};

int main()
{
Pokemon *pokePtr;
Charizard charizard;
pokePtr = &charizard;

//function overriding will happen as virtual is added in attack func. in base class
//example of late binding

pokePtr->attack();
//static resolution at compile time will happen as virtual keyword not added
pokePtr->defend();

return 0;
}

Output –

Charizard attacks and spits fire 
Pokemon has defended