Const object of a class can call non-const member function of class - Functions

Q.  A const object of a class can call non-const member function of the class.
- Published on 17 Jul 15

a. True
b. False

ANSWER: False
 

    Discussion

  • Brijesh   -Posted on 09 Oct 15
    A const object of a class can call non-const member function of the class is false statement because const class objects can only call const member functions.
    Example:
    class Add {
    int a, b;
    public:
    int setValue(int i, int j) const
    {
       return i+j;
    }
    };


    int main()
    {
      const Add addObj;

       cout << "Summation =:" << addObj.setValue(3, 4);
       return 0;
    }

    In the above program addObj is constant. It calls the setValue method that is also constant function. If you remove the const keyword of function setValue then it will give error.

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)