C++ Overloading


  • In C++, it allows you to specify more than one definition for function or operator within a same scope which is known as function overloading and operator overloading respectively.
  • The overloaded declaration means which is declared with the same name as the declaration made in the same scope previously except that both the declarations must have different arguments and different definition.
  • When the function overloading or operator overloading is called the compiler specifies the appropriate definition to be used by comparing the argument types which you have used to call the function or operator with the arguments types specified in the definitions.

There are two types of overloading in C++:

  • Function Overloading
  • Operator Overloading

What is function overloading?

  • In C++, overloading means the use of the same thing for different purposes. Function Overloading is also most important feature of OOPs in C++ in which two or more functions can have the same name but different parameters or arguments for the different tasks.
  • It is known as function polymorphism in OOP. By using the concept of function overloading you can create a many functions with one function name but with different parameters or arguments.

Let us have a look at the syntax:

int test ( ) { }

int test ( int a) { }

int test ( int b, double c) { }

double test ( double a) { }

Let us have a look at the example:

What is operator overloading?

  • In C++, operator overloading is a compile time polymorphism in which the operator is overloaded to provide the special meaning to the user defined data type.
  • It is used to overload the operator in C++ and perform the operation on the user defined data type.

For example:

  • we can overload the ‘+’ operator in a class like String so that we can concatenate two strings by using + and in other classes where arithmetic operators can be overloaded are fractional number, integer, etc.
  • We can overload almost any operator in C++ but there are some of the operators which cannot be overloaded.

Such as:

  • Member selector – (.)
  • Ternary operator – (?:)
  • Scope operator – (::)
  • Member pointer selector – (*)
  • Sizeof

Let us have a look at the example:

Like it? Please Spread the word!