10 Difference Between Function Overloading And Function Overriding In C++

SHARE

Function Overloading

When we have multiple functions with the same name but different parameters, then they are said to be overloaded. This technique is used to enhance the readability of the program. There are two ways to overload a function, they are:

  • Having different number of arguments
  • Having different argument types.

Function overloading is normally done when we have to perform one single operation with different number or types of arguments.

Properties Of Function Overloading

  1. Function overloading is a feature that allows us to have same function more than once in a program. Overloaded functions have same name but their signature must be different.
  2. Overloading can be done with or without classes.
  3. The prototyping of overloading is totally dependent on the number of parameters or the same parameter with different datatype.
  4. Overloading is accomplished at compile time.
  5. Function overloading takes place without any inheritance.
  6. Overloading is used to have the same name of the functions which behave differently depending upon parameters passed to them.
  7. In function overloading, function signature should be different for all the overloaded functions.
  8. The overloaded functions are always in the scope.
  9. Overloading yields appropriate results, almost every time depending on the skill of programmer and the real-time application.
  10. Overloading is used to achieve compile time polymorphism.

What Is Function Overriding?

If both the parent and child class have a member function with the same name and 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 what is referred to as Function Overriding.

Properties Of Function Overriding

  1. Function overriding is a feature that allows us to have a same function in child class which is already present in the parent class.
  2. Overriding can only be done in classes.
  3. In overriding, all the prototyping aspects are constant.
  4. Overriding is accomplished at runtime.
  5. Function overriding take place when one class is inherited by the other class. It can’t be done without inheritance.
  6. Overriding is required when the derived class adds information to the same member function of the base class.
  7. Overridden functions must have the very same name and the same number of arguments. In other words, function signatures must be the same.
  8. Overridden functions have a different scope concerning the object of the class.
  9. Overriding if not practiced carefully can produce unwanted results because of different early binding and late binding techniques.
  10. Overriding is used to achieve runtime polymorphism.

Also Read: Difference Between Compile Time And Runtime Polymorphism

Difference Between  Function Overloading And Function Overriding

BASIS OF COMPARISON   FUNCTION OVERLOADING FUNCTION OVERRIDING
Description Function overloading is a feature that allows us to have same function more than once in a program. Overloaded functions have same name but their signature must be different.   Function overriding is a feature that allows us to have a same function in child class which is already present in the parent class.  
Implementation It can be done with or without classes.   It can only be done in classes.  
Prototyping Aspects The prototyping of overloading is totally dependent on the number of parameters or the same parameter with different datatype.   All the prototyping aspects are constant.  
Accomplishment It is accomplished at compile time.   It is accomplished at runtime.  
Inheritance Function overloading takes place without any inheritance.   Function overriding take place when one class is inherited by the other class. It can’t be done without inheritance.  
Use It is used to have the same name of the functions which behave differently depending upon parameters passed to them.   It is required when the derived class adds information to the same member function of the base class.  
Function Signature In function overloading, function signature should be different for all the overloaded functions.   Overridden functions must have the very same name and the same number of arguments. In other words, function signatures must be the same.  
Scope The overloaded functions are always in the scope.   Overridden functions have a different scope concerning the object of the class.  
Advantage/Disadvantage Overloading yields appropriate results, almost every time depending on the skill of programmer and the real-time application.   Overriding if not practiced carefully can produce unwanted results because of different early binding and late binding techniques.  
Goal It is used to achieve compile time polymorphism.   It is used to achieve runtime polymorphism.