Program 6

Home Windows C++ 241 C++ 242 Contact Info Contents

Program 6 - February 19

Up

In this assignment, you will develop a simple base class and then make use of inheritance to upgrade it with added functionality. You will also get some more practice using operator overloading. There are two required parts and an extra credit assignment for those interested in doing it.

Note: This assignment was originally posted with errors in it. Check for corrections below in red. Click  here for an explanation.

Part 1

The following class declaration represents a rectangle which lives in a cartesian coordinate system. Positive values of coordinates increase from bottom to top and left to right.

class Rect
{
    friend ostream& <<(ostream&, const Rect&);
    friend istream& >>(istream&, Rect&);

public:
    // construct from left, top, bottom and  right values
    Rect(int l = 0, int t = 0, int b = 0, int r = 0);
    Rect(const Rect &);       // from another rect
    ~Rect();

    void Normalize();          // make bottom less than top
                                      // and left less than right
                                      // by swapping  values if needed

    int Width();        // return the width of the rectangle
    int Height();       // return the height of the rectangle

    // Move the upper left corner while maintaining the size
    void Move(int x, int y);

    // Change the width and height without moving it
    void Size(int w, int h);

    const Rect & operator=(const Rect &);

protected:                            //    Why?
    int left, top, bottom, right;
};

Put the above code into a header file. Develop a cpp file which implements the class. Develop a short program that exercises all the functions. You may use any format you desire to output the numbers that make up a rect. One way is to print 
    Rect(nn, nn, nn, nn)
The input should just take the four numbers and plug them into to the  rect object. You should call Normalize whenever it's necessary to ensure that the values are in the correct order.

Part 2

Publicly derive a new class from Rect called SmartRect. Give it the following new functions:

    // True if point (x,y) is inside the rect
    bool IsPtInRect(int x, int y);

    // Return the union of two rects
    Rect operator|(const SmartRect & other) const;    //  Why?

    // Return the intersection of two rects
    Rect operator&(const SmartRect & other) const;   //  Why?

Create a new header file and an implementation file for SmartRect. Don't change any code for Rect. Modify your test
program to use the new functions as well as the old. Turn in
the final test program, the Rect header and implementation and
the SmartRect header and implementation.

Why the Changes?

  1. The Rect class originally had private data. The change from private to protected is necessary so that a SmartRect object can access it's own data members.

  2. The union and intersection operators cannot return a reference as originally specified. That's because they create a new temp object, so you must return a Rect, not a reference to a Rect.

  3. The original header for SmartRect indicated you should be able to take the union or intersection of a SmartRect with a Rect. Because of how protected access works, you have to be working with a SmartRect in order to get at it's protected data members. Making the second argument a SmartRect solves this problem. We'll discuss this further in class and I will give you a few other ways around the problem then.

Extra Credit

Modify your TicTacToe program to use inheritance. Develop a class called Player which has the same interface as your human and computer players. Put anything that is common between the two existing classes into player. Modify your human and computer player classes so that they each derive from Player.

Since we have not yet learned about virtual functions, your Player::MakeMove() routine could just give an error message since it should never be called. Don't change your main program, until we have virtual functions, you should still make all your calls through derived class objects or pointers.

You'll get another shot at this when we get to chapter 10.

Up

Home Windows C++ 241 C++ 242 Contact Info Contents

Copyright © 2000  Charlie Poole. All rights reserved.
Revised: July 15, 2002 - cpoole@ctc.edu