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

What will be the output of the following C++ code?
Code:
#include <iostream>
#include <string>
using namespace std;
class A
{
	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. 10
C. Error
D. Segmentation fault
 
Answer: c
Explanation: As the assign() is a constant function and a constant function cannot change the state of an object and as in the assign function we are trying to modify the member a of the object therefore the program gives error.
 

Tìm kiếm

Back
Top