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;
}
#include <iostream>
using namespace std;
class test{
int a;
public:
test(){
a=0;
cout<<"I) Default constructor\n";
}
test(int x) {
a=x;
cout<<"II) Parameterized constructor\n";
}
test(const test &T){
a=T.a;
cout<<"III) Copy constructor\n";
}
~test(){
cout<<"IV) Destructor\n";
}
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);
int k;
cin>>k;
return 0;
}
I) Default constructor - 2 times
II) Parameterized constructor - 3 times
III) Copy constructor - 1 time
IV) Destructor - 3 times
Comments
Leave a comment