Function Overriding in C++

What is Function Overriding?

  • The mechanism of defining the function with the same name and signature in both the base class and derived class is known as function overriding.
  • It is used to achieve runtime polymorphism.
  • It enables you to provide specific implementation of functions that are already provided by its base class.

Code:

C++ Code

//Program to demonstrate concept of function overriding.
#include<iostream>

using namespace std;
class XYZ {
  public:
    void display() //overridden function
  {
    cout << "I am in class XYZ";
  }
};
class ABC: public XYZ {
  public: 
     void display() //overriding function
  {
    cout << "I am in class ABC";
  }
};
int main() {
  ABC a1;
  a1.display(); //Here the overriding function of derived class is called.
  return 0;
}

Output:

I am in class ABC

In the above example, the same function display() is defined in both base i.e. class XYZ, and derived class i.e. class ABC. So, when we call display() from the ABC object a1, the display() from ABC is executed by overriding the display() in XYZ. Here if we call the display() function from an object of the base class the function will not be overridden.

Code:

C++ Code

#include<iostream>

using namespace std;
class XYZ {
  public:
    void display() //overridden function
  {
    cout << "I am in class XYZ";
  }
};
class ABC: public XYZ {
  public: 
    void display() //overriding function
  {
    cout << "I am in class ABC";
  }
};
int main() {
  XYZ x1;
  x1.display(); //Here the overriden function of base class is called.
  return 0;
}

Output:

I am in class XYZ

Accessing Overriden Function Of The Base Class

  • We can call the overridden base class function with respect to the derived class objects by using the scope resolution operator.

Note: The overriding function cannot be called with respect to the base class object.

Code:

C++ Code

//Program To Demo Calling Of Overriden Base Class Function With Respect To 
//Derived Class 
//Object Using Scope Resolution Operator
#include<iostream>

using namespace std;
class XYZ {
  public:
    void display() //overridden function
  {
    cout << "I am in class XYZ" << endl;
  }
};
class ABC: public XYZ {
  public: 
    void display() //overriding function
  {
    cout << "I am in class ABC" << endl;
  }
};
int main() {
  ABC a1;
  a1.display();
  a1.XYZ::display();
  return 0;
}

Output:

I am in class ABC

I am in class XYZ

  • We can call the overridden base class function within the derived class using the scope resolution operator.

Code:

C++ Code

//Program To Demo Calling of overridden function within an overridding function 
//using scope resolution operator.
#include<iostream>

using namespace std;
class XYZ {
  public:
    void display() //overridden function
  {
    cout << endl << "I am in class XYZ" << endl;
  }
};
class ABC: public XYZ {
  public: 
    void display() 
  {
    cout << endl << "I am in class ABC";
    XYZ::display();
  }
};
int main() {
  ABC a1;
  a1.display(); //call the overridding function
  return 0;
}

Output:

I am in class ABC

I am in class XYZ

  • We can call the overridden base class function using a pointer of the Base type that points to an object of the Derived class.

Code:

C++ Code

// Program to demo accessing of overridden function using pointer
// of Base type that points to an object of Derived class
#include<iostream>

using namespace std;
class XYZ {
  public:
    void display() //overridden function
  {
    cout << endl << "I am in class XYZ" << endl;
  }
};
class ABC: public XYZ {
  public: 
   void display() 
  {
    cout << endl << "I am in class ABC";
  }
};
int main() {
  ABC a1;
  //ptr is pointer of XYZ(Base) type that points to a1
  XYZ * ptr = & a1;
  //Here we are calling display function of XYZ using ptr
  ptr -> display();
  return 0;
}

Output:

I am in class XYZ

Special thanks to Aakriti Singh for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article