Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify C++ copy constructor program so it also stores doubles and int #include <

ID: 3542376 • Letter: M

Question

 Modify C++ copy constructor program so it also stores doubles and int

#include
<iostream> using namespace std; class MyClass { int val; int copynumber; public: MyClass(int i) { val = i; copynumber = 0; cout << "Inside normal constructor "; } // Copy constructor MyClass(const MyClass &o) { val = o.val; copynumber = o.copynumber + 1; cout << "Inside copy constructor. "; } ~MyClass() { if(copynumber == 0) cout << "Destructing original. "; else cout << "Destructing copy " << copynumber << " "; } int getVal() { return val; } }; void display(MyClass ob) { cout << ob.getVal() << ' '; } int main() { MyClass a(10); display(a); return 0; }

Explanation / Answer

here is the code that it can store both int and double.............

#include <iostream>

using namespace std;

template<class t1>


class MyClass {

t1 val;

t1 copynumber;

public:

MyClass(t1 i) {

val = i;

copynumber = 0;

cout << "Inside normal constructor ";

}

// Copy constructor

MyClass(const MyClass &o) {

val = o.val;

copynumber = o.copynumber + 1;

cout << "Inside copy constructor. ";

}

~MyClass() {

if(copynumber == 0)

cout << "Destructing original. ";

else

cout << "Destructing copy " <<

copynumber << " ";

}

};

template<class t1>

t1 MyClass<t1>::getval() {

return val;

}


template<class t1>

void MyClass<t1>::display(MyClass ob)

{

cout << ob.getVal() << ' ';

}

int main()

{

MyClass a(10);

display(a);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote