Sams Teach Yourself C++ in 21 Days

(singke) #1
Answers 839

D


// Fetch then increment
const SimpleCircle SimpleCircle::operator++ (int)
{
// declare local SimpleCircle and initialize to value of *this
SimpleCircle temp(*this);
++(itsRadius);
return temp;
}


  1. The following is one possible answer:
    class SimpleCircle
    {
    public:
    SimpleCircle();
    SimpleCircle(int);
    ~SimpleCircle();
    void SetRadius(int);
    int GetRadius();
    const SimpleCircle& operator++();
    const SimpleCircle operator++(int);
    private:
    int *itsRadius;
    };


SimpleCircle::SimpleCircle()
{
itsRadius = new int(5);
}

SimpleCircle::SimpleCircle(int radius)
{
itsRadius = new int(radius);
}

const SimpleCircle& SimpleCircle::operator++()
{
++(*itsRadius);
return *this;
}

// Operator ++(int) postfix.
// Fetch then increment
const SimpleCircle SimpleCircle::operator++ (int)
{
// declare local SimpleCircle and initialize to value of *this
➥SimpleCircle temp(*this);
++(*itsRadius);
return temp;
}

32 0672327112_app_d.qxd 11/19/04 12:30 PM Page 839

Free download pdf