30: int main()
31: {
32: Rectangle* pRect = new Rectangle;
33: const Rectangle * pConstRect = new Rectangle;
34: Rectangle * const pConstPtr = new Rectangle;
35:
36: cout << “pRect width: “ << pRect->GetWidth()
37: << “ feet” << endl;
38: cout << “pConstRect width: “ << pConstRect->GetWidth()
39: << “ feet” << endl;
40: cout << “pConstPtr width: “ << pConstPtr->GetWidth()
41: << “ feet” << endl;
42:
43: pRect->SetWidth(10);
44: // pConstRect->SetWidth(10);
45: pConstPtr->SetWidth(10);
46:
47: cout << “pRect width: “ << pRect->GetWidth()
48: << “ feet\n”;
49: cout << “pConstRect width: “ << pConstRect->GetWidth()
50: << “ feet\n”;
51: cout << “pConstPtr width: “ << pConstPtr->GetWidth()
52: << “ feet\n”;
53: return 0;
54: }
pRect width: 5 feet
pConstRect width: 5 feet
pConstPtr width: 5 feet
pRect width: 10 feet
pConstRect width: 5 feet
pConstPtr width: 10 feet
Lines 6–19 declare the Rectangleclass. Line 14 declares the GetWidth()member
method const.
Line 32 declares a pointer to Rectanglecalled pRect. On line 33, a pointer to a constant
Rectangleobject is declared and named pConstRect. On line 34,pConstPtris declared
as a constant pointer to a Rectangle. Lines 36–41 print the values of these three variables.
On line 43,pRectis used to set the width of the rectangle to 10. On line 44,pConstRect
would be used to set the width, but it was declared to point to a constant Rectangle.
Therefore, it cannot legally call a non-constmember function. Because it is not a valid
statement, it is commented out.
On line 45,pConstPtrcalls SetWidth(). pConstPtris declared to be a constant pointer
to a rectangle. In other words, the pointer is constant and cannot point to anything else,
OUTPUT
250 Day 8
LISTING8.10 continued
ANALYSIS