Consider the code given below.
[Assume all the header files and namespaces included.]
How many times the following are invoked:
I) Default constructor
II) Parameterized constructor
III) Copy constructor
IV) Destructor
class test
{ int a;
public:
test(){a=0;}
test(int x) {a=x;}
test(const test &T){a=T.a;}
~test(){ }
test add(test X)
{ test T;
T.a=a+X.a;
return 0;
}
};
int main()
{ test t1(5),t2(10),t3;
t3=t1.add(t2);
return 0;
}
I) Default constructor - This is invoked one time when creating the test t3.
II) Parameterized constructor- This is invoked two times when creating the two instances of the class t1, t2.
III) Copy constructor- This is invoked one-time t3=t1.add(t2);
IV) Destructor- This is invoked three times because there are three objects created.
Comments
Leave a comment