Tuesday, 28 June 2016

Whether const and volatile keyword is considered in the function overloading?

 
Guess the following output?

#include<iostream>

class FunctionOverloading
{

   public:
      int a;
      void callFunctionOverloading()
      {                         
         std::cout<<"Called Normal Function";
      }
 
  void callFunctionOverloading()const
      {
         std::cout<<"Called const Function Overloading";
      }
 
  void callFunctionOverloading()volatile
      {
         std::cout<<"Called Volatile Function Overloading";
      }
};

int main()
{
   FunctionOverloading b;
   b.callFunctionOverloading();
  
   return 0;
}


O/P:

Called Normal Function

Explanation:

Not only based on parameters of the function, you can overload the functions based on the const and volatile keyword.

Following functions have different meaning

void callFunctionOverloading()
void callFunctionOverloading()const
void callFunctionOverloading()volatile
void callFunctionOverloading() const volatile


1st function  will be called when  this function is called with normal objects. 2nd function will be called when this object is declared with const object and finally 3rd one will be called when the object is declared with volatile keyword.