What will be the output of the following C++ code? [code] #include <iostream> using namespace std; class A{ public: int a; A(int a){ this-

Nam Phong

New member
What will be the output of the following C++ code?
Code:
#include <iostream>  
using namespace std;
class A{
public:
	int a;
	A(int a){
		this->a = a;
	}
};
int main(int argc, char const *argv[])
{
	A a1, a2(10);
	cout<<a2.a;
	return 0;
}
A. 10
B. Compile time error
C. Run-time error
D. No output
 
Answer: b
Explanation: The declaration of object a1 needs a constructor without any arguments which is not available in the class as we have overwritten the default constructor, therefore, the program gives the error.
 

Tìm kiếm

Back
Top