Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- INSTRUCTIONS
- In this assignment you will create and use a set of classes to draw basic shapes for a quiz on area and
- perimeter. You will implement these classes:
- • QuizShape
- o Members
- ▪ A border character, a char
- ▪ An inner character, a char
- ▪ A quiz label, a string
- • Rectangle
- o A Rectangle is a QuizShape
- o Members
- ▪ A quiz label
- ▪ A border character
- ▪ An inner character
- ▪ Height, an integer
- ▪ Width, an integer
- • Height and width must both be greater than 3 or greater. If a value less than 3 is passed in,
- set to 3.
- • Square
- o A Square is a Rectangle
- o Members
- ▪ A border character
- ▪ An inner character
- ▪ A quiz label
- ▪ A Height and Width.
- • Remember, a square must always be square. The height and width should always be equal. Never
- allow a square with unequal sides. You will need to address this in the constructor.
- • DoubleSquare
- o A DoubleSquare is a Square
- ▪ Two squares of the same size are drawn separated by spaces. See the sample run.
- o Members
- ▪ A border character
- ▪ An inner character
- ▪ A quiz label
- ▪ A Height and Width.
- • Both squares use the same height and width values
- • Remember, a square must always be square. The height and width should always be equal. Never
- allow a square with unequal sides. You will need to address this in the constructor.
- Use the principles of inheritance to minimize the amount of code you actually write. If a base
- class already does something, the derived class should not reinvent the wheel. Each class should
- define or have access to:
- • One constructor has parameters for every member variable the class can access. Call parent
- constructors when appropriate.
- • void draw();
- o The shape is drawn using its border character and its inner character. For example, a rectangle
- with a width of 10, a height of 5, and a border character ‘+’, and a inner character ‘.’ would be
- drawn like this:
- ++++++++++
- +........+
- +........+
- +........+
- ++++++++++
- o Note: when drawing a square, it won’t look like a square due the font in the window uses
- when you run your program. As long as it has the correct number of border characters and
- inner characters, we are going to call it a square.
- • int getArea();
- o Returns the area of the shape
- • getPerimeter();
- o returns the perimeter of the shape
- • getters for each data member the class knows about
- Setting up your code.
- • In shape.h, define the classes and the prototypes for all methods and constructors in each
- class.
- o The order in which you define the classes will matter
- o IMPORTANT: Add these three lines, exactly as written, to the public section of the QuizShape
- class:
- virtual void draw( ) = 0;
- virtual int getArea( ) = 0;
- virtual int getPerimeter( ) = 0;
- • In shape.cpp, implement all the methods and constructors that are defined in shape.h
- o IMPORTANT: Do not write any code in shape.cpp for draw( ), getArea ( ), and getPerimeter ( ) for
- the QuizShape class.
- o Implement for draw( ), getArea ( ), and getPerimeter ( ) for Rectangle, Square, and DoubleSquare
- as needed.
- o Implement getters as needed in all classes
- • In pass7.cpp
- o Implement the following function
- void quizUser (QuizShape &shape);
- This function should display the label of the shape, draw it, and have the user guess its area and
- perimeter. Tell them if they are correct or if they need to work on their geometry skills (in a
- polite way, of course)
- o main()
- ▪ Main should create a sample of each shape and pass it to the quizUser() function.
- ▪ When creating the labels for each shape, add the dimensions to the label name. For example, the
- label for a Rectangle that is 10 height and 5 wide would be “5x10 Rectangle”
- ▪ Do not cout anything in main(). It should just create shapes and pass them to quizUser().
- Notes
- • It is ok to define multiple classes in one .h file. Programmers often do this to group related
- classes.
- Same goes for .cpp files.
- • No main() in shape.cpp.
- • The lines like this:
- virtual void draw( ) = 0;
- are how we define “pure virtual functions”. This is C++’s way of saying a class knows about a
- function, without the function actually existing. See section 15.7 in the book and be sure to ask
- your instructor for tips and clarification.
- Think of it this way: What is the area and perimeter of a QuizShape? Not a Rectangle, or Square,
- but a QuizShape? There isn’t one, is there. A QuizShape is a basic idea, an abstract idea. A
- Rectangle is a concrete idea. A QuizShape can define the fact that all shapes have an area and a
- perimeter, and can be drawn, but it can’t actually do any of that itself. Pure virtual functions
- are what allow us to accomplish this in C++.
- Sample Run
- Your output should look similar to the following. Things in bold are typed by the user.
- Welcome to the Shape Quiz The Shape: 5x7 Rectangle
- +++++++
- +.....+
- +.....+
- +.....+
- +++++++
- What is the total area? 35
- Congratulations! You are correct What is the total perimeter? 24
- Congratulations! You are correct
- The Shape: 4x4 Square
- ****
- *--*
- *--*
- ****
- What is the total area? 16
- Congratulations! You are correct What is the total perimeter? 12
- Sorry :-( The correct answer is 16 The Shape: Two 3x3 Square
- +++ +++
- +.+ +.+
- +++ +++
- What is the total area? 9
- Sorry :-( The correct answer is 18 What is the total perimeter? 24
- Congratulations! You are correct
- Thanks for quizzing with us!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement