< previous page page_158 next page >

Page 158
LISTING 10.5 continued
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
21:
22:     Rectangle::Rectangle():
23:     itsWidth(5),
24:     itsLength(10)
25:     {}
26:
27:     Rectangle::~Rectangle()
28:     {}
29:
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() <<  feet\n;
37:           cout << pConstRect width:  ;
38:           cout << pConstRect->GetWidth() <<  feet\n;
39:           cout << pConstPtr width:  << pConstPtr->GetWidth()
               <<  feet\n;
40:
41:           pRect->SetWidth(10);
42:           // pConstRect->SetWidth(10);
43:           pConstPtr->SetWidth(10);
44:
45:           cout << pRect width:  << pRect->GetWidth() <<  feet\n;
46:           cout << pConstRect width: ;
47:           cout << pConstRect->GetWidth() <<  feet\n;
48:           cout << pConstPtr width:  << pConstPtr->GetWidth()
               <<  feet\n;
49:           return 0;
50:     }

Output:
pRect width:      5 feet
pConstRect width: 5 feet
pConstPtr width:  5 feet
pRect width:      10 feet
pConstRect width: 5 feet
pConstPtr width:  10 feet
Analysis: Lines 620 declare Rectangle. Line 12 declares the GetWidth() member method const. Line 32 declares a pointer to Rectangle. Line 33 declares pConstRect, which is a pointer to the constant Rectangle. Line 34 declares pConstPtr, which is a constant pointer to Rectangle.
Lines 3638 print the value of the widths.

 
< previous page page_158 next page >

If you like this book, buy it!