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::cerr;
5 using std::cout;
6 using std::cin;
7 using std::endl;
8
9 #include <iomanip>
10 using std::setw;
11
12 #include <cstdlib>
13 using std::exit;
14
15 #include "Array.h"
16
17
18 Array::Array( int arraySize )
19 {
20 size = ( arraySize > 0 ? arraySize : 10 );
21 ptr = new int[ size ];
22
23 for ( int i = 0; i < size; i++ )
24 ptr[ i ] = 0;
25 }
26
27
28
29 Array::Array( const Array &arrayToCopy )
30 : size( arrayToCopy.size )
31 {
32 ptr = new int[ size ];
33
34 for ( int i = 0; i < size; i++ )
35 ptr[ i ] = arrayToCopy.ptr[ i ];
36 }
37
38
39 Array::~Array()
40 {
41 delete [] ptr;
42 }
43
44
45 int Array::getSize() const
46 {
47 return size;
48 }
49
50
51
52 const Array &Array::operator=( const Array &right )
53 {
54 if ( &right != this )
55 {
56
57
58 if ( size != right.size )
59 {
60 delete [] ptr;
61 size = right.size;
62 ptr = new int[ size ];
63 }
64
65 for ( int i = 0; i < size; i++ )
66 ptr[ i ] = right.ptr[ i ];
67 }
68
69 return *this;
70 }
71
72
73
74 bool Array::operator==( const Array &right ) const
75 {
76 if ( size != right.size )
77 return false;
78
79 for ( int i = 0; i < size; i++ )
80 if ( ptr[ i ] != right.ptr[ i ] )
81 return false;
82
83 return true;
84 }
85
86
87
88 int &Array::operator[]( int subscript )
89 {
90
91 if ( subscript < 0 || subscript >= size )
92 {
93 cerr << "\nError: Subscript " << subscript
94 << " out of range" << endl;
95 exit( 1 );
96 }
97
98 return ptr[ subscript ];
99 }
100
101
102
103 int Array::operator[]( int subscript ) const
104 {
105
106 if ( subscript < 0 || subscript >= size )
107 {
108 cerr << "\nError: Subscript " << subscript
109 << " out of range" << endl;
110 exit( 1 );
111 }
112
113 return ptr[ subscript ];
114 }
115
116
117
118 istream &operator>>( istream &input, Array &a )
119 {
120 for ( int i = 0; i < a.size; i++ )
121 input >> a.ptr[ i ];
122
123 return input;
124 }
125
126
127 ostream &operator<<( ostream &output, const Array &a )
128 {
129 int i;
130
131
132 for ( i = 0; i < a.size; i++ )
133 {
134 output << setw( 12 ) << a.ptr[ i ];
135
136 if ( ( i + 1 ) % 4 == 0 )
137 output << endl;
138 }
139
140 if ( i % 4 != 0 )
141 output << endl;
142
143 return output;
144 }
|
Fig. 11.7 Array class member- and friend-function definitions.