C++ | const keyword | Question 3
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; } |
chevron_right
filter_none
(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
Recommended Posts:
- C++ | const keyword | Question 5
- C++ | const keyword | Question 5
- C++ | const keyword | Question 1
- C++ | const keyword | Question 2
- Function overloading and const keyword
- Difference between const int*, const int * const, and int const *
- Difference between const char *p, char * const p and const char * const p
- C++ | Static Keyword | Question 2
- C++ | Static Keyword | Question 3
- C++ | friend keyword | Question 1
- C++ | Static Keyword | Question 1
- C++ | Static Keyword | Question 4
- C++ | friend keyword | Question 2
- C++ | Static Keyword | Question 5
- C++ | Static Keyword | Question 6
Improved By : Kjell



