What is the output of the following C++ program?
[gamipress_button type=”submit” label=”Run Code Online” onclick=”window.open(‘https://coderseditor.com’,’_blank’)”]
#include<iostream> using namespace std; class Test { int value; public: Test (int v = 0) {value = v;} int getValue() { return value; } }; int main() { const Test t; cout << t.getValue(); return 0; }
Studently Answered question October 11, 2022
Output:
Compiler Error.
A non-const function cannot be called by a const object. Either making getValue() const or making t non-const will fix the aforementioned code. The modified programme below works as intended and prints 0 because getValue() is declared as const.
Studently Answered question October 11, 2022