Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
C++ How to Program, 5/e

ISBN:
0-13-185757-6
© 2005
pages: 1500
Buy the Book!
Amazon logo
InformIT logo

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)

The program of Figs. 11.6—11.8 demonstrates class Array and its overloaded operators. First we walk through main (Fig. 11.8). Then we consider the class definition (Fig. 11.6) and each of the class’s member-function and friend-function definitions (Fig. 11.7).

 1  // Fig. 11.6: Array.h
 2  // Array class for storing arrays of integers.
 3  #ifndef ARRAY_H
 4  #define ARRAY_H
 5
 6  #include <iostream>
 7  using std::ostream;
 8  using std::istream;
 9
10  class Array
11  {
12     friend ostream &operator<<( ostream &, const Array & );
13     friend istream &operator>>( istream &, Array & );
14  public:
15     Array( int = 10 ); // default constructor
16     Array( const Array & ); // copy constructor
17     ~Array(); // destructor
18     int getSize() const; // return size
19
20     const Array &operator=( const Array & ); // assignment operator
21     bool operator==( const Array & ) const; // equality operator
22
23     // inequality operator; returns opposite of == operator
24     bool operator!=( const Array &right ) const
25     {
26        return ! ( *this == right ); // invokes Array::operator==
27     } // end function operator!=
28
29     // subscript operator for non-const objects returns modifiable lvalue
30     int &operator[]( int );
31
32     // subscript operator for const objects returns rvalue
33     int operator[]( int ) const;
34  private:
35     int size; // pointer-based array size
36     int *ptr; // pointer to first element of pointer-based array
37  }; // end class Array
38
39  #endif

Fig. 11.6 Array class definition with overloaded operators.

Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
Tutorial Index

Google