Sams Teach Yourself C++ in 21 Days
21: itsWidth = 5; 22: itsLength = 10; 23: } 24: 25: Rectangle::Rectangle (int width, int length) 26: { 27: itsWidth = width; 28: ...
Working with Advanced Functions 297 10 Initializing Objects .................................................................... ...
Line 8 contains the second constructor definition. In this overloaded version, two parameters are passed. These parameters are t ...
Working with Advanced Functions 299 10 FIGURE10.1 Using the default copy constructor. 5 itsAge old CAT itsAge New CAT Free Store ...
21: Cat::Cat() 22: { 23: itsAge = new int; 24: itsWeight = new int; 25: *itsAge = 5; 26: *itsWeight = 9; 27: } 28: 29: Cat::Cat( ...
Working with Advanced Functions 301 10 On lines 6–19, the Catclass is declared. Note that on line 9 a default constructor is dec ...
When the Cats fall out of scope, their destructors are automatically invoked. The imple- mentation of the Catdestructor is shown ...
Working with Advanced Functions 303 10 15: int itsVal; 16: }; 17: 18: Counter::Counter(): 19: itsVal(0) 20: {} 21: 22: int main( ...
12: void SetItsVal(int x) {itsVal = x; } 13: void Increment() { ++itsVal; } 14: 15: private: 16: int itsVal; 17: }; 18: 19: Coun ...
Working with Advanced Functions 305 10 4: #include <iostream> 5: using namespace std; 6: 7: class Counter 8: { 9: public: ...
This code intends to create a new Counter,a, and then assign to it the value in iafter i is incremented. The built-in copy const ...
Working with Advanced Functions 307 10 36: Counter i; 37: cout << “The value of i is “ << i.GetItsVal() << end ...
14: void SetItsVal(int x) {itsVal = x; } 15: void Increment() { ++itsVal; } 16: Counter operator++ (); 17: 18: private: 19: int ...
Working with Advanced Functions 309 10 This is more elegant, but raises the question, “Why create a temporary object at all?” Re ...
33: Counter i; 34: cout << “The value of i is “ << i.GetItsVal() << endl; 35: i.Increment(); 36: cout << ...
Working with Advanced Functions 311 10 Overloading the Postfix Operator ........................................................ ...
9: public: 10: Counter(); 11: ~Counter(){} 12: int GetItsVal()const { return itsVal; } 13: void SetItsVal(int x) {itsVal = x; } ...
Working with Advanced Functions 313 10 The value of i is 0 The value of i is 1 The value of i is 2 The value of a: 3 and i: 3 Th ...
15: Counter Add(const Counter &); 16: 17: private: 18: int itsVal; 19: }; 20: 21: Counter::Counter(int initialValue): 22: it ...
Working with Advanced Functions 315 10 Overloading the Addition Operator (operator+) Overloading the +operator would make for a ...
«
12
13
14
15
16
17
18
19
20
21
»
Free download pdf