Rectangle Class - Using a class as member data

Hello, I am a beginner programming in C++ and have searched for explained examples of "rectangle" classes, but have only found simple examples that do not use classes that use other classes as member data.

I would like to know more in-detail what happens in the program when a class uses a class as member data, and how the following line of code works:

MyRectangle.GetUpperLeft().GetY()
Is this line interpreted by the compiler as MyRectangle."ValueReturnedByGetUpperLeft".GetY? How is the function call "GetUpperLeft()" handled when it is used in this way?

Here is the Point class: (later used as member data in the Rectangle class)

1
2
3
4
5
6
7
8
9
10
11
12
   class Point     // holds x,y coordinates
   {
      // no constructor, use default
      public:
         void SetX(int x) { itsX = x; }
         void SetY(int y) { itsY = y; }
         int GetX()const { return itsX;}
        int GetY()const { return itsY;}
     private:
        int itsX;
        int itsY;
  };    // end of Point class declaration 



Here is the Rectangle class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  class  Rectangle
  {
     public:
        Rectangle (int top, int left, int bottom, int right);
        ~Rectangle () {} //not implemented here, implemented in .cpp
		//file

		//access to a private data member in rectangle class
        int GetTop() const { return itsTop; }
        int GetLeft() const { return itsLeft; }
        int GetBottom() const { return itsBottom; }
        int GetRight() const { return itsRight; }

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

        void SetUpperLeft(Point Location)  {itsUpperLeft = Location;}
        void SetLowerLeft(Point Location)  {itsLowerLeft = Location;}
        void SetUpperRight(Point Location)  {itsUpperRight = Location;}
        void SetLowerRight(Point Location)  {itsLowerRight = Location;}

        void SetTop	(int top) { itsTop = top; }
        void SetLeft (int left) { itsLeft = left; }
        void SetBottom (int bottom) { itsBottom = bottom; }
        void SetRight (int right) { itsRight = right; }

        int GetArea() const;

     private:
        Point  itsUpperLeft;
        Point  itsUpperRight;
        Point  itsLowerLeft;
        Point  itsLowerRight;
        int    itsTop;
        int    itsLeft;
        int    itsBottom;
        int    itsRight;
  };


What happens in the program when the void SetLowerRight(Point Location) is written; how is that "Point Location" parameter handled? What exactly does it mean to create an instance of class Point in the private member data section of the class (what is the purpose for doing this)?

These classes are taken from a tutorial I am reading about classes and structures in C++.

I will be more than happy to fill in any missing information and/or clarify my question if it is not clear. Thanks in advance for your time in reading this and any help.
1. Yes
2. Private means non-class member functions cannot access it or use it. It is for encapsulation purposes.

Read:

http://www.cplusplus.com/doc/tutorial/classes/
Alright, I read through that tutorial and have a better understanding.

Is there a way to set the debugger to get the value of each variable in the two classes during run time? (I am using Microsoft Visual C++), I would like to be able to see more of what each line is doing/what is being accessed and with what values. Thanks for the reply.
Though it's not in front of me right now, I believe msvc++ has a "watch list" window you can activate where you can type (or just drag) in the variable names into the window. The values should show up next to the variable names.

There are a couple other windows that automatically show values for the currently in-scope variables as well.
Topic archived. No new replies allowed.