Answer to Question #239639 in C++ for hello

Question #239639

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-25T10:08:26-0400
#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


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