- Arrays in C++
- No range checking
- Cannot be compared meaningfully with ==
- No array assignment (array names const pointers)
- Cannot input/output entire arrays at once; One element at a time
- Example:Implement an Array class with
- Range checking
- Array assignment
- Arrays that know their size
- Outputting/inputting entire arrays with and
- Array comparisons with == and !=
- Copy constructor
- Used whenever copy of object needed
- Passing by value (return value or parameter)
- Initializing an object with a copy of another; Array newArray( oldArray );
- newArray copy of oldArray
- Prototype for class Array
- Array( const Array & );
- Must take reference
- Otherwise, pass by value
- Tries to make copy by calling copy constructor
- Infinite loop
The program of Figs. 3.3-3.11 demonstrates class Array and its overloaded operators.
Figure 3.3:
Array class definition with overloaded operators.
|
Figure 3.4:
Array class member-and friend-function definitions. (part 1 of 4)
|
Figure 3.5:
Array class member-and friend-function definitions. (part 2 of 4)
|
Figure 3.6:
Array class member-and friend-function definitions. (part 3 of 4)
|
Figure 3.7:
Overloaded stream-insertion and stream extraction operators. (part 4 of 2)
|
Figure 3.8:
Array class test program. (part 1 of 2)
|
Figure 3.9:
Array class test program. (part 2 of 2)
|
Figure 3.10:
Array class test program, output. (part 1 of 2)
|
Figure 3.11:
Array class test program, output. (part 2 of 2)
|
2004-07-29