Challenge 57

Description

The purpose of this challenge is to define basic classes.

 Requirements

  1. 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; }
    };
  2. 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()
        {
        }
    };
  3. Create Point 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. 
  4. In Point, create public getter functions for the variables and y.
  5. Create an overloaded constructor for Map to pass in two variables of type Point.
  6. 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 properties. Consider using absolute value when calculating the differences.
  7. In main(), instantiate two Point objects. For each of the two objects, call set_coords() with some values that correspond to and coordinates on a Cartesian plane. See sample interaction.
  8. In main() calculate and display the area bounded by the two points.

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