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

Pham Quỳnh

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<<"Default constructor called\n";
	}
	A(const A&#038; a){
		cout<<"Copy Constructor called\n";
	}
};
int main(int argc, char const *argv[])
{
	A obj;
	A a1 = obj;
	A a2(obj);
}
A.
Default constructor called
Copy Constructor called
B.
Default constructor called
Copy Constructor called
Copy Constructor called
C.
Default constructor called
Default constructor called
Copy Constructor called
D.
Copy Constructor called
Default constructor called
Copy Constructor called
 
Answer: b
Explanation: When object obj is declared then the default constructor is called. When we are declaring the a1 object as we are equating obj to a1 object obj will be copied to a1 hence copy constructor is called, similarly when object a2 is created obj is passed as a parameter to function which is available as copy constructor function, hence copy constructor will be called. So one time Default constructor and two times copy constructor.
 

Tìm kiếm

Back
Top