Advertisement

Questions about Classes

Started by September 16, 2002 09:35 PM
2 comments, last by jchen 22 years, 2 months ago
Listing 6.8. Declaring a complete class. 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 Listing 6.9. RECT.CPP. 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: } Output: Area: 3000 Upper Left X Coordinate: 20 An excerpt from SAMS Teach Yourself C++ in 21 Days. My questions are: 1. In lines 28-31 for listing 6.8, what is "Point" doing there? What does this do? 2. In rectangle() for Listing 6.9, how the hell is it calling 10-20? 3. AHHHH! DAMNIT, this is confusing. If anyone could help break it down for me in an understandable way, that''d be great.
28: Point GetUpperLeft() const { return itsUpperLeft; }
29: Point GetLowerLeft() const { return itsLowerLeft; }30: Point GetUpperRight() const { return itsUpperRight; }31: Point GetLowerRight() const { return itsLowerRight; } 


1. These functions are returning a point. This is just as if you had a function "int GetUpperLeft()..." that returned an integer, but instead it''s returning a point.

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); 


2. This is how the method of a class is called. Here''s an example:

  class MyClass {private:    int value;public:    void SetValue (int val);};void main () {    MyClass foobar;    foobar.SetValue (69);}  

To access the members of an object, you use a period and the name of the member.

- Jay

"I have head-explody!!!" - NNY

Get Tranced!
Quit screwin' around! - Brock Samson
Advertisement
Ok, but where does itsUpperLeft, itsUpperRight, etc come from? Don''t you have to define an object first, or is it just getting those from the private sector of Rectangle()? Even so, aren''t those just variables? How can you call a member through a variable?

(Sorry if some of these questions don''t make sense)
itsUpperLeft, itsUpperRight... are declared in the private sector of the Rectangle class. You don''t need to declare an object to use them because the compiler knows that if they are being used, an object has already been declared.
Yes they are variables, but since they are variables of type Point, you can call any of Point''s public member functions through them using the "." operator as coderx75 said.
The difference between us and a computer is that, the computer is blindingly stupid, but it is capable of being stupid many, many million times a second.-Douglas Adams

This topic is closed to new replies.

Advertisement