SlideShare a Scribd company logo
LECTURE 16
Operator Overloading
Array Class Case Study
Case Study: Array class
• 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 !=
Case Study: Array class
• 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
array1.h (1 of 2)
1 // Fig. 8.4: array1.h
2 // Array class for storing arrays of integers.
3 #ifndef ARRAY1_H
4 #define ARRAY1_H
5
6 #include <iostream>
7
8 using std::ostream;
9 using std::istream;
10
11 class Array {
12 friend ostream &operator<<( ostream &, const Array & );
13 friend istream &operator>>( istream &, Array & );
14
15 public:
16 Array( int = 10 ); // default constructor
17 Array( const Array & ); // copy constructor
18 ~Array(); // destructor
19 int getSize() const; // return size
20
21 // assignment operator
22 const Array &operator=( const Array & );
23
24 // equality operator
25 bool operator==( const Array & ) const;
26
Most operators overloaded as
member functions (except <<
and >>, which must be non-
member functions).
Prototype for copy constructor.
array1.h (2 of 2)
27 // inequality operator; returns opposite of == operator
28 bool operator!=( const Array &right ) const
29 {
30 return ! ( *this == right ); // invokes Array::operator==
31
32 } // end function operator!=
33
34 // subscript operator for non-const objects returns lvalue
35 int &operator[]( int );
36
37 // subscript operator for const objects returns rvalue
38 const int &operator[]( int ) const;
39
40 private:
41 int size; // array size
42 int *ptr; // pointer to first element of array
43
44 }; // end class Array
45
46 #endif
!= operator simply returns
opposite of == operator.
Thus, only need to define the
== operator.
array1.cpp (1 of 7)
1 // Fig 8.5: array1.cpp
2 // Member function definitions for class Array
3 #include <iostream>
4
5 using std::cout;
6 using std::cin;
7 using std::endl;
8
9 #include <iomanip>
10
11 using std::setw;
12
13 #include <new> // C++ standard "new" operator
14
15 #include <cstdlib> // exit function prototype
16
17 #include "array1.h" // Array class definition
18
19 // default constructor for class Array (default size 10)
20 Array::Array( int arraySize )
21 {
22 // validate arraySize
23 size = ( arraySize > 0 ? arraySize : 10 );
24
25 ptr = new int[ size ]; // create space for array
26
array1.cpp (2 of 7)
27 for ( int i = 0; i < size; i++ )
28 ptr[ i ] = 0; // initialize array
29
30 } // end Array default constructor
31
32 // copy constructor for class Array;
33 // must receive a reference to prevent infinite recursion
34 Array::Array( const Array &arrayToCopy )
35 : size( arrayToCopy.size )
36 {
37 ptr = new int[ size ]; // create space for array
38
39 for ( int i = 0; i < size; i++ )
40 ptr[ i ] = arrayToCopy.ptr[ i ]; // copy into object
41
42 } // end Array copy constructor
43
44 // destructor for class Array
45 Array::~Array()
46 {
47 delete [] ptr; // reclaim array space
48
49 } // end destructor
50
We must declare a new integer array so
the objects do not point to the same
memory.
array1.cpp (3 of 7)
51 // return size of array
52 int Array::getSize() const
53 {
54 return size;
55
56 } // end function getSize
57
58 // overloaded assignment operator;
59 // const return avoids: ( a1 = a2 ) = a3
60 const Array &Array::operator=( const Array &right )
61 {
62 if ( &right != this ) { // check for self-assignment
63
64 // for arrays of different sizes, deallocate original
65 // left-side array, then allocate new left-side array
66 if ( size != right.size ) {
67 delete [] ptr; // reclaim space
68 size = right.size; // resize this object
69 ptr = new int[ size ]; // create space for array copy
70
71 } // end inner if
72
73 for ( int i = 0; i < size; i++ )
74 ptr[ i ] = right.ptr[ i ]; // copy array into object
75
76 } // end outer if
Want to avoid self-assignment.
array1.cpp (4 of 7)
77
78 return *this; // enables x = y = z, for example
79
80 } // end function operator=
81
82 // determine if two arrays are equal and
83 // return true, otherwise return false
84 bool Array::operator==( const Array &right ) const
85 {
86 if ( size != right.size )
87 return false; // arrays of different sizes
88
89 for ( int i = 0; i < size; i++ )
90
91 if ( ptr[ i ] != right.ptr[ i ] )
92 return false; // arrays are not equal
93
94 return true; // arrays are equal
95
96 } // end function operator==
97
array1.cpp (5 of 7)
98 // overloaded subscript operator for non-const Arrays
99 // reference return creates an lvalue
100 int &Array::operator[]( int subscript )
101 {
102 // check for subscript out of range error
103 if ( subscript < 0 || subscript >= size ) {
104 cout << "nError: Subscript " << subscript
105 << " out of range" << endl;
106
107 exit( 1 ); // terminate program; subscript out of range
108
109 } // end if
110
111 return ptr[ subscript ]; // reference return
112
113 } // end function operator[]
114
integers1[5] calls
integers1.operator[]( 5 )
exit() (header <cstdlib>) ends
the program.
array1.cpp (6 of 7)
115 // overloaded subscript operator for const Arrays
116 // const reference return creates an rvalue
117 const int &Array::operator[]( int subscript ) const
118 {
119 // check for subscript out of range error
120 if ( subscript < 0 || subscript >= size ) {
121 cout << "nError: Subscript " << subscript
122 << " out of range" << endl;
123
124 exit( 1 ); // terminate program; subscript out of range
125
126 } // end if
127
128 return ptr[ subscript ]; // const reference return
129
130 } // end function operator[]
131
132 // overloaded input operator for class Array;
133 // inputs values for entire array
134 istream &operator>>( istream &input, Array &a )
135 {
136 for ( int i = 0; i < a.size; i++ )
137 input >> a.ptr[ i ];
138
139 return input; // enables cin >> x >> y;
140
141 } // end function
array1.cpp (7 of 7)
142
143 // overloaded output operator for class Array
144 ostream &operator<<( ostream &output, const Array &a )
145 {
146 int i;
147
148 // output private ptr-based array
149 for ( i = 0; i < a.size; i++ ) {
150 output << setw( 12 ) << a.ptr[ i ];
151
152 if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output
153 output << endl;
154
155 } // end for
156
157 if ( i % 4 != 0 ) // end last line of output
158 output << endl;
159
160 return output; // enables cout << x << y;
161
162 } // end function operator<<
fig08_06.cpp
(1 of 3)
1 // Fig. 8.6: fig08_06.cpp
2 // Array class test program.
3 #include <iostream>
4
5 using std::cout;
6 using std::cin;
7 using std::endl;
8
9 #include "array1.h"
10
11 int main()
12 {
13 Array integers1( 7 ); // seven-element Array
14 Array integers2; // 10-element Array by default
15
16 // print integers1 size and contents
17 cout << "Size of array integers1 is "
18 << integers1.getSize()
19 << "nArray after initialization:n" << integers1;
20
21 // print integers2 size and contents
22 cout << "nSize of array integers2 is "
23 << integers2.getSize()
24 << "nArray after initialization:n" << integers2;
25
fig08_06.cpp
(2 of 3)
26 // input and print integers1 and integers2
27 cout << "nInput 17 integers:n";
28 cin >> integers1 >> integers2;
29
30 cout << "nAfter input, the arrays contain:n"
31 << "integers1:n" << integers1
32 << "integers2:n" << integers2;
33
34 // use overloaded inequality (!=) operator
35 cout << "nEvaluating: integers1 != integers2n";
36
37 if ( integers1 != integers2 )
38 cout << "integers1 and integers2 are not equaln";
39
40 // create array integers3 using integers1 as an
41 // initializer; print size and contents
42 Array integers3( integers1 ); // calls copy constructor
43
44 cout << "nSize of array integers3 is "
45 << integers3.getSize()
46 << "nArray after initialization:n" << integers3;
47
fig08_06.cpp
(3 of 3)
48 // use overloaded assignment (=) operator
49 cout << "nAssigning integers2 to integers1:n";
50 integers1 = integers2; // note target is smaller
51
52 cout << "integers1:n" << integers1
53 << "integers2:n" << integers2;
54
55 // use overloaded equality (==) operator
56 cout << "nEvaluating: integers1 == integers2n";
57
58 if ( integers1 == integers2 )
59 cout << "integers1 and integers2 are equaln";
60
61 // use overloaded subscript operator to create rvalue
62 cout << "nintegers1[5] is " << integers1[ 5 ];
63
64 // use overloaded subscript operator to create lvalue
65 cout << "nnAssigning 1000 to integers1[5]n";
66 integers1[ 5 ] = 1000;
67 cout << "integers1:n" << integers1;
68
69 // attempt to use out-of-range subscript
70 cout << "nAttempt to assign 1000 to integers1[15]" << endl;
71 integers1[ 15 ] = 1000; // ERROR: out of range
72
73 return 0;
74
75 } // end main
fig08_06.cpp
output (1 of 3)
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
Input 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
fig08_06.cpp
output (2 of 3)
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
fig08_06.cpp
output (3 of 3)
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
Thank You

