Mostly all members of a family share a common last name and that’s how outsiders recognize them.
But to address the individuals we use first name along with the last name to uniquely identify that individual.
Overloading is quite similar to what we saw above.
Overloading suggests that two or more functions in the same scope having same name, can have different parameters.
The parameters can differ with respect to its number or data type.
But, I can write multiple functions doing the same task with different parameters.
Seriously? Why?
Overloading prevents us from writing multiple functions which perform the same task.
We can overload the function and manipulate the type of parameters that it accepts.
Lets see this in action.
void hello(){
cout<<“Hello“;
}void hello(string name){
cout<<“Hello “<<name;
}
Here, we have two functions with same name, one of which accepts no parameters and another one which accepts one parameter.
Thus, we can say that the second function overloads the first one.
But how to identify which one is called when?
The compiler does that for you.
When you call a function, it checks for the parameters passed, if any, and executes the respective function accordingly.
Let’s try calling the overloaded function.
void hello(){
cout<<“Hello“;
}void hello(string name){
cout<<“Hello “<<name;
}int main() {
hello();
}
The output of the above code is:
Hello
Awesome! But what if we pass a parameter with it?
void hello(){
cout<<“Hello“;
}void hello(string name){
cout<<“Hello “<<name;
}int main() {
hello(“Rahul“);
}
The output of the above written snippet will be
Hello Rahul
Hell yeah! That’s how it is done.
What is Overriding?
Some members of a family have a nickname as the real name is very long or hard to call.
Thus, in this case nickname is being used to identify the same individual.
Overriding aligns perfectly with the above example.
Overriding happens when the function in the derived class has the same name as the function in the base class along with the same parameters.
Remember, the function which overrides the previous one is written in the derived class.
This enables us to provide specifications while implementing the same function in the derived class.
We have already learned about derived classes and inheritance.
It is important to note that overriding takes place when a function declared in the base class is rewritten in a derived class with same parameters.
Let’s see how overriding is implemented.
class Hello{
public:
void hello(){
cout<<“Hello from the Base.“;
}
};
class HelloAgain: public Hello{
public:
void hello(){
cout<<“Hello from the Sub.“;
}
};
Here, HelloAgain is a class inherited from Hello which contains the same function hello() as declared in Hello class.
What do you think when we call the hello() function?
class Hello{
public:
void hello(){
cout<<“Hello from the Base.“;
}
};
class HelloAgain: public Hello{
public:
void hello(){
cout<<“Hello from the Sub.“;
}
};
int main() {
HelloAgain h1;
h1.hello();
}
Can you try to guess the output of the above snippet?
Hello from the Sub.
To Summarize
Overloading is when two or more functions in a class have the same name but different parameters.
Overloading prevents us from writing different multiple functions which perform the same task.
Overriding is when two or more functions in a class have the same name and same parameters.
The function which overrides the previous one is written in the derived class.
Overriding is having two functions with the same name and parameters.
Overloading is when two or more functions in the same scope have same name but different parameters