Wednesday, 7 January 2015

CONST FUNCTION OVERLOADING

Const function overloading

Today we are try to overload const function:--
class A
     {
             public:
                    int func(int a) const;
                    int func(int ) ;
     };
int A:: func(int x) const
       {
                  cout<<"I am in Const"<<x;
       }
int B::func(int y)
      {
                  cout<<"I am in func"<<y;  
      }
int main()
     {
            A y;
            A const x;
            x.func(20);
            y.func(10);
    }
Output:-
       I am in Const 20
       I am in func 10
if you observe the output, you  can see a const object called the const function and object y called the func().
It means C++ allows member methods to be overloaded on the basis of const type.

No comments:

Post a Comment