Function overloading and function overriding are two important features of polymorphism in C++. Function overloading allows multiple functions with the same name but different parameters, whereas function overriding allows a derived class to redefine a base class function with the same signature.
- Function overloading is an example of compile-time polymorphism.
- Function overriding is an example of runtime polymorphism.
Function Overloading
Function overloading allows multiple functions to have the same name but different parameter lists. The compiler determines which function to call based on the number and type of arguments passed.
- Functions must have the same name but different parameter lists.
- Return type alone cannot differentiate overloaded functions.
- It is resolved at compile time.
- It can be implemented within the same class or scope.
#include <iostream>
using namespace std;
// overloaded functions
void test(int);
void test(float);
void test(int, float);
int main()
{
int a = 5;
float b = 5.5;
// Overloaded functions
// with different type and
// number of parameters
test(a);
test(b);
test(a, b);
return 0;
}
// Method 1
void test(int var)
{
cout << "Integer number: " << var << endl;
}
// Method 2
void test(float var)
{
cout << "Float number: "<< var << endl;
}
// Method 3
void test(int var1, float var2)
{
cout << "Integer number: " << var1;
cout << " and float number:" << var2;
}
Output
Integer number: 5 Float number: 5.5 Integer number: 5 and float number:5.5
Explanation
- Three functions with the same name test() are defined.
- Each function differs in the number or type of parameters.
- The compiler selects the appropriate function during compilation.
Function Overriding
Function overriding occurs when a derived class provides a new implementation for a function already defined in its base class using the same function signature.
- It requires inheritance between the base and derived classes.
- The function signature must remain the same in both classes.
- The base class function is generally declared using virtual.
- Function calls are resolved at runtime through dynamic binding.
#include<iostream>
using namespace std;
class BaseClass
{
public:
virtual void Display()
{
cout << "\nThis is Display() method"
" of BaseClass";
}
void Show()
{
cout << "\nThis is Show() method "
"of BaseClass";
}
};
class DerivedClass : public BaseClass
{
public:
// Overriding method - new working of
// base class display method
void Display()
{
cout << "\nThis is Display() method"
" of DerivedClass";
}
};
// Driver code
int main()
{
DerivedClass dr;
BaseClass &bs = dr;
bs.Display();
dr.Show();
}
Output
This is Display() method of DerivedClass This is Show() method of BaseClass
Explanation
- Display() is declared as a virtual function in the base class.
- The derived class overrides Display() with its own implementation.
- Runtime polymorphism ensures that the derived class version is executed.
Difference Between Function Overloading and Function Overriding
| Function Overloading | Function Overriding |
|---|---|
| Provides multiple definitions of a function by changing its parameter list. | Redefines a base class function in the derived class using the same signature. |
| Example of compile-time polymorphism. | Example of runtime polymorphism. |
| Function signatures must be different. | Function signatures must be identical. |
| Functions exist in the same scope. | Functions exist in different scopes. |
| Does not require inheritance. | Requires inheritance. |
| Resolved during compilation. | Resolved during program execution. |
| Used when the same operation must behave differently for different parameters. | Used when a derived class needs different behavior than the base class implementation. |