0% found this document useful (0 votes)
65 views17 pages

3.operator Overloading

Object Oriented Programming in C++ ...Slides Notes About the Classes .This is is Third Slides related to Operator Overloading in C++

Uploaded by

zainzahid99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views17 pages

3.operator Overloading

Object Oriented Programming in C++ ...Slides Notes About the Classes .This is is Third Slides related to Operator Overloading in C++

Uploaded by

zainzahid99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

5/14/2014

Lecture 4 Operator
Overloading (C++)
Lecture adapted from
C++ How to Program (9th Edition)
By Paul Deitel and Harvey Deitel
Prentice Hall, Inc. All rights reserved.

CS162 Object Oriented Programming


Imran Ihsan
www.imranihsan.com
OOP 04 - Operator
Overloading

imranihsan.com

+ 4.1 Introduction

Use operators with objects (operator overloading)

Examples

Clearer than function calls for certain classes


Operator sensitive to context
<<
Stream insertion, bitwise left-shift
+
Performs arithmetic on multiple types (integers, floats, etc.)

Will discuss when to use operator overloading

imranihsan.com

OOP 04 - Operator
Overloading

5/14/2014

+ 4.2

Types

Fundamentals of Operator Overloading

Built in (int, char) or user-defined


Can use existing operators with user-defined types
Cannot create new operators

Overloading operators

Create a function for the class


Name function operator followed by symbol
Operator+ for the addition operator +

imranihsan.com

+ 4.2

Fundamentals of Operator Overloading

Using operators on a class object

OOP 04 - Operator
Overloading

It must be overloaded for that class


Exceptions:
Assignment operator, =
Memberwise assignment between objects
Address operator, &
Returns address of object
Both can be overloaded

Overloading provides concise notation

object2 = object1.add(object2);
object2 = object2 + object1;

imranihsan.com

OOP 04 - Operator
Overloading

5/14/2014

+ 4.3 Restrictions on Operator Overloading

Cannot change

How operators act on built-in data types


I.e., cannot change integer addition
Precedence of operator (order of evaluation)
Use parentheses to force order-of-operations
Associativity (left-to-right or right-to-left)
Number of operands
& is unitary, only acts on one operand

Cannot create new operators

Operators must be overloaded explicitly

Overloading + does not overload +=

OOP 04 - Operator
Overloading

imranihsan.com

+ 4.3 Restrictions on Operator Overloading


Operators that can be overloaded
+

&

<

>

+=

-=

*=

/=

%=

^=

&=

|=

<<

>>

>>=

<<=

==

!=

<=

>=

&&

||

++

--

->*

->

[]

()

new

delete

new[]

delete[]

Operators that cannot be overloaded


.

imranihsan.com

.*

::

?:

sizeof

OOP 04 - Operator
Overloading

5/14/2014

+ 4.4

Operator Functions As Class Members


Vs. As Friend Functions

Operator functions

Member functions
Use this keyword to implicitly get argument
Gets left operand for binary operators (like +)
Leftmost object must be of same class as operator
Non member functions
Need parameters for both operands
Can have object of different class than operator
Must be a friend to access private or protected data
Called when
Left operand of binary operator of same class
Single operand of unitary operator of same class

imranihsan.com

OOP 04 - Operator
Overloading

+ 4.4

Operator Functions As Class Members


Vs. As Friend Functions

Overloaded << operator

Left operand of type ostream &


Such as cout object in cout << classObject
Similarly, overloaded >> needs istream &
Thus, both must be non-member functions

imranihsan.com

OOP 04 - Operator
Overloading

5/14/2014

+ 4.4

Operator Functions As Class Members


Vs. As Friend Functions

Commutative operators

May want + to be commutative


So both a + b and b + a work
Suppose we have two different classes
Overloaded operator can only be member function when its class
is on left
HugeIntClass + Long int
Can be member function
When other way, need a non-member overload function
Long int + HugeIntClass

imranihsan.com

OOP 04 - Operator
Overloading

+ 4.5

Overloading Stream-Insertion and


Stream-Extraction Operators

<< and >>

Already overloaded to process each built-in type


Can also process a user-defined class

Example program

Class PhoneNumber
Holds a telephone number
Print out formatted number automatically
(123) 456-7890

imranihsan.com

OOP 04 - Operator
Overloading

5/14/2014

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

// Fig. 8.3: fig08_03.cpp


