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

What will be the output of the following C++ code?
Code:
#include <iostream>  
using namespace std;
class A{
public:
	int a;
	A(){
		cout<<"Constructor called\n";
	}
} a;
int main(int argc, char const *argv[])
{
	cout<<"Main function\n";
	return 0;
}
A.
Constructor called
Main function
B.
Main function
Constructor called
C. Error
D. Segmentation fault
 
Answer: a
Explanation: In this program, we have defined a global variable an outside main function for which constructor will be called. Now as a is a global variable, therefore, the call for constructor will be before the main function hence constructor called will be printed before the Main function.
 

Tìm kiếm

Back
Top