What is Overloading ?

  • If a single object behaves in many ways, it is known as overloading.
  • It provides different versions of the same function while a single object has the same name.
  • C++ facilitates you to specify more than one definition for an operator or a function in the same scope.
  • Overloading consists of two types, they are:
    • Operator Overloading
    • Function Overloading

Operator Overloading

  • Operator overloading is otherwise known as compile-time polymorphism in which a standard operator is overloaded to provide a user-defined definition to it.
  • ‘+’ operator is overloaded to perform the addition operation on data types such as float, int, etc.
  • In following functions operator overloading can be implemented such as Member function, Non-Member function, Friend Function.

Sample Code

[pastacode lang=”cpp” manual=”using%20namespace%20std%3B%20%20%20%20%0Aclass%20Test%20%20%20%20%0A%7B%20%20%20%20%0A%20%20%20private%3A%20%20%20%20%0A%20%20%20%20%20%20int%20num%3B%20%20%20%20%0A%20%20%20public%3A%20%20%20%20%0A%20%20%20%20%20%20%20Test()%3A%20num(8)%7B%7D%20%20%20%20%0A%20%20%20%20%20%20%20void%20operator%20%2B%2B()%20%20%20%20%20%20%20%20%20%7B%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20num%20%3D%20num%2B2%3B%20%20%20%20%20%0A%20%20%20%20%20%20%20%7D%20%20%20%20%0A%20%20%20%20%20%20%20void%20Print()%20%7B%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20cout%3C%3C%22The%20Count%20is%3A%20%22%3C%3Cnum%3B%20%20%20%20%20%0A%20%20%20%20%20%20%20%7D%20%20%20%20%0A%7D%3B%20%20%20%20%0Aint%20main()%20%20%20%20%0A%7B%20%20%20%20%0A%20%20%20%20Test%20tt%3B%20%20%20%20%0A%20%20%20%20%2B%2Btt%3B%20%20%2F%2F%20calling%20of%20a%20function%20%22void%20operator%20%2B%2B()%22%20%20%20%20%0A%20%20%20%20tt.Print()%3B%20%20%20%20%0A%20%20%20%20return%200%3B%20%20%20%20%0A%7D%20%20%0A” message=”” highlight=”” provider=”manual”/]

Output

Function Overloading

  • Function overloading is 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.
  • In argument list function to be invoked depends on the number of arguments and the type of the arguments.

Sample Code

[pastacode lang=”cpp” manual=”using%20namespace%20std%3B%20%20%20%20%0Aclass%20Cal%20%7B%20%20%20%20%0A%20%20%20%20public%3A%20%20%20%20%0Astatic%20int%20add(int%20a%2Cint%20b)%7B%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20return%20a%20%2B%20b%3B%20%20%20%20%20%20%0A%20%20%20%20%7D%20%20%20%20%20%20%0Astatic%20int%20add(int%20a%2C%20int%20b%2C%20int%20c)%20%20%20%20%20%20%0A%20%20%20%20%7B%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20return%20a%20%2B%20b%20%2B%20c%3B%20%20%20%20%20%20%0A%20%20%20%20%7D%20%20%20%20%20%20%0A%7D%3B%20%20%20%20%20%0Aint%20main(void)%20%7B%20%20%20%20%0A%20%20%20%20Cal%20C%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20%20%20%20%20class%20object%20declaration.%20%20%20%0A%20%20%20%20cout%3C%3CC.add(10%2C%2020)%3C%3Cendl%3B%20%20%20%20%20%20%0A%20%20%20%20cout%3C%3CC.add(12%2C%2020%2C%2023)%3B%20%20%20%20%20%0A%20%20%20return%200%3B%20%20%20%20%0A%7D%20%20%20%20%0A” message=”” highlight=”” provider=”manual”/]

Output

Leave a Reply

Your email address will not be published. Required fields are marked *