Help Understanding Classes

This is a discussion on Help Understanding Classes within the C++ Programming forums, part of the General Programming Boards category; I am reading "Tech Yourself C++ in 21 Days." In the book is the following code (it takes the form ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    9

    Help Understanding Classes

    I am reading "Tech Yourself C++ in 21 Days." In the book is the following code (it takes the form of two listings, one is a header file and the other actual code including the header file)

    Rect.hpp
    Code:
    1:   // Begin Rect.hpp
    2:   #include <iostream.h>
    3:   class Point     // holds x,y coordinates
    4:   {
    5:      // no constructor, use default
    6:      public:
    7:         void SetX(int x) { itsX = x; }
    8:         void SetY(int y) { itsY = y; }
    9:         int GetX()const { return itsX;}
    10:        int GetY()const { return itsY;}
    11:     private:
    12:        int itsX;
    13:        int itsY;
    14:  };    // end of Point class declaration
    15:
    16:
    17:  class  Rectangle
    18:  {
    19:     public:
    20:        Rectangle (int top, int left, int bottom, int right);
    21:        ~Rectangle () {}
    22:
    23:        int GetTop() const { return itsTop; }
    24:        int GetLeft() const { return itsLeft; }
    25:        int GetBottom() const { return itsBottom; }
    26:        int GetRight() const { return itsRight; }
    27:
    28:        Point  GetUpperLeft() const { return itsUpperLeft; }
    29:        Point  GetLowerLeft() const { return itsLowerLeft; }
    30:        Point  GetUpperRight() const { return itsUpperRight; }
    31:        Point  GetLowerRight() const { return itsLowerRight; }
    32:
    33:        void SetUpperLeft(Point Location)  {itsUpperLeft = Location;}
    34:        void SetLowerLeft(Point Location)  {itsLowerLeft = Location;}
    35:        void SetUpperRight(Point Location)  {itsUpperRight = Location;}
    36:        void SetLowerRight(Point Location)  {itsLowerRight = Location;}
    37:
    38:        void SetTop(int top) { itsTop = top; }
    39:        void SetLeft (int left) { itsLeft = left; }
    40:        void SetBottom (int bottom) { itsBottom = bottom; }
    41:        void SetRight (int right) { itsRight = right; }
    42:
    43:        int GetArea() const;
    44:
    45:     private:
    46:        Point  itsUpperLeft;
    47:        Point  itsUpperRight;
    48:        Point  itsLowerLeft;
    49:        Point  itsLowerRight;
    50:        int    itsTop;
    51:        int    itsLeft;
    52:        int    itsBottom;
    53:        int    itsRight;
    54:  };
    55: // end Rect.hpp
    Rect.cpp
    Code:
    1:   // Begin rect.cpp
    2:   #include "rect.hpp"
    3:   Rectangle::Rectangle(int top, int left, int bottom, int right)
    4:   {
    5:         itsTop = top;
    6:         itsLeft = left;
    7:         itsBottom = bottom;
    8:         itsRight = right;
    9:
    10:        itsUpperLeft.SetX(left);
    11:        itsUpperLeft.SetY(top);
    12:
    13:        itsUpperRight.SetX(right);
    14:        itsUpperRight.SetY(top);
    15:
    16:        itsLowerLeft.SetX(left);
    17:        itsLowerLeft.SetY(bottom);
    18:
    19:        itsLowerRight.SetX(right);
    20:        itsLowerRight.SetY(bottom);
    21:  }
    22:
    23:
    24:  // compute area of the rectangle by finding corners,
    25:  // establish width and height and then multiply
    26:  int Rectangle::GetArea() const
    27:  {
    28:        int Width = itsRight-itsLeft;
    29:        int Height = itsTop - itsBottom;
    30:        return (Width * Height);
    31:  }
    32:
    33:  int main()
    34:  {
    35:        //initialize a local Rectangle variable
    36:        Rectangle MyRectangle (100, 20, 50, 80 );
    37:
    38:        int Area = MyRectangle.GetArea();
    39:
    40:        cout << "Area: " << Area << "\n";
    41:        cout << "Upper Left X Coordinate: ";
    42:        cout << MyRectangle.GetUpperLeft().GetX();
    43:      return 0;
    44: }
    That is the exact code from the book. Anyway, I am pretty confused by how it is using the Point class...

    Thank you.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Note that the Point class has x and y member variables.
    A Point object must be seen as a co-ordinate.
    A rectangle has four corners, so the Rectangle class must have has four Points/Co-ordinates, top-left, top-right, bottom-left, bottom-right.

    I hope this helps.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    9
    What about on line 28-31 on the header file - why is it prefixed by 'Point' ? Is it using the point class for something I cannot see?

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    9
    Oh, because all of the function from line 28-31 return private member variables that are Point - why are they using Point?

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    No.

    Point GetUpperLeft() const { return itsUpperLeft; }
    Point GetLowerLeft() const { return itsLowerLeft; }
    Point GetUpperRight() const { return itsUpperRight; }
    Point GetLowerRight() const { return itsLowerRight; }

    These functions return a Point object, so that you'd have access to the appropriate x and y co-ordinate.

    Eg.
    Code:
    Rectangle one;
    cout << one.GetUpperLeft().GetX();
    That would print the x-coordinate of the upper-left coner of the rectangle.

    This code is just for illustration. Don't do stuff like that because the variables have not been initialized.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    9
    Ahh! I finaly get it now!!

    I feel a great overwhelming power of joy!



    Thanks The Dog!

  7. #7
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Anytime!

    btw you might wanna get yourself another book.

    I've noticed that the teach yourself in 21 day series is a bit crappy for beginners.

    Just a suggestion.

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    9
    Suggestion noted

    Any suggestion on waht book - because I actually am thinking of getting one.

  9. #9
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Here's one:

    C++ From the Beginning by Jan Skansholm

    Do a google search, you're bound to find something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes partial understanding
    By Boomba in forum C++ Programming
    Replies: 3
    Last Post: 06-13-2004, 07:44 AM
  2. Having Trouble Understanding Classes
    By prog-bman in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2004, 12:44 PM
  3. i need help understanding classes
    By jmprich in forum C++ Programming
    Replies: 1
    Last Post: 06-10-2003, 07:10 PM
  4. I'm having a really hard time understanding classes.
    By imortal in forum C++ Programming
    Replies: 4
    Last Post: 05-25-2003, 01:02 PM
  5. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 01:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21