Description
The purpose of this challenge is to define basic classes.
Requirements
- Define a class called Point as below
class Point { private: double x, y; public: void set_coords(double _x, double _y) { x = _x; y = _y; } };
- Beneath the definition of Point, define a class called Map as below
class Map { private: Point tl, br; public: Map(Point _tl, Point _br) { } double calc_area() { } };
- Notice that the Map class above contains variables tl, br. Think of these variables to hold top-left, and bottom-right sections of a map. Note that these data members are of type Point.
- In Point, create public getter functions for the variables x and y.
- Create an overloaded constructor for Map to pass in two variables of type Point.
- In Map, create a getter function that calculates the area bounded by tl and br. Note that tl may not necessarily always hold values that are top-left in relation to br. Regardless, tl and br represent coordinates of two points that are diagonally across from each other. The area bounded by these two points can be calculated by getting the difference between their respective x and y properties. Consider using absolute value when calculating the differences.
- In main(), instantiate two Point objects. For each of the two objects, call set_coords() with some values that correspond to x and y coordinates on a Cartesian plane. See sample interaction.
- In main()
- Instantiate a Map object (you can name the object anything you want). Note that the Map class only contains one constructor and your instantiation MUST call the overloaded constructor, passing in the appropriate parameter variables.
- calculate and display the area bounded by the two points.
- ADDITIONAL:
- Instantiate another Map object in main. You will also have to create additional Point objects for this additional Map object.
- Create a global non-class function bool detect_collision(Map m1, Map m2). This function returns true if m1 overlaps/collides with m2, and false otherwise.
- Call the detect_collision() function to test if your two Map objects collide. Be sure to try different maps with different points to test for collision and non-collision
Sample main()
// class definitions here int main() { Point p1, p2; p1.set_coords(3, 3); p2.set_coords(6, 7); // instantiate a map object // display total area bounded by p1 and p2 return 0; }
LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT
CATALOG ID: CPP-CHAL0057
Print Requirements