More Related Content

PPTX
Synapse india dotnet development overloading operater part 3
PPT
Operator overloading
PPT
operator overloading concept in oops lecture12.ppt
PPT
OOP OOOOOverloading Concept lecture12.ppt
PPT
lecture12.ppt
PPT
Operator overloading in c++ is the most required.
PPT
Object oriented programming; operator overloading array
PPTX
Operator Overloading & Type Conversions
Synapse india dotnet development overloading operater part 3
Operator overloading
operator overloading concept in oops lecture12.ppt
OOP OOOOOverloading Concept lecture12.ppt
lecture12.ppt
Operator overloading in c++ is the most required.
Object oriented programming; operator overloading array
Operator Overloading & Type Conversions

Similar to Operator overloading Object Oriented Programming (20)

PPT
Lecture#5-Arrays-oral patholohu hfFoP.ppt
PPT
Lecture#9 Arrays in c++
PPT
Lecture#8 introduction to array with examples c++
PDF
C++ Nested loops, matrix and fuctions.pdf
PDF
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
PDF
Arrays and strings in c++
DOCX
Use a simple vector you created before to create two other more
PPT
Arrays
PPTX
CPP Homework Help
PDF
Assignment of Advanced data structure and algorithms..pdf
PDF
Assignment of Advanced data structure and algorithms ..pdf
PDF
Assignment of Advanced data structure and algorithm ..pdf
PPT
Synapse india complain sharing info on chapter 8 operator overloading
PPT
its arrays ppt for first year students .
PPT
lecture7.ppt
PDF
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
PPTX
Chp4(ref dynamic)
PPT
Lecture#6 functions in c++
PDF
GeoGebra JavaScript CheatSheet
PDF
4th_Ed_Ch03.pdf
Lecture#5-Arrays-oral patholohu hfFoP.ppt
Lecture#9 Arrays in c++
Lecture#8 introduction to array with examples c++
C++ Nested loops, matrix and fuctions.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
Arrays and strings in c++
Use a simple vector you created before to create two other more
Arrays
CPP Homework Help
Assignment of Advanced data structure and algorithms..pdf
Assignment of Advanced data structure and algorithms ..pdf
Assignment of Advanced data structure and algorithm ..pdf
Synapse india complain sharing info on chapter 8 operator overloading
its arrays ppt for first year students .
lecture7.ppt
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Chp4(ref dynamic)
Lecture#6 functions in c++
GeoGebra JavaScript CheatSheet
4th_Ed_Ch03.pdf
Ad