// Overloading the stream-insertion and
// stream-extraction operators.
#include <iostream>
using
using
using
using
using

Notice function prototypes for


overloaded operators >> and <<
They must be non-member friend
functions, since the object of class
Phonenumber appears on the right of
the operator.
cin << object
cout >> object

std::cout;
std::cin;
std::endl;
std::ostream;
std::istream;

#include <iomanip>
using std::setw;

// PhoneNumber class definition


class PhoneNumber {
friend ostream &operator<<( ostream&, const PhoneNumber & );
friend istream &operator>>( istream&, PhoneNumber & );
private:
char areaCode[ 4 ];
char exchange[ 4 ];
char line[ 5 ];

// 3-digit area code and null


// 3-digit exchange and null
// 4-digit line and null

}; // end class PhoneNumber

OOP 04 - Operator
Overloading

imranihsan.com

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

// overloaded stream-insertion operator; cannot be


// a member function if we would like to invoke it with
// cout << somePhoneNumber;
ostream &operator<<( ostream &output, const PhoneNumber &num )
{
The expression:
output << "(" << num.areaCode << ") "
cout << phone;
<< num.exchange << "-" << num.line;
return output;

// enables cout << a << b << c;

} // end function operator<<

is interpreted as the function call:


operator<<(cout, phone);
output is an alias for cout.

// overloaded stream-extraction operator; cannot be


// a member function if we would like to invoke it with
// cin >> somePhoneNumber;
istream &operator>>( istream &input, PhoneNumber &num )
{
input.ignore();
// skip (
input >> setw( 4 ) >> num.areaCode; // input area code
input.ignore( 2 );
// skip ) and space
input >> setw( 4 ) >> num.exchange; // input exchange
input.ignore();
// skip dash (-)
input >> setw( 5 ) >> num.line;
// input line
return input;

Next, cout << phone2


executes.

// enables cin >> a >> b >> c;

ignore() skips specified


number of characters from
input (1 by default).
imranihsan.com

This allows objects to be


cascaded.
cout << phone1 << phone2;
first calls
operator<<(cout, phone1),
and returns cout.

Stream manipulator setw restricts


number of characters read. setw(4)
allows 3 characters to be read,
leaving room for the null character.

OOP 04 - Operator
Overloading

5/14/2014

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

} // end function operator>>


int main()
{
PhoneNumber phone; // create object phone
cout << "Enter phone number in the form (123) 456-7890:\n";
// cin >> phone invokes operator>> by implicitly issuing
// the non-member function call operator>>( cin, phone )
cin >> phone;
cout << "The phone number entered was: " ;
// cout << phone invokes operator<< by implicitly issuing
// the non-member function call operator<<( cout, phone )
cout << phone << endl;
return 0;
} // end main

Enter phone number in the form (123) 456-7890:


(800) 555-1212
The phone number entered was: (800) 555-1212

imranihsan.com

+ 4.6

OOP 04 - Operator
Overloading

Overloading Unary Operators

Overloading unary operators

Non-static member function, no arguments


Non-member function, one argument
Argument must be class object or reference to class object
Remember, static functions only access static data

imranihsan.com

OOP 04 - Operator
Overloading

5/14/2014

+ 4.6

Overloading Unary Operators

Upcoming example

Overload ! to test for empty string


If non-static member function, needs no arguments
!s becomes s.operator!()
class String {
public:
bool operator!() const;
...
};
If non-member function, needs one argument
s! becomes operator!(s)
class String {
friend bool operator!( const String & )
...
}

imranihsan.com

+ 4.7

Overloading Binary Operators

Overloading binary operators

OOP 04 - Operator
Overloading

Non-static member function, one argument


Non-member function, two arguments
One argument must be class object or reference

Upcoming example

If non-static member function, needs one argument


class String {
public:
const String &operator+=( const String & );
...
};
y += z equivalent to y.operator+=( z )

imranihsan.com

OOP 04 - Operator
Overloading

5/14/2014

+ 4.7

Overloading Binary Operators

Upcoming example

If non-member function, needs two arguments


Example:
class String {
friend const String &operator+=(
String &, const String & );
...
};
y += z equivalent to operator+=( y, z )

imranihsan.com

+ 4.8

Case Study: Array class

Arrays in C++

OOP 04 - Operator
Overloading

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 !=

imranihsan.com

OOP 04 - Operator
Overloading

5/14/2014

+ 4.8

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

OOP 04 - Operator
Overloading

imranihsan.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

// Fig. 8.4: array1.h


// Array class for storing arrays of integers.
#ifndef ARRAY1_H
#define ARRAY1_H
#include <iostream>
using std::ostream;
using std::istream;
class Array {
friend ostream &operator<<( ostream &, const Array & );
friend istream &operator>>( istream &, Array & );
public:
Array( int = 10 );
Array( const Array & );
~Array();
int getSize() const;

//
//
//
//

default constructor
copy constructor
destructor
return size

// assignment operator
const Array &operator=( const Array & );
// equality operator
bool operator==( const Array & ) const;

imranihsan.com

OOP 04 - Operator
Overloading

10

5/14/2014

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

// inequality operator; returns opposite of == operator


bool operator!=( const Array &right ) const
{
return ! ( *this == right ); // invokes Array::operator==
} // end function operator!=
// subscript operator for non-const objects returns lvalue
int &operator[]( int );
// subscript operator for const objects returns rvalue
const int &operator[]( int ) const;
private:
int size; // array size
int *ptr; // pointer to first element of array
}; // end class Array
#endif

OOP 04 - Operator
Overloading

imranihsan.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

// Fig 8.5: array1.cpp


// Member function definitions for class Array
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::setw;
#include <new>

// C++ standard "new" operator

#include <cstdlib>

// exit function prototype

#include "array1.h"

// Array class definition

// default constructor for class Array (default size 10)


Array::Array( int arraySize )
{
// validate arraySize
size = ( arraySize > 0 ? arraySize : 10 );
ptr = new int[ size ]; // create space for array

imranihsan.com

OOP 04 - Operator
Overloading

11

5/14/2014

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

for ( int i = 0; i < size; i++ )


ptr[ i ] = 0;
// initialize array
} // end Array default constructor
// copy constructor for class Array;
// must receive a reference to prevent infinite recursion
Array::Array( const Array &arrayToCopy )
: size( arrayToCopy.size )
{
ptr = new int[ size ]; // create space for array
for ( int i = 0; i < size; i++ )
ptr[ i ] = arrayToCopy.ptr[ i ];

// copy into object

} // end Array copy constructor


// destructor for class Array
Array::~Array()
{
delete [] ptr; // reclaim array space
} // end destructor

OOP 04 - Operator
Overloading

imranihsan.com

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

// return size of array


int Array::getSize() const
{
return size;
} // end function getSize
// overloaded assignment operator;
// const return avoids: ( a1 = a2 ) = a3
const Array &Array::operator=( const Array &right )
{
if ( &right != this ) { // check for self-assignment
// for arrays of different sizes, deallocate original
// left-side array, then allocate new left-side array
if ( size != right.size ) {
delete [] ptr;
// reclaim space
size = right.size;
// resize this object
ptr = new int[ size ]; // create space for array copy
} // end inner if
for ( int i = 0; i < size; i++ )
ptr[ i ] = right.ptr[ i ]; // copy array into object
} // end outer if

imranihsan.com

OOP 04 - Operator
Overloading

12

5/14/2014

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

return *this;

// enables x = y = z, for example

} // end function operator=


// determine if two arrays are equal and
// return true, otherwise return false
bool Array::operator==( const Array &right ) const
{
if ( size != right.size )
return false;
// arrays of different sizes
for ( int i = 0; i < size; i++ )
if ( ptr[ i ] != right.ptr[ i ] )
return false; // arrays are not equal
return true;

// arrays are equal

} // end function operator==

OOP 04 - Operator
Overloading

imranihsan.com

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

// overloaded subscript operator for non-const Arrays


// reference return creates an lvalue
int &Array::operator[]( int subscript )
{
// check for subscript out of range error
if ( subscript < 0 || subscript >= size ) {
cout << "\nError: Subscript " << subscript
<< " out of range" << endl;
exit( 1 );

// terminate program; subscript out of range

} // end if
return ptr[ subscript ]; // reference return
} // end function operator[]

imranihsan.com

OOP 04 - Operator
Overloading

13

5/14/2014

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

// overloaded subscript operator for const Arrays


