Sams Teach Yourself C++ in 21 Days

(singke) #1

  1. The destructor is called each time an object is destroyed, either because it goes out
    of scope or because you call deleteon a pointer pointing to it.

  2. The assignment operator acts on an existing object; the copy constructor creates a
    new one.

  3. The thispointer is a hidden parameter in every member function that points to the
    object itself.

  4. The prefix operator takes no parameters. The postfix operator takes a single int
    parameter, which is used as a signal to the compiler that this is the postfix variant.

  5. No, you cannot overload any operator for built-in types.

  6. It is legal, but it is a bad idea. Operators should be overloaded in a way that is
    likely to be readily understood by anyone reading your code.

  7. None. Like constructors and destructors, they have no return values.


Exercises


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

  2. The following is one possible answer:
    SimpleCircle::SimpleCircle():
    itsRadius(5)
    {}

  3. The following is one possible answer:
    SimpleCircle::SimpleCircle(int radius):
    itsRadius(radius)
    {}

  4. The following is one possible answer:
    const SimpleCircle& SimpleCircle::operator++()
    {
    ++(itsRadius);
    return *this;
    }


// Operator ++(int) postfix.

838 Appendix D

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

Free download pdf