Recently uploaded (20)

PDF
PPT on Performance Review to get promotions
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
web development for engineering and engineering
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
Well-logging-methods_new................
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Construction Project Organization Group 2.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Welding lecture in detail for understanding
PPT on Performance Review to get promotions
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
web development for engineering and engineering
Operating System & Kernel Study Guide-1 - converted.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Well-logging-methods_new................
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
UNIT 4 Total Quality Management .pptx
Model Code of Practice - Construction Work - 21102022 .pdf
CH1 Production IntroductoryConcepts.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Arduino robotics embedded978-1-4302-3184-4.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
Construction Project Organization Group 2.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Welding lecture in detail for understanding
Ad

Operator overloading Object Oriented Programming

  • 2. Case Study: Array class • 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 !=
  • 3. Case Study: Array class • 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
  • 4. array1.h (1 of 2) 1 // Fig. 8.4: array1.h 2 // Array class for storing arrays of integers. 3 #ifndef ARRAY1_H 4 #define ARRAY1_H 5 6 #include <iostream> 7 8 using std::ostream; 9 using std::istream; 10 11 class Array { 12 friend ostream &operator<<( ostream &, const Array & ); 13 friend istream &operator>>( istream &, Array & ); 14 15 public: 16 Array( int = 10 ); // default constructor 17 Array( const Array & ); // copy constructor 18 ~Array(); // destructor 19 int getSize() const; // return size 20 21 // assignment operator 22 const Array &operator=( const Array & ); 23 24 // equality operator 25 bool operator==( const Array & ) const; 26 Most operators overloaded as member functions (except << and >>, which must be non- member functions). Prototype for copy constructor.
  • 5. array1.h (2 of 2) 27 // inequality operator; returns opposite of == operator 28 bool operator!=( const Array &right ) const 29 { 30 return ! ( *this == right ); // invokes Array::operator== 31 32 } // end function operator!= 33 34 // subscript operator for non-const objects returns lvalue 35 int &operator[]( int ); 36 37 // subscript operator for const objects returns rvalue 38 const int &operator[]( int ) const; 39 40 private: 41 int size; // array size 42 int *ptr; // pointer to first element of array 43 44 }; // end class Array 45 46 #endif != operator simply returns opposite of == operator. Thus, only need to define the == operator.
  • 6. array1.cpp (1 of 7) 1 // Fig 8.5: array1.cpp 2 // Member function definitions for class Array 3 #include <iostream> 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include <iomanip> 10 11 using std::setw; 12 13 #include <new> // C++ standard "new" operator 14 15 #include <cstdlib> // exit function prototype 16 17 #include "array1.h" // Array class definition 18 19 // default constructor for class Array (default size 10) 20 Array::Array( int arraySize ) 21 { 22 // validate arraySize 23 size = ( arraySize > 0 ? arraySize : 10 ); 24 25 ptr = new int[ size ]; // create space for array 26
  • 7. array1.cpp (2 of 7) 27 for ( int i = 0; i < size; i++ ) 28 ptr[ i ] = 0; // initialize array 29 30 } // end Array default constructor 31 32 // copy constructor for class Array; 33 // must receive a reference to prevent infinite recursion 34 Array::Array( const Array &arrayToCopy ) 35 : size( arrayToCopy.size ) 36 { 37 ptr = new int[ size ]; // create space for array 38 39 for ( int i = 0; i < size; i++ ) 40 ptr[ i ] = arrayToCopy.ptr[ i ]; // copy into object 41 42 } // end Array copy constructor 43 44 // destructor for class Array 45 Array::~Array() 46 { 47 delete [] ptr; // reclaim array space 48 49 } // end destructor 50 We must declare a new integer array so the objects do not point to the same memory.
  • 8. array1.cpp (3 of 7) 51 // return size of array 52 int Array::getSize() const 53 { 54 return size; 55 56 } // end function getSize 57 58 // overloaded assignment operator; 59 // const return avoids: ( a1 = a2 ) = a3 60 const Array &Array::operator=( const Array &right ) 61 { 62 if ( &right != this ) { // check for self-assignment 63 64 // for arrays of different sizes, deallocate original 65 // left-side array, then allocate new left-side array 66 if ( size != right.size ) { 67 delete [] ptr; // reclaim space 68 size = right.size; // resize this object 69 ptr = new int[ size ]; // create space for array copy 70 71 } // end inner if 72 73 for ( int i = 0; i < size; i++ ) 74 ptr[ i ] = right.ptr[ i ]; // copy array into object 75 76 } // end outer if Want to avoid self-assignment.
  • 9. array1.cpp (4 of 7) 77 78 return *this; // enables x = y = z, for example 79 80 } // end function operator= 81 82 // determine if two arrays are equal and 83 // return true, otherwise return false 84 bool Array::operator==( const Array &right ) const 85 { 86 if ( size != right.size ) 87 return false; // arrays of different sizes 88 89 for ( int i = 0; i < size; i++ ) 90 91 if ( ptr[ i ] != right.ptr[ i ] ) 92 return false; // arrays are not equal 93 94 return true; // arrays are equal 95 96 } // end function operator== 97
  • 10. array1.cpp (5 of 7) 98 // overloaded subscript operator for non-const Arrays 99 // reference return creates an lvalue 100 int &Array::operator[]( int subscript ) 101 { 102 // check for subscript out of range error 103 if ( subscript < 0 || subscript >= size ) { 104 cout << "nError: Subscript " << subscript 105 << " out of range" << endl; 106 107 exit( 1 ); // terminate program; subscript out of range 108 109 } // end if 110 111 return ptr[ subscript ]; // reference return 112 113 } // end function operator[] 114 integers1[5] calls integers1.operator[]( 5 ) exit() (header <cstdlib>) ends the program.
  • 11. array1.cpp (6 of 7) 115 // overloaded subscript operator for const Arrays 116 // const reference return creates an rvalue 117 const int &Array::operator[]( int subscript ) const 118 { 119 // check for subscript out of range error 120 if ( subscript < 0 || subscript >= size ) { 121 cout << "nError: Subscript " << subscript 122 << " out of range" << endl; 123 124 exit( 1 ); // terminate program; subscript out of range 125 126 } // end if 127 128 return ptr[ subscript ]; // const reference return 129 130 } // end function operator[] 131 132 // overloaded input operator for class Array; 133 // inputs values for entire array 134 istream &operator>>( istream &input, Array &a ) 135 { 136 for ( int i = 0; i < a.size; i++ ) 137 input >> a.ptr[ i ]; 138 139 return input; // enables cin >> x >> y; 140 141 } // end function
  • 12. array1.cpp (7 of 7) 142 143 // overloaded output operator for class Array 144 ostream &operator<<( ostream &output, const Array &a ) 145 { 146 int i; 147 148 // output private ptr-based array 149 for ( i = 0; i < a.size; i++ ) { 150 output << setw( 12 ) << a.ptr[ i ]; 151 152 if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output 153 output << endl; 154 155 } // end for 156 157 if ( i % 4 != 0 ) // end last line of output 158 output << endl; 159 160 return output; // enables cout << x << y; 161 162 } // end function operator<<
  • 13. fig08_06.cpp (1 of 3) 1 // Fig. 8.6: fig08_06.cpp 2 // Array class test program. 3 #include <iostream> 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include "array1.h" 10 11 int main() 12 { 13 Array integers1( 7 ); // seven-element Array 14 Array integers2; // 10-element Array by default 15 16 // print integers1 size and contents 17 cout << "Size of array integers1 is " 18 << integers1.getSize() 19 << "nArray after initialization:n" << integers1; 20 21 // print integers2 size and contents 22 cout << "nSize of array integers2 is " 23 << integers2.getSize() 24 << "nArray after initialization:n" << integers2; 25
  • 14. fig08_06.cpp (2 of 3) 26 // input and print integers1 and integers2 27 cout << "nInput 17 integers:n"; 28 cin >> integers1 >> integers2; 29 30 cout << "nAfter input, the arrays contain:n" 31 << "integers1:n" << integers1 32 << "integers2:n" << integers2; 33 34 // use overloaded inequality (!=) operator 35 cout << "nEvaluating: integers1 != integers2n"; 36 37 if ( integers1 != integers2 ) 38 cout << "integers1 and integers2 are not equaln"; 39 40 // create array integers3 using integers1 as an 41 // initializer; print size and contents 42 Array integers3( integers1 ); // calls copy constructor 43 44 cout << "nSize of array integers3 is " 45 << integers3.getSize() 46 << "nArray after initialization:n" << integers3; 47
  • 15. fig08_06.cpp (3 of 3) 48 // use overloaded assignment (=) operator 49 cout << "nAssigning integers2 to integers1:n"; 50 integers1 = integers2; // note target is smaller 51 52 cout << "integers1:n" << integers1 53 << "integers2:n" << integers2; 54 55 // use overloaded equality (==) operator 56 cout << "nEvaluating: integers1 == integers2n"; 57 58 if ( integers1 == integers2 ) 59 cout << "integers1 and integers2 are equaln"; 60 61 // use overloaded subscript operator to create rvalue 62 cout << "nintegers1[5] is " << integers1[ 5 ]; 63 64 // use overloaded subscript operator to create lvalue 65 cout << "nnAssigning 1000 to integers1[5]n"; 66 integers1[ 5 ] = 1000; 67 cout << "integers1:n" << integers1; 68 69 // attempt to use out-of-range subscript 70 cout << "nAttempt to assign 1000 to integers1[15]" << endl; 71 integers1[ 15 ] = 1000; // ERROR: out of range 72 73 return 0; 74 75 } // end main
  • 16. fig08_06.cpp output (1 of 3) 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 Input 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
  • 17. fig08_06.cpp output (2 of 3) 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
  • 18. fig08_06.cpp output (3 of 3) 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