Question: Using C++, write a complete program to define a copy constructor, destructor, and copy assignment operator so the following class follows RAII and its objects
Using C++, write a complete program to define a copy constructor, destructor, and copy assignment operator so the following class follows RAII and its objects are copied correctly.
class MyInt { public: MyInt(int x) { the_int = new int(x); } void print() const { cout << *the_int; } ... private: int* the_int; }; Note: Copy assignment is operator= for copying object of the same class. For example,
MyInt x(3), y(5); MyInt z = x; // copy constructor is used y = x; // copy assignment y.print(); // 3, if you defined copy assignment correctly
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
