Answer to Question #234711 in C++ for SOM

Question #234711

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;

}



1
Expert's answer
2021-09-09T05:01:43-0400
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. 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS