Examveda

In the following C++ code how many times the string "A's constructor called" will be printed?
#include <iostream>
#include <string>
using namespace std;
class A{
	int a;
public:
	A(){
		cout<<"A's constructor called";
	}
};
class B{
	static A a;
public:
	B(){
		cout<<"B's constructor called";
	}
	static A get(){
		return a;
	}
};
A B::a;
int main(int argc, char const *argv[])
{
	B b;
	A a1 = b.get();
	A a2 = b.get();
	A a3 = b.get();
}

A. 3

B. 4

C. 2

D. 1

Answer: Option D


Join The Discussion

Related Questions on Constructors and Destructors in C plus plus