Description
Requirements
- Write the following classes:
Point
- Describes a point in the Euclidean plane.
- Note: This means that the x and y values need to be eitherÂ
float
 orÂdouble
.
- Note: This means that the x and y values need to be eitherÂ
- Must be able to be initialized with no arguments, defaulting to the point (0,0).
- Note: This is the same as saying that it must have a default constructor. If a constructor has default values for all arguments, it fulfills this requirement and is considered a default constructor.
- Must be able to be initialized with an x and y value.
- Must not be possible to change the x or y value once it has been constructed.
- Note: This means that the member variables containing the x and y values must be private (or protected), and you must implement getters (but not setters) for each.
- Describes a point in the Euclidean plane.
Shape
- Must contain the following pure virtual function:
/** * A function to determine whether a shape contains a given point. * * Arguments: * - `p`: The point we are considering. * * Returns: * - `true` if the given point is inside the shape, `false` * otherwise. */ virtual bool contains(const Point & p) const = 0;
- Should also contain a virtual default destructor:
virtual ~Shape() = default;
This allows for child classes being handled through a pointer of typeÂ
Shape
 to have their destructors called when the variable goes out of scope or isÂdelete
d (search “cpp polymorphism”).
- Must contain the following pure virtual function:
Rectangle
 andÂEllipse
- Must inherit fromÂ
Shape
, and be concrete classes (i.e. not abstract classes, i.e. they must override allÂShape
s pure virtual functions).
- Must inherit fromÂ
Square
- Must inherit fromÂ
Rectangle
.
- Must inherit fromÂ
Circle
- Must inherit fromÂ
Ellipse
.
- Must inherit fromÂ
- Write a function with the following prototype (probably inÂ
main.cpp
):/** * A function to draw the `Shape*`s in the given vector in a terminal. * * Arguments: * - `v`: A vector containing pointers to each `Shape` to draw. * * Notes: * - A terminal window is typically 80 columns wide by 25 lines high. * - The width:height aspect ratio of a terminal character is approximately * 1:1.9. */ void draw(const vector<Shape*> & v) {
This function should iterate through every column on every line. If the point represented by the character at that location is contained in any of theÂ
Shape
s, it should print aÂ"*"
 (or something). If not, it should print aÂ" "
 (or something).This function should also scale the shapes correctly, according the aspect ratio of the terminal characters. It would probably be best to use several constants: one to represent the number of columns, one to represent the factor used to convert the column number to the x value represented by a character in that column, and similarly for the number of lines and the factor used to convert the line number to the corresponding y value.
Optionally, you may also draw a border, where the top and bottom of each column is aÂ
"-"
 (or something), and the first and last character of each line is aÂ"|"
 (or something). - Use the following template forÂ
main()
:int main() { // declare constants for the number of frames to draw and the // amount of time to sleep after drawing each frame // for each frame // create some shapes (with values depending on the current frame // number) // put pointers to them in an array // // for example, given a `Rectangle r` and a `Square s`: vector<Shape*> shapes = { &r, &s, }; // this is possible because `Rectangle`s and `Squares`, and all // your other shapes, inherit from `Shape` // draw the shapes in the terminal // // for example, given the `shapes` array above: draw(shapes); // wait before drawing the next frame // // for example: std::this_thread::sleep_for(std::chrono::milliseconds(frameSleep)); // if you'd like to know more about what this line is doing, look // up the documentation for `std::this_thread::sleep_for` and // `std::chrono::milliseconds()`. return 0; // success }
If you’d like to write yourÂ
main()
 in a different way that’s fine, so long as it accomplishes the same goal.
Requests
- Please try to do this in groups, and try to use Google, etc., only for answers to questions about the language (as opposed to questions about this problem).
Assumptions
- A terminal window is 80 columns wide by 25 lines high.
- The width:height aspect ratio of a terminal character is approximately 1:1.9.
Style
- Place your solution in aÂ
solution--YOURNAME
 subdirectory (whereÂYOURNAME
 is your GitHub username). - Include your copyright and license information at the top of every file, followed by a brief description of the file’s contents, e.g.
/* ---------------------------------------------------------------------------- * Copyright © 2016 Ben Blazak <[email protected]> * Released under the [MIT License] (http://opensource.org/licenses/MIT) * ------------------------------------------------------------------------- */ /** * A short program to print "Hello World!" to standard output. */
- Use “include guards” in allÂ
.h
 files. Be sure to give the preprocessor variable a name corresponding to the file name. For example, inÂpoint.h
:#ifndef POINT_H #define POINT_H // ---------------------------------------------------------------------------- // ... everything besides the copyright information and file description // ---------------------------------------------------------------------------- #endif // POINT_H
main()
 must have its ownÂ.cpp
 file. I suggest calling itÂmain.cpp
.- Classes must have bothÂ
.h
 andÂ.cpp
 files, with member functions defined in theÂ.cpp
 files unless they are truly trivial. If it makes sense, you may put multiple classes into one pair ofÂ.h
 andÂ.cpp
 files. - Declare member functions and function arguments asÂ
const
 when appropriate (in general, whenever possible). - Document and format your code well and consistently. Be sure to clearly document the source of any code, algorithm, information, etc. that you use or reference while completing your work.
- Wrap lines at 79 or 80 columns whenever possible.
- End your file with a blank line.
- Do not useÂ
using namespace std;
. You may get around typingÂstd::
 in front of things or with, e.g.,Âusing std::cout;
.