What will be the output of the following C++ program?

#include <iostream>
using namespace std;
class Number
{
    int value;
public:
    Number(int value)
    {
        this->value = value;
    }
    void show()
    {
        cout << value;
    }
};
int main()
{
    Number n(25);
    n.show();
    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 B

Explanation:
The parameter and data member are both named value. The expression this->value refers to the current object member, while the unqualified value on the right refers to the parameter. The constructor stores 25 in the object, so show() prints 25.