Jan 28, 2018 at 2:27pm UTC
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include<iostream>
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 Jan 28, 2018 at 4:29pm UTC
Jan 28, 2018 at 4:59pm UTC
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.
Jan 28, 2018 at 5:08pm UTC
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#include <iostream>
#include <utility>
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:
http://coliru.stacked-crooked.com/a/2186aaddd78b1258
Last edited on Jan 28, 2018 at 8:29pm UTC
Jan 30, 2018 at 2:06pm UTC
Ok thank you kindly for your help,