### deep copy incremented values incrementing to much

Hi I'm doing a chapter on deep copy in my book and the question asks me to create two objects one with a default value and deep copy it and then change the value when the second object is created, increment both then assign the second object to the first. I'm having trouble understanding how many copies are made, why both objects increment by four and why the second value come out wrong.

Here is what I have got so far:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172`` ``````#include using namespace std; class SimpleCircle { public: SimpleCircle(){}; SimpleCircle(int val); SimpleCircle(const SimpleCircle &); ~SimpleCircle(){} int GetitsRadius()const{return itsRadius;} int SetitsRadius(int radius){itsRadius = radius;} const SimpleCircle& operator++(); const SimpleCircle& operator++(int); SimpleCircle operator=(const SimpleCircle &); private: int *itsRadius; }; SimpleCircle::SimpleCircle(int val) { itsRadius=val; } SimpleCircle::SimpleCircle(const SimpleCircle& rhs) { itsRadius=new int; *itsRadius= rhs.GetitsRadius(); } const SimpleCircle& SimpleCircle::operator++() { ++itsRadius; return *this; } const SimpleCircle & SimpleCircle::operator++(int x) { itsRadius++; return *this; } SimpleCircle SimpleCircle::operator=(const SimpleCircle & rhs) { if(this == &rhs) return *this; itsRadius = new int; *itsRadius = rhs.GetitsRadius(); } int main() { SimpleCircle CircleOne; SimpleCircle CircleTwo(9); cout << "The value of CircleOne's radius is " << CircleOne.GetitsRadius() << "\n"; cout << "The value of CircleTwo's radius is " << CircleTwo.GetitsRadius() << "\n"; cout << "Incrementing.....\n"; CircleOne++; CircleTwo++; cout << "The value of CircleOne's radius is " << CircleOne.GetitsRadius() << "\n"; cout << "The value of CircleTwo's radius is " << CircleTwo.GetitsRadius() << "\n"; cout << "Copying.\n"; CircleTwo=CircleOne; cout << "The value of CircleOne's radius is " << CircleOne.GetitsRadius() << "\n"; cout << "The value of CircleTwo's radius is " << CircleTwo.GetitsRadius() << "\n"; }``````
Last edited on
Turn on compiler warnings. There are several places in the code where you use itsRadius when you should be using *itsRadius or other related errors.
operator=() doesn't return a value if it reaches the end of the method.

The destructor needs to delete itsRadius.
This program is an excellent example of how to misuse new.

In real life, we'd never write SimpleCircle like this: we would rather write
`struct SimpleCircle { int radius; };`Or maybe even
`using SimpleCircle = int;`With some extra care.

Anyways:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960`` ``````#include #include class simple_circle { public: // NOTE: always allocate space for the radius, always initialize it. simple_circle(int val = 0) : radius{new int{val}} {} simple_circle(simple_circle const &rhs) : simple_circle(rhs.get_radius()) {} ~simple_circle() { delete radius; } // NOTE: copy-and-swap (simple, exception safe) // https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Copy-and-swap simple_circle &operator=(simple_circle rhs) { using std::swap; swap(radius, rhs.radius); return *this; } int get_radius() const { return *radius; } void set_radius(int r) { *radius = r; } simple_circle &operator++() { ++*radius; return *this; }; simple_circle operator++(int) { simple_circle result(*this); ++(*this); return result; } private: // NOTE: eliminate information-free words like "its" in variable names int *radius; }; int main() { simple_circle CircleOne; simple_circle CircleTwo(9); std::cout << "The value of CircleOne's radius is " << CircleOne.get_radius() << '\n'; std::cout << "The value of CircleTwo's radius is " << CircleTwo.get_radius() << '\n'; std::cout << "Incrementing.....\n"; CircleOne++; CircleTwo++; std::cout << "The value of CircleOne's radius is " << CircleOne.get_radius() << '\n'; std::cout << "The value of CircleTwo's radius is " << CircleTwo.get_radius() << '\n'; std::cout << "Copying.\n"; CircleTwo = CircleOne; std::cout << "The value of CircleOne's radius is " << CircleOne.get_radius() << '\n'; std::cout << "The value of CircleTwo's radius is " << CircleTwo.get_radius() << '\n'; }``````

Live demo: