DUE: Sun May 1, 11 pm
(10% penalty for late submission, up to 1 day late)
Any changes to this document, typically due to an error on the part of the author, will be logged here.
Collaboration is strongly ENCOURAGED within a programming team, but the program you submit must still represent YOUR OWN original work. Teams should work on the algorithm together, and help debug/test each others code. However, copying code from ANY outside source (any book, current or past students, past solutions, web sites, etc.) is STRICTLY FORBIDDEN. Copy and Paste from another solution, including the solution of a team mate will incur a stiff penelty.
Inheritance is one of the most powerful tools availabe to you when you use C++. It allows you to share code among like objects. In building the game suite we will have several objects that posess many properties in common with the Button object that we created in assignment3. For instance we will want the ability to show and hide an object, determine whether an object has been clicked, and move an object. This week we will re-implement the Button class using what we have learned about inheritance.
For this assignment we will take a substantial portion of the Button's functionality and place it into a class called Clickable. Next, we will inherit from this class in order to create the Button class. The program that you turn in will do the exact same thing as it did for assignment 3, however the underlying implementation of the Button will be different. You can use the main program that you used for assignment 3 if you wish, though you may have to make an alteration or two to it, due to a change in the default behavior of a Button.
You will create an abstract base class called 'clickable' which has the following members and methods.
Clickable();
Clickable( const Point &ul_corner, const double height,
const double width );
Clickable( const Point &ul_corner, const Point &ur_corner,
const Point &lr_corner, const Point &ll_corner );
virtual ~Clickable();
The different constructors are meant to give many options for how a Clickable can be created. It's left as an exercise for you to find out why the destructor is declared virtual. We're going to change the default value of the shown variable this time so that any Clickable object is hidden by default when it is created.
//! \brief Draw the object in the drawing area.
virtual void draw() const = 0;
//! \brief Test to see if the object has been clicked.
bool isClicked( const Point &click ) const;
//! \brief Test to see if the object is currently shown.
bool isShown() const;
The draw function is purely virtual, forcing the derived class object to implement how that object is drawn. isClicked behaves exactly as it did in the Button class from assignment3, and isShown has been added to report whether the object is currently shown or hidden.
//! \brief Make the object visible in the drawing window when drawn.
virtual void show();
//! \brief Make the object invisible in the drawing area when drawn.
virtual void hide();
//! \brief Move the object by a displacement.
virtual void move( const double x_displace, const double y_displace ) = 0;
//! \brief Move the upper left corner of the object to a position.
void moveTo( const Point &loc );
//! \brief Center the object about a point.
void center( const Point &loc );
These functions are pretty much the same as the assignment3 implementation with the exception that the move function is purely virtual and is not implemented in the Clickable class. The moveTo and center methods should be implemented so that they call the move function in order to move the object. This ensures that the derived class object will be moved in the way in which its designers intended.
Also, the show and hide methods are declared virtual which will allow derived class objects objects to recursively hide or show objects that they own when they are hidden or shown.
bool shown; //!< Whether the object is currently shown.
Point ul_corner; //!< The upper left corner of the object.
Point ur_corner; //!< The upper right corner of the object.
Point lr_corner; //!< The lower right corner of the object.
Point ll_corner; //!< The lower left corner of the object.
These variables serve the same purpose as they did in assignment3.
The button class will inherit much of it's functionality from the Clickable class. What must be implemented in the button class is as follows:
Button();
Button( const Point &ul_corner, const std::string &label );
These are the same constructors from assignment 3. This time, the button should be hidden by default when it is created. This is a change from assignment3.
//! \brief Draw the Button in the drawing area.
virtual void draw() const;
The draw method should draw the button just as it did in assignment 3.
//! \brief Move the Button by a displacement.
virtual void move( const double x_displace, const double y_displace );
Again, the implementation of move should be the same as in assignment 3. This implementation will be used by the moveTo and center methods of the Clickable class.
std::string label; //!< The text label inside the Button.
Point label_pos; //!< The position where the label should be drawn.
The same variables as in assignment 3.
For this assignment a framwork will be provided for you which includes all of the files that are necessary for you to complete the assignment. The files included in the framework are the following:
You may download the framework here. To uncompress the framework archive you should type:
where <framework_file> is the name of the file that you downloaded.
Your code must compile on the school's linux system, using the flags specified in the class coding standards. You must provide a makefile with the targets "all" and "clean" so the grader can compile you assignment. Submit your work (the source file(s) and Makefile ONLY) electronically to the correct folder on the cs secure server. Don't forget to include the header template at the top of each file that you submit.
For this assignment you will turn in the following files:
Each file that you have written should conform to the class coding standard. This includes but is not limited to:
You may download a sample program right-clicking and save-as here. You will probably need to change the permissions on the file so that it is user executable. You can do this with the command:
If your program does not compile, it will not be graded. Also, programs which implement the functionality of the Button class in the main or do not inherit the Button from Clickable will not be graded. Compile your code often. Only write a small portion of code before checking that it still compiles. This way when you get a syntax error, you can be fairly certain the error is in the part you just wrote.
The points for this assignment will be distributed as follows:
| Points | Feature Description |
|---|---|
| 2 pts | Program compiles |
| 3 pts |
Adherence to the class coding standard -- ( .5 pts ) Program does not use global variables or objects -- ( .5 pts ) Good variable and function names -- ( .5 pts ) Proper indentation and spacing -- ( .5 pts ) Good comments ( including header and function comments ) -- ( .5 pts ) No line wraps -- ( .5 pts ) Other miscellaneous parts of the standard. |
| 1 pts | Program draws both buttons. |
| 1 pts | Hide/show button works properly. |
| 1 pts | Program exits when the "quit" button is clicked. |
| 2 pts | "Quit" button is centered on a point in the drawing area when the drawing area is clicked. |