What will be the output of the following C++ code? [code] #include <iostream> #include <string> using namespace std; class A { mutable

What will be the output of the following C++ code?
Code:
#include <iostream>
#include <string>
using namespace std;
class A
{
	mutable int a;
        public:
	int assign(int i) const {
		a = i;
	}
	int return_value() const {
		return a;
	}
};
int main(int argc, char const *argv[])
{
	A obj;
	obj.assign(5);
	cout<<obj.return_value();
}
A. 5
B. Error
C. Segmentation fault
D. Undefined value
 
Answer: a
Explanation: As a is mutable member of the class it's value can be modified whether it is a part of constant object or not. It can be modified even inside a constant member function. Hence, the program tuns fine and does not gives any error.
 

Tìm kiếm

Back
Top