What is overloading?

  • When a single object behaves in many ways is known as overloading. A single object has the same name, but it provides different versions of the same function.
  • C++ facilitates you to specify more than one definition for a function name or an operator in the same scope. It is called function overloading and operator overloading respectively.
  • Overloading is of two types:
C++ Interview Questions

1. Operator overloading: Operator overloading is a compile-time polymorphism in which a standard operator is overloaded to provide a user-defined definition to it. For example, ‘+’ operator is overloaded to perform the addition operation on data types such as int, float, etc.

Operator overloading can be implemented in the following functions:

  • Member function
  • Non-member function
  • Friend function

Syntax of Operator overloading:

  1. Return_type classname :: Operator Operator_symbol(argument_list)  
  2. {  
  3.       // body_statements;  
  4. }  

2. Function overloading: Function overloading is also a type of compile-time polymorphism which can define a family of functions with the same name. The function would perform different operations based on the argument list in the function call. The function to be invoked depends on the number of arguments and the type of the arguments in the argument list.