Sams Teach Yourself C++ in 21 Days

(singke) #1
Working with Advanced Functions 327

10


Exercises ........................................................................................................



  1. Write a SimpleCircleclass declaration (only) with one member variable:
    itsRadius. Include a default constructor, a destructor, and accessor methods for
    radius.

  2. Using the class you created in Exercise 1, write the implementation of the default
    constructor, initializing itsRadiuswith the value^5. Do this within the initializa-
    tion phase of the constructor and not within the body.

  3. Using the same class, add a second constructor that takes a value as its parameter
    and assigns that value to itsRadius.

  4. Create a prefix and postfix increment operator for your SimpleCircleclass that
    increments itsRadius.

  5. Change SimpleCircleto store itsRadiuson the free store, and fix the existing
    methods.

  6. Provide a copy constructor for SimpleCircle.

  7. Provide an assignment operator for SimpleCircle.

  8. Write a program that creates two SimpleCircleobjects. Use the default construc-
    tor on one and instantiate the other with the value 9. Call the increment operator on
    each and then print their values. Finally, assign the second to the first and print its
    values.

  9. BUG BUSTERS:What is wrong with this implementation of the assignment oper-
    ator?
    SQUARE SQUARE ::operator=(const SQUARE & rhs)
    {
    itsSide = new int;
    itsSide = rhs.GetSide();
    return
    this;
    }
    10.BUG BUSTERS:What is wrong with this implementation of the addition
    operator?
    VeryShort VeryShort::operator+ (const VeryShort& rhs)
    {
    itsVal += rhs.GetItsVal();
    return *this;
    }

Free download pdf