Sams Teach Yourself C++ in 21 Days

(singke) #1
LISTING8.8 Using the thisPointer
1: // Listing 8.8
2: // Using the this pointer
3:
4: #include <iostream>
5:
6: class Rectangle
7: {
8: public:
9: Rectangle();
10: ~Rectangle();
11: void SetLength(int length)
12: { this->itsLength = length; }
13: int GetLength() const
14: { return this->itsLength; }
15:
16: void SetWidth(int width)
17: { itsWidth = width; }
18: int GetWidth() const
19: { return itsWidth; }
20:
21: private:
22: int itsLength;
23: int itsWidth;
24: };
25:
26: Rectangle::Rectangle()
27: {
28: itsWidth = 5;
29: itsLength = 10;
30: }
31: Rectangle::~Rectangle()
32: {}
33:
34: int main()
35: {
36: using namespace std;
37: Rectangle theRect;
38: cout << “theRect is “ << theRect.GetLength()
39: << “ feet long.” << endl;
40: cout << “theRect is “ << theRect.GetWidth()
41: << “ feet wide.” << endl;
42: theRect.SetLength(20);
43: theRect.SetWidth(10);
44: cout << “theRect is “ << theRect.GetLength()
45: << “ feet long.” << endl;
46: cout << “theRect is “ << theRect.GetWidth()
47: << “ feet wide. “ << endl;
48: return 0;
49: }

244 Day 8

Free download pdf