|
|
 |
|
|
|
|
21:
22: Rectangle::Rectangle()
23: {
24: itsWidth = 5;
25: itsLength = 10;
26: }
27: Rectangle::~Rectangle()
28: {}
29:
30: int main()
31: {
32: Rectangle theRect;
33: cout << theRect is << theRect.GetLength()
<< feet long.\n;
34: cout << theRect is << theRect.GetWidth()
<< feet wide.\n;
35: theRect.SetLength(20);
36: theRect.SetWidth(10);
37: cout << theRect is << theRect.GetLength()
<< feet long.\n;
38: cout << theRect is << theRect.GetWidth()
<< feet wide.\n;
39: return 0;
40: } |
|
|
|
|
|
|
|
|
theRect is 10 feet long
theRect is 5 feet long
theRect is 20 feet long
theRect is 10 feet long |
|
|
|
|
|
|
|
|
Analysis: The SetLength() and GetLength() accessor functions explicitly use the this pointer to access the member variables of the Rectangle object. The SetWidth and GetWidth accessors do not. There is no difference in their behavior, although the syntax is easier to understand. |
|
|
|
|
|
|
|
|
What's the this Pointer For? |
|
|
|
|
|
|
|
|
If that's all there is to the this pointer, there would be little point in bothering you with it. The this pointer, however, is a pointer, which means it stores the memory address of an object. As such, it can be a powerful tool. |
|
|
|
|
|
|
|
|
You'll see a practical use for the this pointer later in the book, when operator overloading is discussed. For now, your goal is to know about the this pointer and to understand what it is: a pointer to the object itself. |
|
|
|
|
|