Monday, 29 December 2014

Why we used const function???

We Used Const function why??

#include<iostream>
using namespace std;
class A
     {
           private:
                    int a=5;
           public:
                int func1() const;
     };
int A::func1() const
     {
           cout<<a;
     }
int main()
     {
          A const a;
          a.func1();
     }
Now here i have a small example of const function................
So, why we need const function 
because a const function can't able to modify the class member variable in it. For example we have 
a meter reading class which reads the value of meter in km in its member variable ""M" now we want to display it so we use use a show () const which is only able to read it and display it.
                                   Let's you want to modify a class member variable in const function so you can use mutable keyword ........

No comments:

Post a Comment