Table of contents
1.
Introduction 
2.
What is Function Overloading?
3.
What is Function Overriding?
4.
Program to Illustrate Function Overloading in C++
4.1.
Code
4.2.
C++
5.
Program to Illustrate Function Overriding in C++
5.1.
Code
5.2.
C++
6.
Difference between Function Overloading and Overriding in C++
7.
Frequently asked questions
7.1.
What is overloading and overriding in C++?
7.2.
What is operator overriding in C++?
7.3.
What is the difference between overriding and polymorphism in C++?
8.
Conclusion
Last Updated: Aug 7, 2024
Easy

Function Overloading and Overriding in C++

Author Shivam Verma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Welcome to our blog on two fundamental concepts in C++ programming: function overloading and overriding. In the realm of object-oriented programming (OOP), these concepts serve as powerful tools for enhancing code readability, reusability, and flexibility. Understanding how to effectively use function overloading and overriding is crucial for every C++ developer aiming to write clean, maintainable, and efficient code.

Function Overloading and Overriding in C++

What is Function Overloading?

Function overloading is a feature of C++. When we use more than one function with the same name and different types of parameters, then it is known as function overloading.

For example, we have two functions, add(int a, int b) and add(double a, double b), which have the same name but different types of parameters.

What is Function Overriding?

In the case of inheritance, when the base class and the derived class have a function with the name, then the function of the derived class overrides the function of the base class, known as function overriding. Function overriding is achieved during runtime. It is a form of runtime polymorphism.

Function Overriding is the redefinition of the base class function in its derived class with the same return type and parameters. 

For example

Class Base_calss
{
    public: 
    virtual void print()
    {
        cout<<"Coding"<<endl;
    }
};
Class Derived_class:public Base_class
{
    public:
    void print()
    { 
        cout<<"Coding Ninjas"<<endl;
    }
};


Also see, Literals in C.

Program to Illustrate Function Overloading in C++

Code

  • C++

C++

#include<bits/stdc++.h>
using namespace std;
int addition(int x,int y)
{
   return x+y;
}
int addition(int x,int y,int z)
{
   return x+y+z;
}
double addition(double x,double y)
{
   return x+y;
}
int main()
{
 int a=10, b=5, c=12;
 double p=5.25, q=15.92;
 cout<<"The value of a+b+c="<<addition(a,b,c)<<endl;
 cout<<"The value of a+b="<<addition(a,b)<<endl;
 cout<<"The value of p+q="<<addition(p,q);
 return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

The value of a+b+c=27
The value of a+b=15
The value of p+q=21.17

 

In the above code, We overload the addition() function. Based on the number and type of arguments passed during the function call, the corresponding addition() function is called.

Program to Illustrate Function Overriding in C++

Code

  • C++

C++

#include<bits/stdc++.h>
using namespace std;
class Base_class
{
   public:
   virtual void print()
   {
       cout<<"This is print() function of Base_calss"<<endl;
   }
   void show()
   {
       cout<<"This is show() function of Base_class"<<endl;
   }
};
class Derived_class:public Base_class
{
   public:
   void print()
   {
       cout<<"This is print() function of Derived_class"<<endl;
   }
};
// Driver code
int main()
{
   Derived_class dc;
   Base_class &bc=dc;
   bc.print();
   dc.show();
   dc.print();
}
You can also try this code with Online C++ Compiler
Run Code

Output

This is print() function of Derived_class
This is show() function of Base_class
This is print() function of Derived_class

 

Try and compile with online c++ compiler.

In the above code, The same function print() is defined in both the Base_class and Derived_class. When we call the function print() from the Derived_class object, "dc", and the base_class object,  "bc", the print() from the Derived_class is invoked and executed by overriding the same function of the Base_class. When we call show() function from the Derived_class object, "dc", the show() function from the Base_class is executed. 

Difference between Function Overloading and Overriding in C++

Sr. No.       

Function Overloading

Function Overriding

1.

In Function Overloading, we declare more than one function with the same name and different types of parameters.In Function Overriding, we declare a function in the base class and the derived class with the same return type and parameters.

2.

Function Overloading can happen without inheritance.Function Overriding can happen only when a class inherits from another class.

3.

Function Overloading is a compile-time polymorphism.Function Overriding is a form of runtime polymorphism. 

4.

In function overloading, the function signature of the overloaded function must be different.In function overriding, the function signature of the overloaded function must be the same.

5.

In function overloading, a function can be overloaded multiple times.In function overriding, a function is overridden in its derived class a single time.

6.

In function overloading, the scope of overloaded functions is the same.In function overriding, the scope of the functions is different.

 

Frequently asked questions

What is overloading and overriding in C++?

Function overloading in C++ allows multiple functions in the same scope with the same name but different parameters. Overriding, on the other hand, occurs in inheritance when a derived class redefines a base class member function.

What is operator overriding in C++?

Operator overriding in C++ enables operators like +, -, *, etc., to be redefined for user-defined types. This allows custom behavior when using these operators with objects of a class.

What is the difference between overriding and polymorphism in C++?

Overriding involves redefining a base class function in a derived class, while polymorphism refers to the ability of a single function name to have multiple implementations based on the object it is called upon. Overriding is a specific aspect of achieving polymorphism in C++.

Conclusion

In this blog, we learned about Function Overloading and Function Overriding. This blog also covers the difference between function overloading and function overriding.

To study more about Function Overloading, refer to this

Recommended Readings:

This blog is over, but learning never stops, and there is a lot more to learn. Have a nice day and Happy Coding! 

Live masterclass