Return to Existing Everywhere Main Page

C++ Classes and Objects: Point and Rectangle

 

C++ is a programming language used in computer programming and software engineering. One of the differences between C++ and C, a predecessor of C++, is that C++ uses data stuctures called objects. Objects can contain various data types in one construct. They contain the data along with methods to operate on the data. An object is created when its class is instantiated. A class is like a blueprint for an object and can be considered as a data type along with characters, strings, integers and floating point numbers. Objects and classes provide modularity which is preferred in software engineering and programming. With inheritance and polymorphism, classes and objects allow information, data and processes to be organized in a hierarchy. Besides C++, Java is a programming language that also uses classes and objects.

The following C++ program uses classes and objects to store and process data on the points that define a rectangle. Two types of objects are defined by two different classes. The first class is for the point which is defined by x and y values that are contained in a two dimensional coordinate plane. This class is used by the Rectangle class to store its point values. A point can be entered to determine if it is inside or outside the rectangle. The center and the area of the rectangle is also calculated. Although the code can be run in a single file, it is divided into three to conform with software engineering and computer programming conventions. The first file contains the main function that instantiates the objects and displays output. The second file is the class definitions with the method prototypes and makes up the header file. The third file is the implementation part that defines the methods used in the classes. The other two files are output examples.

File 1:
     /*Title: Programming with C++ classes and structures
         The following tests the classes that are designed to store
	     information about objects on a graphics screen. A Point class stores
         the x and y coordinates of a point on the screen. A Rectangle
         structure stores the coordinates of the upper left and lower
         right corners of a rectangle.

         Modified on 3/23/02.
     */

     #include 
     #include 
     #include "shapes.h"

     using namespace std;

     int main()
       {
       Point p;

       Rectangle R(Point(2,B), Point(10,4));
       Rectangle  R1 = R;

       do {
            cout  << "The area of R = " << R.area() << endl;
            cout  << "\nThe center of R = " << R.center().x
                    << "," << R.center().y << endl;

            cout  << "\nEnter the x-y coordinates of a point" << endl;
            cout  << "x  ? "; cin >> p.x;
            cout  << "y  ? "; cin >> p.y;

            if (R.inside(p) == true)
                  cout << p.x << "," << p.y << " is inside of R\n\n";

            else
                  cout << p.x << "," << p.y << " is outside of R\n\n";

            R1.move(+4,-2);

            cout << "The center of R1 = "
                  << R1.center().x <<  ","  << R1.center().y;
            cout << "\nHit CR for more, CTRL-z to exit" << endl;

            while ( getchar() != '\n');

          }while (cin.get() != EOF);

       return 0;
       }

File 2:
     /* Shapes.H ------------------------------------------------------------
         This header file defines the data types Point and Rectangle for processing
         points.
         Basic operations:
              area:            To find the area
              center:          To find the center
              move:            To move the Rectangle
              inside:          To see if a point falls inside the Rectangle
         Written on 3/23/02.
     */


      class Point
      {
         public:
         Point(int mx=0,int my=0) { x=mx; y=my;}
         int x, y;
      };


      class Rectangle
      {
         public:
              Rectangle(Point p1=Point(0,0), Point p2=Point(1,1));
              int area();
              Point center();
              void move(int dx, int dy);
              bool inside(Point p);
         private:
              Point upper_left, lower_right;
      };


File 3:
     /*    shapes.cpp implements the Rectangle member functions */

     #include 
     #include 
     #include "shapes.h"

     using namespace std;

     Rectangle::Rectangle(Point pl, Point p2)
     {
         upper_left = pl;
         lower-right = p2;
     }

     int Rectangle::area()
     {
         return abs((upper_left.x - lower_right.x) * (upper_left.y - lower_right.y));
     }

     Point Rectangle::centero
     {
         Point cen;

         cen.x = (upper_left.x + lower_right.x)/2;
         cen.y = (upper_left.y + lower_right.y)/2;

         return cen;
     }

     void Rectangle::move(int dx, int dy)
     {
         upper_left.x = upper_left.x + dx;
         upper_left.y = upper_left.y + dy;

         lower_right.x   lower_right.x + dx;
         lower_right.y   lower_right.y + dy;
     }

     bool Rectangle::inside(Point p)
     {
         if ( p.x < lower_right.x && p.x > upper_left.x &&
             p.y > lower_right.y && p.y < upper_left.y )
             return true;

         else
             return false;
     }



File 4:
                                Shapes_output.txt








        The area of R = 32

        The center of R = 6,6

        Enter the x-y coordinates of a point
        x = ? 0
        y = ? 0
        0,0 is outside of R

        The center of R1 =  10,4
        Hit CR for more, CTRL-z to exit
        4
        The area of R = 32

        The center of R = 6,6

        Enter the x-y coordinates of a point
        x = ? 5
        y = ? 4
        5,4 is outside of R

        The center of R1 =  14,2
        Hit CR for more, CTRL-z to exit





File 5:
                                   Shapes_output.txt






        The area of R =  32

        The center of R =  6,6

        Enter the x-y coordinates of a point
        x = ? 5
        y = ? 6
        5,6 is inside of R

        The center of R1 = 18,0
        Hit CR for more, CTRL-z to exit

        The area of R = 32

        The center of R =  6,6

        Enter the x-y coordinates of a point
        x = ? 120
        y = ? 340
        120,340 is outside of R

        The center of R1 = 22,-2
        Hit CR for more, CTRL-z to exit
        Press any key to continue