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 &t; public: Test (int &x) { t = x; } int getT() { return t; } }; int main() { int x = 20; Test t1(x); cout << t1.getT() << " "; x = 30; cout << t1.getT() << endl; return 0; }
Studently Answered question October 11, 2022
Output:
Compiler Error.
Since t is a reference in Test, Initializer List must be used to initialise it. Here is the updated programme. It prints “20 30” correctly.
Studently Answered question October 11, 2022