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

Hương Trang

New member
What will be the output of the following C++ code?
Code:
#include <iostream>
#include <string>
using namespace std;
class A{
	mutable int a;
public:
	A(){
		cout<<"A's Constructor called\n";
	}
	~A(){
		cout<<"A's Destructor called\n";
	}
};
class B{
	static A a;
public:
	B(){
		cout<<"B's Constructor called\n";
	}
	~B(){
		cout<<"B's Destructor called\n";
	}
};
A B::a;
int main(int argc, char const *argv[])
{
	return 0;
}
A.
A's Constructor called
A's Destructor called
B.
B's Constructor called
B's Destructor called
C.
A's Constructor called
B's Constructor called
B's Destructor called
A's Destructor called
D.
A's Constructor called
B's Constructor called
A's Destructor called
B's Destructor called
 
Answer: a
Explanation: Here as no object of B is declared so no call to B's constructor but as we have initialised the static member 'a' of class B, hence A's constructor and destructor will be called once.
 

Tìm kiếm

Back
Top