What is the output of the following C++ program?
#include <iostream>
using namespace std;
class Test {
int x;
public:
Test(int x) {
this->x = x;
}
void display() {
cout << x;
}
};
int main() {
Test t(25);
t.display();
return 0;
}
Options
-
B.
25
-
C.
Compilation error
-
D.
More than one of the above
-
E.
None of the above
Correct Answer (Detailed Explanation is Below)
B.
25
Detailed Explanation
Correct Answer: Option BExplanation:The constructor parameter x has the same name as the data member x. The expression this->x refers to the object's data member, while the right-hand x refers to the parameter. The value 25 is stored and printed.