Wednesday, December 5, 2018
Home > OOP > Function Overriding in C++

What is Function overriding in c++?

In C++ inheritance allows users to derive one or more classes from a class inheriting characteristics and member functions of its parent class. If both parent and child, classes have function/s with same name and arguments (i.e. same type and equal number). This is function overriding in c++. It uses the concept of rewriting the base class function in the child class. Sometimes it is referred to as method overriding in c++.

What are the advantages of Function overriding in c++?

1- We use Function overriding in runtime polymorphism.

2- Method overriding is used to provide the specific implementation of a function in the child class.

Rules for Method Overriding

1- The function name in the child class should be same as of the parent class.

2- The function in the child class must have same parameters as in parent class.

Below example can help you understand the concept.

Example program: –

#include "stdafx.h"
#include "iostream"
#include"string"

using namespace std;

class person
{
     protected:
          string name;
          string address;
     public:
          person()
          {
               name = "";
               address = "";
          }
     public:
          void getData()
          {
               cout << "Please Enter Name : ";
               getline(cin, name);
               cout << "Please Enter Address : ";
               getline(cin, address);
          }
          void show()
          {
               cout << "Name : " << name << endl;
               cout << "Address : " << address << endl;
          }
};

class student :public person
{
     string regNo;
     float cgpa;
     
     public:
          void getData()
          {
               cout << "Please Enter Registration Number : ";
               getline(cin, regNo);
               cout << "Please Enter cgpa : ";
               cin >> cgpa;
          }
          void show()
          {
               cout << "Registration Number : " << regNo << endl;
               cout << "CGPA : " << cgpa << endl;
          }
};
void main()
{
     person p;
     p.getData();
     p.show();
     student s;
     s.getData();
     s.show();
}

Output: –
function overriding in c++
As we can see in above code that both parent and child classes have getData and show functions. The object for each class calls to its own member function.

Method overriding in c++ Example

We can access parent class function inside child class overridden function as:
Example program: –


#include "stdafx.h"
#include "iostream"
#include"string"

using namespace std;

class person
{
     protected:
          string name;
          string address;
     public:
          person()
          {
               name = "";
               address = "";
          }
     public:
          void getData()
          {
               cout << "Please Enter Name : ";
               getline(cin, name);
               cout << "Please Enter Address : ";
               getline(cin, address);
          }
          void show()
          {
               cout << "Name : " << name << endl;
               cout << "Address : " << address << endl;
          }
};

class student :public person
{
     string regNo;
     float cgpa;

     public:
          void getData()
          {
               person::getData();
               cout << "Please Enter Registration Number : ";
               getline(cin, regNo);
               cout << "Please Enter cgpa : ";
               cin >> 
          }
          void show()
          {
               person::show();
               cout << "Registration Number : " << regNo << endl;
               cout << "CGPA : " << cgpa << endl;
          }
};
void main()
{
     student s;
     s.getData();
     s.show();
}

Output: –
method overriding in c++

 

Note: – Function overriding is different from function overloading.

One thought on “Function Overriding in C++

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.