Introduction
Inheritance is the ability to inherit the features and functionalities from another class. It supports the reuse of the same code. Inheritance is the most important feature of object-oriented programming. A class that inherits the member functions and functionality from another class is called the derived class, and the class from which the derived class acquires is called the parent class. At the same time, overloading is the ability to use a single identifier to define more than one class method that differs from one another in terms of their input/output parameters. In case of overloading, the method chosen will be selected at the compile time. This blog will discuss whether overloading works with inheritance in different languages?
Click on the following link to read further: Features of C Language
Overloading Methods in Inheritance in C++
Method overloading is a feature that allows a class to have multiple methods having the same name but different parameters.
Let us take an example to examine whether overloading works with inheritance in C++?
Example
#include<iostream>
using namespace std;
// Base class
class Parent {
public:
int increment(int i)
{
cout << "-----Inside Integer Increment function-----"<<endl;
cout<<"Before Value: "<<i<<" After Value: ";
return i+1;
}
};
// Derived class
class Child : public Parent {
public:
double increment(double d)
{
cout << "-----Inside Double Increment function-----"<<endl;
cout<<"Before Value: "<<d<<" After Value: ";
return d+1.3;
}
};
int main()
{
Child* obj = new Child;
cout << obj->increment(10) << '\n';
cout << obj->increment(10.5) << '\n';
}
Expected Output
-----Inside Integer Increment function-----
Before Value: 10 After Value: 11
-----Inside Double Increment function-----
Before Value: 10.5 After Value: 11.8
Actual Output
-----Inside Double Increment function-----
Before Value: 10 After Value: 11.3
-----Inside Double Increment function-----
Before Value: 10.5 After Value: 11.8
You can also practice with the help of Online C++ Compiler
Explanation
There is a difference between the expected output and the actual output because there is no overload resolution between the Base class (Parent class) and the Derived class (Child class). When we call the increment() function from the main, the compiler starts to look for it in the scope of the derived class, when it finds the single function “double increment(double)”, the compiler simply calls it. The compiler never bothers with the scope of the Base (Parent class). This happens because, in C++, there is no overloading across the scopes.
Example
#include<iostream>
using namespace std;
// Base class
class Parent {
public:
int increment(int i)
{
cout << "-----Inside Integer Increment function-----"<<endl;
cout<<"Before Value: "<<i<<" After Value: ";
return i+1;
}
};
// Derived class
class Child : public Parent {
public:
using Parent :: increment;
double increment(double d)
{
cout << "-----Inside Double Increment function-----"<<endl;
cout<<"Before Value: "<<d<<" After Value: ";
return d+1.3;
}
};
int main()
{
Child* obj = new Child;
cout << obj->increment(10) << '\n';
cout << obj->increment(10.5) << '\n';
}
Expected Output
-----Inside Integer Increment function-----
Before Value: 10 After Value: 11
-----Inside Double Increment function-----
Before Value: 10.5 After Value: 11.8
Actual Output
-----Inside Integer Increment function-----
Before Value: 10 After Value: 11
-----Inside Double Increment function-----
Before Value: 10.5 After Value: 11.8
Explanation
There is no difference between the actual and the expected output for the above code as we have created an overload set of all increment() functions from the base and derived class with the help of the “using” declaration.





