C++ allows programmers to specify how operators work with objects of new class types--a concept known as operator overloading. One example of an overloaded operator built into C++ is <<, which is used both as the stream insertion operator and as the bitwise left-shift operator. Similarly, >> is used as both the stream extraction operator and as the bitwise right-shift operator.
This tutorial discusses an Array class that overloads several operators. Our Array class provides enhanced functionality over traditional C++ arrays, such as assigning and comparing Array objects, and checking array indices to ensure that we do not access elements outside the bounds of the underlying C++ array. In addition, this tutorial introduces a copy constructor for initializing a new Array object with the contents of an existing Array object. This tutorial is intended for students and professionals who are familiar with basic array, pointer and class concepts in C++.
Download the code examples for this tutorial.
[Note: This tutorial is an excerpt (Section 11.8) of Chapter 11, Operator Overloading, from our textbook C++ How to Program, 5/e. These
tutorials may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., C++ HOW TO PROGRAM, ©2005, pp.582-593. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
11.8 Case Study: Array Class (Continued)
1
2
3 #include <iostream>
4 using std::cout;
5 using std::cin;
6 using std::endl;
7
8 #include "Array.h"
9
10 int main()
11 {
12 Array integers1( 7 );
13 Array integers2;
14
15
16 cout << "Size of Array integers1 is "
17 << integers1.getSize()
18 << "\nArray after initialization:\n" << integers1;
19
20
21 cout << "\nSize of Array integers2 is "
22 << integers2.getSize()
23 << "\nArray after initialization:\n" << integers2;
24
25
26 cout << "\nEnter 17 integers:" << endl;
27 cin >> integers1 >> integers2;
28
29 cout << "\nAfter input, the Arrays contain:\n"
30 << "integers1:\n"<< integers1
31 << "integers2:\n"<< integers2;
32
33
34 cout << "\nEvaluating: integers1 != integers2" << endl;
35
36 if ( integers1 != integers2 )
37 cout << "integers1 and integers2 are not equal" << endl;
38
39
40
41 Array integers3( integers1 );
42
43 cout << "\nSize of Array integers3 is "
44 << integers3.getSize()
45 << "\nArray after initialization:\n" << integers3;
46
47
48 cout << "\nAssigning integers2 to integers1:" << endl;
49 integers1 = integers2;
50
51 cout << "integers1:\n"<< integers1
52 << "integers2:\n"<< integers2;
53
54
55 cout << "\nEvaluating: integers1 == integers2" << endl;
56
57 if ( integers1 == integers2 )
58 cout << "integers1 and integers2 are equal" << endl;
59
60
61 cout << "\nintegers1[5] is " << integers1[ 5 ];
62
63
64 cout << "\n\nAssigning 1000 to integers1[5]" << endl;
65 integers1[ 5 ] = 1000;
66 cout << "integers1:\n"<< integers1;
67
68
69 cout << "\nAttempt to assign 1000 to integers1[15]" << endl;
70 integers1[ 15 ] = 1000; // ERROR: out of range
71 return 0;
72 }
|
Size of Array integers1 is 7
Array after initialization:
0 0 0 0
0 0 0
Size of Array integers2 is 10
Array after initialization:
0 0 0 0
0 0 0 0
0 0
Enter 17 integers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
After input, the Arrays contain:
integers1:
1 2 3 4
5 6 7
integers2:
8 9 10 11
12 13 14 15
16 17
Evaluating: integers1 != integers2
integers1 and integers2 are not equal
Size of Array integers3 is 7
Array after initialization:
1 2 3 4
5 6 7
Assigning integers2 to integers1:
integers1:
8 9 10 11
12 13 14 15
16 17
integers2:
8 9 10 11
12 13 14 15
16 17
Evaluating: integers1 == integers2
integers1 and integers2 are equal
integers1[5] is 13
Assigning 1000 to integers1[5]
integers1:
8 9 10 11
12 1000 14 15
16 17
Attempt to assign 1000 to integers1[15]
Error: Subscript 15 out of range
|
Fig. 11.8 Array class test program.