AppDividend
Latest Code Tutorials

Function Overriding in C++ Example | C++ Function Overriding

0

Function Overriding in C++ Example | C++ Function Overriding is today’s topic. For understanding function overriding, we need to be aware about the concepts of inheritance. Inheritance has a concept in which we can derive the child class from the parent class(also known as derived and base class). The derived class inherits the properties of a base class. It enables you to provide a specific implementation of the function which is already provided by its base class.

Function Overriding in C++

If both the parent and child class have a member function with the same name and the same number of arguments, then we have to create an object of the child class, and we call the member function present in both the child and parent class with the very same name and an equal number of arguments. Then the member function of the child class is called, and the member function of the base class is ignored. This concept is known as function overriding. We have already seen the Function Overloading in C++ in this blog.

It allows run-time polymorphism.

#Properties of function overriding:

  1. Overriding only occurs when one class is inherited to another class.
    It can’t be done without inheritance.
  2. Overridden functions have a different scope concerning the object of the class.
  3. Overridden functions must have the very same name and the same number of arguments.
  4. Overriding is done when the derived class adds information to the same member function of the base class.

#Syntax of function overriding:

See the following syntax.

class A
{
public:
  void fun_1(int a, int b) //member function fun_1 having two arguments  a & b of integer datatype
  {
    body;
  }
};
class B : public class A //Inheriting the properties of class A to class B
{
public:
  void fun_1(int a, int b) //using the same function name and number       of arguments in the derived class
  {
    body;
  }
};

int main()
{
  B obj;
  obj.fun_1(5, 6); //fun_1 of derived class(class B)   gets called.
}

For accessing the member function of the base class or the parent class, we can use the scope resolution operator. I have written an example code for the same below.

#Examples of function overriding in C++

Example: 1

Create a member function to add two numbers and show the mechanism of function overriding.

See the following code file.

#include <iostream>
using namespace std;
class A
{
public:
  int add(int x, int y, int z)
  {
    return (x + y + z);
  }
};
class B : public A
{
public:
  int add(int x, int y, int z)
  {
    cout << "The first number: " << x << endl;
    cout << "The second number:" << y << endl;
    cout << "The third number:" << z << endl;
    return (x + y);
  }
};
int main()
{
  B obj;
  int q, w, r;
  cout << "Enter 3 numbers for showing function overriding: ";
  cin >> q >> w >> r;

  cout << "The number retuns the sum of only two numbers(Function Overriding): " << obj.add(q, w, r) << endl;
  return (0);
}

See the following output.

 

Function Overriding in C++

In this program, I’ve created two add function with the same name and the same number of arguments in one function. I have used three variables for addition, and in the second function, I’ve used two variables, for addition.

Example 2:

Write a program to find the area of a rectangle by passing length and breadth as arguments after creating member function in both derived and base class with the same name.

See the following code example.

#include <iostream>
using namespace std;
class A
{
public:
  void area(int length, int breadth)
  {
    cout << "The length is: " << length << endl;
    cout << "The breadth is:" << breadth << endl;
    cout << "Area from base class is:" << length * breadth << endl;
  }
};
class B : public A
{
public:
  void area(int length, int breadth)
  {
    cout << "The length is: " << length << endl;
    cout << "The breadth is:" << breadth << endl;
    cout << "Area from the derived class is:" << length * breadth << endl;
  }
};
int main()
{
  B obj;

  obj.area(5, 6);
  return (0);
}

See the following output.

 

C++ Function Overriding

In this, we have created area member function and passed length and breadth as arguments and shown the results as output, which is derived from the child class.

Example 3:

Write a program to access the base class member function after the occurrence of function overriding. (Using scope resolution operator).

See the following code example.

#include <iostream>
using namespace std;
class A
{
public:
  void base_derived()
  {
    cout << "I am the base class member function with the same name as  member function of derived class" << endl;
  }
};
class B : public A
{
public:
  void base_derived()
  {
    cout << "I am the derived class member function" << endl;
    A::base_derived();
  }
};
int main()
{
  B obj;

  obj.base_derived();
  return (0);
}

See the following output.

 

C++ Function Overriding Example Tutorial

In this program, I’ve used the scope resolution operator to let the user know that the data is taken from the base class member function even after function overriding.

#Function Overloading VS Function Overriding

#Inheritance

Function Overriding occurs when a class is inherited from the another class. Function Overloading occurs without inheritance.

#Function Signature

Function Overloading must differ in the function signature. For example, either the number of parameters or type of parameters should differ. In overriding, function signatures must be the same.

#Scope of functions

Overridden functions are in the different scopes; whereas overloaded functions are in the same scope.

#Behavior of functions

Overriding is needed when derived class function has to do some added or different job than the base class function. Overloading is used to have the same name functions which behave differently depending upon parameters passed to them.

Finally, Function Overriding in C++ Example | C++ Function Overriding is over.

Leave A Reply

Your email address will not be published.

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