Difference Between Function Overloading and Overriding in C++


In objectoriented programming, there are two important concepts of polymorphism namely, function overloading and function overriding.

  • When two or more functions have the same name but their parameters are different, it is called function overloading.

  • On the other hand, function overriding is one that provides a facility to redefine a function with a name and signature same as the inheriting class.

Read this article to learn more about function overloading and overriding in C++ and how they are different from each other.

What is Function Overloading?

The concept by which we can define different function in a class with the same name but with different parameters is known as function overloading. Function overloading takes place during compile time. Therefore, it is also called compiletime polymorphism. Function overloading happens without inheritance.

For example, consider two functions add(float a, float b) and add(int a, int b). Here, the two functions have the same name but different types of parameters.

What is Function Overriding?

Function overriding is the concept that allows two classes to have a function with the same name. Function overriding is accomplished by using inheritance and virtual functions.

As we know every derived class inherits all the functions of its base class, in this case all the member functions of a derived class override the member functions of the base class, hence this is known as the function overriding. Function overriding is achieved during runtime, hence it is also known as runtime polymorphism.

Difference between Function Overloading and Function Overriding

The following are the important differences between function overloading and overriding in C++.

S.No.

Function Overloading

Function Overriding

1.

The concept through which we can define two or more functions with the same name and different numbers and parameters is known as function overloading.

The concept through which we define a function in parent class and the child class with the same return type and parameters is known as function overriding.

2.

It can take place without inheritance.

It can take place only when a class inherited from another class.

3.

It happens during compile time.

It happens during run time.

4.

It is also known as compile time polymorphism.

It is also known as run time polymorphism.

5.

The function overloading can take place multiple times.

Function overriding can take place in the derived class only at once.

6.

Here, the scope of the overloaded functions remain the same.

Here, the scope of the overridden function is different.

7.

No keyword is used during function overloading.

When the function is defined, it is preceded by 'virtual' keyword in main class.

8.

The functions would be redefined with the same name, different number or type of parameters

The same function is redefined in derived class using 'out' keyword

9.

Destructor can't be overloaded.

Destructor can be overridden.

10.

It can be used to achieve early binding.

Overriding is also known as late binding.

Conclusion

The most significant difference between the two concepts is that function overloading does not use inheritance, while function overriding is achieved with the help of inheritance.

Updated on: 21-Feb-2023

20K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements