The Wayback Machine - https://web.archive.org/web/20240225000931/https://www.geeksforgeeks.org/c-const-keyword-question-3/
Open In App
Related Articles

C++ | const keyword | Question 3

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
Predict the output of following program.
#include <iostream>
using namespace std;
class Point
{
    int x, y;
public:
 Point(int i = 0, int j =0)
   { x = i; y = j;  }
   int getX() const { return x; }
   int getY() {return y;}
};
  
int main()
{
    const Point t;
    cout << t.getX() << " ";
    cout << t.getY();
    return 0;
}

                    
(A) Garbage Values (B) 0 0 (C) Compiler Error in line cout << t.getX() << " "; (D) Compiler Error in line cout << t.getY();

Answer: (D)

Explanation: There is compiler Error in line cout << t.getY(); A const object can only call const functions.

Quiz of this Question

Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads