Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The class A is responsible for managing a single int (not an array) on the heap. Write a copy constructor that does a deep copy

image text in transcribed

The class A is responsible for managing a single int (not an array) on the heap. Write a copy constructor that does a deep copy of an A object. You need to accept an A& as parameter, get the value of that object's myNumber and make your own int on the heap that contains that value Hints One way to start is by just allocating a new int in the copy constructor and always giving it the value 10 (or something else arbitrary), It won't pass the tests, but you can use that to verity that the copied object actually owns its own memory (10 won't get wiped out by changing the original). other.myNumber is a pointer to an int. If you want the value of other's myNumber you need to do "other.myNumber Code #1nclude 3 using nazespace std; 5 class A f 6 public: A(int num) myNumber = new int (num); { 10 11 //Do not modify anything on or above the line below this 12 //YOUR CODE BELOW 13 14 15 16 //YOUR CODE ABOVE 17 //Do not modify anything on or below the line above this /YOUR CODE void print) 19 20 21 23 24 25 26 27 private: void setNum(int value) myNumbervalue; int myNumber //Memory address of number we own 29 30 31 int main) A thing1(5) //thing1 points to 5 34 35 36 37 38 39 A thing2(thingl)i //thing2 copies thingl thingl.setNum (8) thingl.print1/8 41 42 4 3 thing2.print):I/should sti11 be 5 - if 8 you have a shallow copy cout

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

3rd Edition

0071107010, 978-0071107013

More Books

Students also viewed these Databases questions

Question

What is one tax advantage of a limited liability company?

Answered: 1 week ago