// const reference return creates an rvalue
const int &Array::operator[]( int subscript ) const
{
// check for subscript out of range error
if ( subscript < 0 || subscript >= size ) {
cout << "\nError: Subscript " << subscript
<< " out of range" << endl;
exit( 1 );

// terminate program; subscript out of range

} // end if
return ptr[ subscript ]; // const reference return
} // end function operator[]
// overloaded input operator for class Array;
// inputs values for entire array
istream &operator>>( istream &input, Array &a )
{
for ( int i = 0; i < a.size; i++ )
input >> a.ptr[ i ];
return input;

// enables cin >> x >> y;

} // end function

OOP 04 - Operator
Overloading

imranihsan.com

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

// overloaded output operator for class Array


ostream &operator<<( ostream &output, const Array &a )
{
int i;
// output private ptr-based array
for ( i = 0; i < a.size; i++ ) {
output << setw( 12 ) << a.ptr[ i ];
if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output
output << endl;
} // end for
if ( i % 4 != 0 ) // end last line of output
output << endl;
return output;

// enables cout << x << y;

} // end function operator<<

imranihsan.com

OOP 04 - Operator
Overloading

14

5/14/2014

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

// Fig. 8.6: fig08_06.cpp


// Array class test program.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "array1.h"
int main()
{
Array integers1( 7 );
Array integers2;

// seven-element Array
// 10-element Array by default

// print integers1 size and contents


cout << "Size of array integers1 is "
<< integers1.getSize()
<< "\nArray after initialization:\n" << integers1;
// print integers2 size and contents
cout << "\nSize of array integers2 is "
<< integers2.getSize()
<< "\nArray after initialization:\n" << integers2;

imranihsan.com

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

OOP 04 - Operator
Overloading

// input and print integers1 and integers2


cout << "\nInput 17 integers:\n";
cin >> integers1 >> integers2;
cout << "\nAfter input, the arrays contain:\n"
<< "integers1:\n" << integers1
<< "integers2:\n" << integers2;
// use overloaded inequality (!=) operator
cout << "\nEvaluating: integers1 != integers2\n";
if ( integers1 != integers2 )
cout << "integers1 and integers2 are not equal\n";
// create array integers3 using integers1 as an
// initializer; print size and contents
Array integers3( integers1 ); // calls copy constructor
cout << "\nSize of array integers3 is "
<< integers3.getSize()
<< "\nArray after initialization:\n" << integers3;

imranihsan.com

OOP 04 - Operator
Overloading

15

5/14/2014

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

// use overloaded assignment (=) operator


cout << "\nAssigning integers2 to integers1:\n";
integers1 = integers2; // note target is smaller
cout << "integers1:\n" << integers1
<< "integers2:\n" << integers2;
// use overloaded equality (==) operator
cout << "\nEvaluating: integers1 == integers2\n";
if ( integers1 == integers2 )
cout << "integers1 and integers2 are equal\n";
// use overloaded subscript operator to create rvalue
cout << "\nintegers1[5] is " << integers1[ 5 ];
// use overloaded subscript operator to create lvalue
cout << "\n\nAssigning 1000 to integers1[5]\n";
integers1[ 5 ] = 1000;
cout << "integers1:\n" << integers1;
// attempt to use out-of-range subscript
cout << "\nAttempt to assign 1000 to integers1[15]" << endl;
integers1[ 15 ] = 1000; // ERROR: out of range
return 0;
} // end main

OOP 04 - Operator
Overloading

imranihsan.com

+
Size of array integers1 is 7
Array after initialization:
0
0
0
0
Size of array integers2 is 10
Array after initialization:
0
0
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
5
6
integers2:
8
9
12
13

imranihsan.com

3
7

10
14

11
15

OOP 04 - Operator
Overloading

16

5/14/2014

Evaluating: integers1 != integers2


integers1 and integers2 are not equal
Size of array integers3 is 7
Array after initialization:
1
2
5
6
Assigning integers2 to integers1:
integers1:
8
9
12
13
16
17
integers2:
8
9
12
13
16
17

3
7

10
14

11
15

10
14

11
15

Evaluating: integers1 == integers2


integers1 and integers2 are equal
integers1[5] is 13

OOP 04 - Operator
Overloading

imranihsan.com

Assigning 1000 to integers1[5]


integers1:
8
9
12
1000
16
17

10
14

11
15

Attempt to assign 1000 to integers1[15]


Error: Subscript 15 out of range

imranihsan.com

OOP 04 - Operator
Overloading

17

You might also like