Lecture11 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 110

1

11
Operator
Overloading;
String and Array
Objects
2008 Pearson Education, Inc. All rights reserved.

OBJECTIVES
In this chapter youll learn:
 What operator overloading is and how it makes programs more
readable and programming more convenient.
 To redefine (overload) operators to work with objects of userdefined classes.
 The differences between overloading unary and binary
operators.
 To convert objects from one class to another class.
 When to, and when not to, overload operators.
 To create PhoneNumber, Array, String and Date classes
that demonstrate operator overloading.
 To use overloaded operators and other member functions of
standard library class string.
 To use keyword explicit to prevent the compiler from using
single-argument constructors to perform implicit conversions.
2008 Pearson Education, Inc. All rights reserved.

11.1
11.2
11.3
11.4
11.5
11.6
11.7
11.8
11.9
11.10
11.11
11.12
11.13
11.14
11.15

Introduction
Fundamentals of Operator Overloading
Restrictions on Operator Overloading
Operator Functions as Class Members vs. Global Functions
Overloading Stream Insertion and Stream Extraction
Operators
Overloading Unary Operators
Overloading Binary Operators
Case Study: Array Class
Converting between Types
Case Study: String Class
Overloading ++ and -Case Study: A Date Class
Standard Library Class string
explicit Constructors
Wrap-Up

2008 Pearson Education, Inc. All rights reserved.

11.1 Introduction
Use operators with objects (operator overloading)
Clearer than function calls for certain classes
Operator sensitive to context

Examples
<<
Stream insertion, bitwise left-shift

+
Performs arithmetic on multiple items (integers, floats, etc.)

2008 Pearson Education, Inc. All rights reserved.

11.2 Fundamentals of Operator


Overloading

Types for operator overloading


Built in (int
int,
int char)
char or user-defined (classes)
Can use existing operators with user-defined types
Cannot create new operators

Overloading operators
Create a function for the class
Name of operator function
Keyword operator followed by symbol
Example
operator+ for the addition operator +

2008 Pearson Education, Inc. All rights reserved.

Software Engineering Observation 11.1


Operator overloading contributes to C++s
extensibilityone of the languages most
appealing attributes.

2008 Pearson Education, Inc. All rights reserved.

Good Programming Practice 11.1


Use operator overloading when it makes
a program clearer than accomplishing
the same operations with function calls.

2008 Pearson Education, Inc. All rights reserved.

Good Programming Practice 11.2


Overloaded operators should mimic the
functionality of their built-in counterpartsfor
example, the + operator should be overloaded to
perform addition, not subtraction. Avoid excessive
or inconsistent use of operator overloading, as this
can make a program cryptic and difficult to read.

2008 Pearson Education, Inc. All rights reserved.

11.2 Fundamentals of Operator


Overloading (Cont.)

Using operators on a class object


It must be overloaded for that class
Exceptions: (can also be overloaded by the programmer)
Assignment operator (=
=)
Memberwise assignment between objects
Address operator (&
&)
Returns address of object
Comma operator (,
,)
Evaluates expression to its left then the expression to its
right
Returns the value of the expression to its right

Overloading provides concise notation


object2 = object1.add( object2 );

vs.

object2 = object2 + object1;


2008 Pearson Education, Inc. All rights reserved.

11.3 Restrictions on Operator


Overloading

10

Cannot change
Precedence of operator (order of evaluation)
Use parentheses to force order of operators

Associativity (left-to-right or right-to-left)


Number of operands
e.g., & is unary, can only act on one operand

How operators act on built-in data types (i.e., cannot change


integer addition)

Cannot create new operators


Operators must be overloaded explicitly
Overloading + and = does not overload +=

Operator ?: cannot be overloaded

2008 Pearson Education, Inc. All rights reserved.

11

Common Programming Error 11.1


Attempting to overload a nonoverloadable
operator is a syntax error.

2008 Pearson Education, Inc. All rights reserved.

12

Operators that can be overloaded


+
~

*
=

/
<

%
>

^
+=

&
-=

|
*=

/=

%=

^=

&=

|=

<<

>>

>>=

<<=

==

!=

<=

>=

&&

||

++

--

->*

->

[]

()

new

delete

new[]
new[]

delete[]
delete[]

Fig. 11.1 | Operators that can be overloaded.

2008 Pearson Education, Inc. All rights reserved.

13

Operators that cannot be overloaded


.

.*

::

?:

Fig. 11.2 | Operators that cannot be overloaded.

2008 Pearson Education, Inc. All rights reserved.

14

Common Programming Error 11.2


Attempting to change the arity of an operator
via operator overloading is a compilation error.

2008 Pearson Education, Inc. All rights reserved.

15

Common Programming Error 11.3


Attempting to create new operators via operator
overloading is a syntax error.

2008 Pearson Education, Inc. All rights reserved.

16

Software Engineering Observation 11.2


At least one argument of an operator function
must be an object or reference of a user-defined
type. This prevents programmers from changing
how operators work on fundamental types.

2008 Pearson Education, Inc. All rights reserved.

17

Common Programming Error 11.4


Attempting to modify how an operator works
with objects of fundamental types is a compilation
error.

2008 Pearson Education, Inc. All rights reserved.

18

Common Programming Error 11.5


Assuming that overloading an operator such
as + overloads related operators such as += or
that overloading == overloads a related
operator like != can lead to errors. Operators
can be overloaded only explicitly; there is no
implicit overloading.

2008 Pearson Education, Inc. All rights reserved.

11.4 Operator Functions as Class


Members vs. Global Members

19

Operator functions
As member functions
Leftmost object must be of same class as operator function
Use this keyword to implicitly get left operand argument
Operators (),
() [],
[] -> or any assignment operator must be
overloaded as a class member function
Called when
Left operand of binary operator is of this class
Single operand of unary operator is of this class

As global functions
Need parameters for both operands
Can have object of different class than operator
Can be a friend to access private or protected data
2008 Pearson Education, Inc. All rights reserved.

11.4 Operator Functions as Class


Members vs. Global Members (Cont.)

20

Overloaded << operator


Left operand of type ostream &
Such as cout object in cout << classObject

Similarly, overloaded >> has left operand of istream &


Thus, both must be global functions

2008 Pearson Education, Inc. All rights reserved.

21

Performance Tip 11.1


It is possible to overload an operator as a global,
non-friend
friend function, but such a function
requiring access to a classs private or
protected data would need to use set or get
functions provided in that classs public
interface. The overhead of calling these functions
could cause poor performance, so these functions
can be inlined to improve performance.

2008 Pearson Education, Inc. All rights reserved.

11.4 Operator Functions as Class


Members vs. Global Members (Cont.)

22

Commutative operators
May want + to be commutative
So both a
a + b
b + 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 global overloaded function
long int + HugeIntClass

2008 Pearson Education, Inc. All rights reserved.

11.5 Overloading Stream Insertion and


Stream Extraction Operators

23

<<
<< and >> operators
Already overloaded to process each built-in type
Can also process a user-defined class
Overload using global, friend functions

Example program
Class PhoneNumber
Holds a telephone number

Print out formatted number automatically


(123) 456456-7890

2008 Pearson Education, Inc. All rights reserved.

// Fig. 11.3: PhoneNumber.h

// PhoneNumber class definition

#ifndef PHONENUMBER_H

#define PHONENUMBER_H

Outline

24

5
6

#include <iostream>

using std::ostream;

using std::istream;

PhoneNumber.h

(1 of 1)

9
10 #include <string>
11 using std::string;
12
13 class PhoneNumber
14 {
15

friend ostream &operator<<( ostream &, const PhoneNumber & );

16

friend istream &operator>>( istream &, PhoneNumber & );

17 private:
private:
18

string areaCode; // 33-digit area code

19

string exchange; // 33-digit exchange

20

string line; // 44-digit line

21 }; // end class PhoneNumber


22
23 #endif

Notice function prototypes for overloaded operators


>> and << (must be global, friend functions)

2008 Pearson Education,


Inc. All rights reserved.

// Fig. 11.4: PhoneNumber.cpp

// Overloaded stream insertion and stream extraction operators

// for class PhoneNumber.

#include <iomanip>

using std::setw;

6
7

#include "PhoneNumber.h"

8
9

Outline

25

Allows cout << phone;PhoneNumber.cpp


to be interpreted
as: operator<<(cout, phone);
(1 of 2)

// overloaded stream insertion operator;


operator; cannot be

10 // a member function if we would like to invoke it with


11 // cout << somePhoneNumber;
12 ostream &operator
&operator<<(
operator<<( ostream &output, const PhoneNumber &number )
13 {
14

output << "(" << number.areaCode << ") "

15

<< number.exchange << "-" << number.line;

16

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

Display formatted phone number

17 } // end function operator<<

2008 Pearson Education,


Inc. All rights reserved.

18
19 // overloaded stream extraction operator; cannot be
20 // a member function if we would like to invoke it with

Outline

26

21 // cin >> somePhoneNumber;


22 istream &operator
&operator>>(
operator>>( istream &input, PhoneNumber &number )
23 {
24

input.ignore(); // skip (

25

input >> setw( 3 ) >> number.areaCode; // input area code

26

input.ignore( 2 ); // skip ) and space


space

27

input >> setw( 3 ) >> number.exchange; // input exchange

28

input.ignore(); // skip dash ((-)

29

input >> setw( 4 ) >> number.line; // input line

30

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

ignore skips specified number of


PhoneNumber.cpp
characters from input
(1 by default)
(2 of 2)

Input each portion of


phone number separately

31 } // end function operator>>

2008 Pearson Education,


Inc. All rights reserved.

// Fig. 11.5: fig11_05.cpp

// Demonstrating class PhoneNumber's overloaded stream insertion

// and stream extraction operators.

#include <iostream>

using std::cout;

using std::cin;

using std::endl;

Outline

fig11_05.cpp

(1 of 2)

8
9

27

#include "PhoneNumber.h"
"PhoneNumber.h"

10
11 int main()
12 {
13

PhoneNumber phone; // create object phone

14
15

cout << "Enter phone number in the form (123) 456456-7890:" << endl;

16
17

// cin >> phone invokes operator>> by implicitly issuing

18

// the global function call operator>>( cin, phone )

19

cin >> phone;

20
21

cout << "The phone number entered was: ";


";

22
23

// cout << phone invokes operator<< by implicitly issuing

24

// the global
global function call operator<<( cout, phone )

25

cout << phone << endl;

26

return 0;

Testing overloaded >> and <<


operators to input and output a
PhoneNumber object

27 } // end main

2008 Pearson Education,


Inc. All rights reserved.

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


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

Outline

28

fig11_05.cpp

(2 of 2)

2008 Pearson Education,


Inc. All rights reserved.

29

Error-Prevention Tip 11.1


Returning a reference from an overloaded <<
or >> operator function is typically successful
because cout,
cout cin and most stream objects
are global, or at least long-lived. Returning a
reference to an automatic variable or other
temporary object is dangerouscreating
dangling references to nonexisting objects.

2008 Pearson Education, Inc. All rights reserved.

30

Software Engineering Observation 11.3


New input/output capabilities for user-defined
types are added to C++ without modifying C++s
standard input/output library classes. This is
another example of C++s extensibility.

2008 Pearson Education, Inc. All rights reserved.

31

11.6 Overloading Unary Operators


Overloading unary operators
Can overload as non-static
static member function with no
arguments
Can overload as global function with one argument
Argument must be class object or reference to class object

Remember, static functions only access static data

2008 Pearson Education, Inc. All rights reserved.

32

11.6 Overloading Unary Operators (Cont.)


Upcoming example (Section 11.10)
Overload ! to test for empty string
If non-static
static member function, needs no arguments
class String
{
public:
public:
bool operator!()
operator!() const;
const;

};
!s becomes s.operator
s.operator!()
operator!()
If global function, needs one argument
bool operator!(
operator!( const String & )
s! becomes operator!(s
operator!(s)
!(s)
2008 Pearson Education, Inc. All rights reserved.

33

11.7 Overloading Binary Operators


Overloading binary operators
Non-static
static member function, one argument
Global function, two arguments
One argument must be class object or reference

2008 Pearson Education, Inc. All rights reserved.

11.7 Overloading Binary Operators


(Cont.)

34

Upcoming example: Overloading +=


If non-static
static member function, needs one argument
class String
{
public:
public:
const String & operator+=(
operator+=( const String & );

};
y += z becomes y.operator
operator+=( z )
y.operator+=(

If global function, needs two arguments


const String &operator
&operator+=(
operator+=( String &, const String
& );
y += z becomes operator+=(
operator+=( y, z )

2008 Pearson Education, Inc. All rights reserved.

35

11.8 Case Study: Array Class


Pointer-based arrays in C++

No range checking
Cannot be compared meaningfully with ==
No array assignment (array names are const pointers)
If array passed to a function, size must be passed as a separate
argument

Example: Implement an Array class with

Range checking
Array assignment
Arrays that know their own size
Outputting/inputting entire arrays with << and >>
Array comparisons with == and !=

2008 Pearson Education, Inc. All rights reserved.

36

11.8 Case Study: Array Class (Cont.)


Copy constructor
Used whenever copy of object is needed:
Passing by value (return value or parameter)
Initializing an object with a copy of another of same type
Array newArray(
newArray( oldArray ); or
Array newArray = oldArray (both are identical)
newArray is a copy of oldArray

Prototype for class Array


Array( const Array & );
Must take reference
Otherwise, the argument will be passed by value
Which tries to make copy by calling copy constructor
Infinite loop
2008 Pearson Education, Inc. All rights reserved.

// Fig. 11.6: Array.h

// Array class for storing arrays of integers.

#ifndef ARRAY_H

#define ARRAY_H

Outline

37

5
6

#include <iostream>

using std::ostream;

using std::istream;

Array.h

(1 of 2)

9
10 class Array
11 {
12

friend ostream &operator<<(


&operator<<( ostream &, const Array & );

13

friend istream &operator>>( istream &, Array & );

14 public:
public:
15

Array( int = 10 ); // default constructor

16

Array( const Array & ); // copy constructor

17

~Array(); // destructor

18

int getSize() const;


const; // return size

Most operators overloaded as


member functions (except << and
>>, which must be global functions)

19
20

const Array &operator


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

21

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

22
23

// inequality operator; returns opposite of == operator

24

bool operator!=(
operator!=( const Array &right ) const

25

26
27

Prototype for copy constructor

return ! ( *this
*this == right ); // invokes Array::operator==
} // end function operator!=
operator!=

!= operator simply returns opposite of ==


operator only need to define the == operator

2008 Pearson Education,


Inc. All rights reserved.

28
29

// subscript operator for nonnon-const objects returns modifiable lvalue

30

int &operator
&operator[](
operator[]( int );

Outline

38

31
32

// subscript operator for const objects returns rvalue

33

int operator[](
operator[]( int ) const;
const;

Array.h

34 private:
private:
35

int size; // pointerpointer-based array size

36

int *ptr; // pointer to first element of pointerpointer-based

(2 ofspecific
2)
Operators for accessing
array elements of Array object

37 }; // end class Array


38
39 #endif

2008 Pearson Education,


Inc. All rights reserved.

// Fig 11.7: Array.cpp

// MemberMember-function definitions for class Array

#include <iostream>

using std::cerr;

using std::cout;

using std::cin;

using std::endl;

Array.cpp

(1 of 6)

8
9

Outline

39

#include <iomanip>

10 using std::setw;
11
12 #include <cstdlib>
<cstdlib> // exit function prototype
13 using std::exit;
14
15 #include "Array.h" // Array class definition
16
17 // default constructor for class Array (default size 10)
18 Array::Array( int arraySize )
19 {
20

size = ( arraySize > 0 ? arraySize : 10 ); // validate arraySize

21

ptr = new int[


int[ size ]; // create space for pointerpointer-based array

22
23
24

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


ptr[ i ] = 0; // set pointerpointer-based array element

25 } // end Array default constructor


constructor

2008 Pearson Education,


Inc. All rights reserved.

26

Outline

27 // copy constructor for class Array;


28 // must receive a reference to prevent infinite recursion

40

29 Array::Array( const Array &arrayToCopy )


30

: size( arrayToCopy.size )

Array.cpp

31 {
32

ptr = new int[


int[ size ]; // create space for pointer
pointer-based array

(2 of 6)

33
34
35

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


ptr[ i ] = arrayToCopy.ptr[ i ]; // copy into object

36 } // end Array copy constructor


37
38 // destructor for class Array

We must declare a new integer array so the


objects do not point to the same memory

39 Array::~Array()
40 {
41

delete [] ptr; // release pointerpointer-based array space

42 } // end destructor
43
44 // return number of elements of Array
45 int Array::getSize() const
46 {
47

return size; // number of elements in Array

48 } // end function getSize

2008 Pearson Education,


Inc. All rights reserved.

49
50 // overloaded assignment operator;

Outline

41

51 // const return avoids: ( a1 = a2 ) = a3


52 const Array &Array::operator
&Array::operator=(
operator=( const Array &right )
53 {
54

if ( &right != this ) // avoid selfself-assignment

55

56

// for Arrays of different


different sizes, deallocate original

57

// leftleft-side array, then allocate new leftleft-side array

58

if ( size != right.size )

59

60

delete [] ptr; // release space

61

size = right.size; // resize this object

62

ptr = new int[


int[ size ]; // create space for array copy

63

Array.cpp

(3 of 6)
Want to avoid self assignment
This would be dangerous if this
is the same Array as right

} // end inner if

64
65
66
67

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


ptr[ i ] = right.ptr[ i ]; // copy array into object
} // end outer if

68
69

return *this; // enables x = y = z, for example

70 } // end function operator=

2008 Pearson Education,


Inc. All rights reserved.

71
72 // determine if two Arrays are equal and
73 // return true, otherwise return false
74 bool Array::
Array::operator
operator==(
operator
==( const Array &right ) const
75 {

Outline

76
77
78

if ( size != right.size )
return false;
false; // arrays of different number of elements
elements

79
80
81

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


if ( ptr[ i ] != right.ptr[ i ] )
return false;
false; // Array contents are not equal

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

42

Array.cpp

(4 of 6)

return true;
true; // Arrays are equal
} // end function operator==
// overlo
overloaded
aded subscript operator for nonnon-const Arrays;
// reference return creates a modifiable lvalue
int &Array::operator
&Array::operator[](
operator[]( int subscript )
{
// check for subscript outout-ofof-range error
if ( subscript < 0 || subscript >= size )
{

integers1[ 5 ] calls
integers1.operator[]( 5 )

cerr << "


"\
\nError: Subscript " << subscript
<< " out of range" << endl;
exit( 1 ); // terminate program; subscript out of range
} // end if

97
98
return ptr[ subscript ]; // reference return
99 } // end function operator[]

2008 Pearson Education,


Inc. All rights reserved.

100
101 // overloaded subscript operator for const Arrays
102 // const reference return creates an rvalue

Outline

43

103 int Array::operator


Array::operator[](
operator[]( int subscript ) const
104 {
105

// check for subscript outout-ofof-range error

106

if ( subscript < 0 || subscript


subscript >= size )

107

108
109
110
111

Array.cpp

(5 of 6)
cerr << "\nError: Subscript " << subscript
<< " out of range" << endl;
exit( 1 ); // terminate program; subscript out of range

} // end if

112
113

return ptr[ subscript ]; // returns copy of this element

114 } // end function operator[]


115
116 // overloaded input operator for class Array;
117 // inputs values for entire Array
118 istream &operator
&operator>>(
operator>>( istream &input, Array &a )
119 {
120
121

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


input >> a.ptr[ i ];

122
123

return input; // enables cin >> x >> y;

124 } // end function

2008 Pearson Education,


Inc. All rights reserved.

125
126 // overloaded output operator for class Array
127 ostream &operator
&operator<<(
operator<<( ostream &output, const Array &a )

Outline

44

128 {
129

int i;

Array.cpp

130
131

// output private ptrptr-based array

132

for ( i = 0; i < a.size; i++ )

133

134

(6 of 6)

output << setw( 12 ) << a.ptr[ i ];

135
136
137
138

if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output


output << endl;
} // end for

139
140
141

if ( i % 4 != 0 ) // end last line of output


output << endl;

142
143

return output; // enables cout << x << y;

144 } // end function operator<<

2008 Pearson Education,


Inc. All rights reserved.

// Fig. 11.8: fig11_08.cpp

// Array class test program.

#include <iostream>

using std::cout;

using std::cin;

using std::endl;

fig11_08.cpp

#include "Array.h"

(1 of 5)

Outline

45

7
8
9
10 int main()
11 {
12

Array integers1( 7 ); // sevenseven-element Array


Array

13

Array integers2; // 1010-element Array by default

14
15

// print integers1 size and contents

16

cout << "Size of Array integers1 is "

Retrieve number of elements in Array

17

<< integers1.getSize()

18

<< "\nArray after initialization:\


initialization:\n" << integers1;

19
20

// print integers2 size and contents

21

cout << "\nSize of Array integers2 is "

22

<< integers2.getSize()

23

<< "\
"\nArray after initialization:\
initialization:\n" << integers2;

24
25

// input and print integers1 and integers2

26

cout << "\nEnter 17 integers:" << endl;

27

cin >> integers1 >> integers2;

Use overloaded >> operator to input

2008 Pearson Education,


Inc. All rights reserved.

28
29

cout << "\nAfter input, the Arrays contain:\


contain:\n"

30

<< "integers1:\
"integers1:\n" << integers1

31

<< "integers2:\
"integers2:\n" << integers2;
integers2;

Outline

46

Use overloaded << operator to output

32
33

// use overloaded inequality (!=) operator

34

cout << "\nEvaluating: integers1 != integers2" << endl;

fig11_08.cpp

35
36
37

Use overloaded != operator (2


to of
test5)for inequality

if ( integers1 != integers2 )

cout << "integers1 and integers2 are not equal" << endl;

38
39

// create Array integers3 using integers1 as an

40

// initializer; print size and contents

41

Array integers3( integers1 ); // invokes copy constructor

42
43

cout << "\nSize of Array integers3 is "

Use copy constructor

44

<< integers3.getSize()

45

<< "\nArray after initialization:\


initialization:\n" << integers3;

46
47

// use overloaded assignment


assignment (=) operator

48

cout << "\nAssigning integers2 to integers1:" << endl;

49

integers1 = integers2; // note target Array is smaller

50
51
52

cout << "integers1:\


"integers1:\n" << integers1
<< "integers2:\
"integers2:\n" << integers2;
integers2;

53

Use overloaded = operator to assign

54

// use overloaded equality (==) operator

55

cout << "\nEvaluating: integers1 == integers2" << endl;

2008 Pearson Education,


Inc. All rights reserved.

56
57
58

if ( integers1 == integers2 )

Use overloaded == operator Outline


to test for equality

47

cout << "integers1 and integers2 are equal" << endl;

59
60

// use overloaded subscript operator to create rvalue

61

cout << "\nintegers1[5] is " << integers1[ 5 ];


];

fig11_08.cpp

63

// use overloaded
overloaded subscript operator to create lvalue

(3 of 5)

64

cout << "\n\nAssigning 1000 to integers1[5]" << endl;

65

integers1[ 5 ] = 1000;
1000;

66

cout << "integers1:\


"integers1:\n" << integers1;
integers1;

62

Use overloaded [] operator to access


individual integers, with range-checking

67
68

// attempt to use outout-ofof-range subscript

69

cout << "\nAttempt


nAttempt to assign 1000 to integers1[15]" << endl;

70

integers1[ 15 ] = 1000; // ERROR: out of range

71

return 0;

72 } // end main

2008 Pearson Education,


Inc. All rights reserved.

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

Outline
0
0

48

fig11_08.cpp
0
0

0
0

3
7

10
14

11
15

(4 of 5)

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
5
6
integers2:
8
9
12
13
16
17

Evaluating: integers1 != integers2


integers1 and integers2 are not equal

2008 Pearson Education,


Inc. All rights reserved.

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

Outline
3
7

49

fig11_08.cpp
10
14

11
15

10
14

11
15

(5 of 5)

Evaluating: integers1 == integers2


integers1 and integers2 are equal
equal
integers1[5] is 13
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

2008 Pearson Education,


Inc. All rights reserved.

50

Software Engineering Observation 11.4


The argument to a copy constructor should be
a const reference to allow a const object to
be copied.

2008 Pearson Education, Inc. All rights reserved.

51

Common Programming Error 11.6


Note that a copy constructor must receive its
argument by reference, not by value. Otherwise,
the copy constructor call results in infinite
recursion (a fatal logic error) because receiving
an object by value requires the copy constructor
to make a copy of the argument object. Recall
that any time a copy of an object is required, the
classs copy constructor is called. If the copy
constructor received its argument by value, the
copy constructor would call itself recursively to
make a copy of its argument!

2008 Pearson Education, Inc. All rights reserved.

52

Common Programming Error 11.7


If the copy constructor simply copied the
pointer in the source object to the target
objects pointer, then both objects would point
to the same dynamically allocated memory. The
first destructor to execute would then delete the
dynamically allocated memory, and the other
objects ptr would be undefined, a situation
called a dangling pointerthis would likely
result in a serious run-time error (such as early
program termination) when the pointer was
used.
2008 Pearson Education, Inc. All rights reserved.

53

Software Engineering Observation 11.5


A copy constructor, a destructor and an
overloaded assignment operator are usually
provided as a group for any class that uses
dynamically allocated memory.

2008 Pearson Education, Inc. All rights reserved.

54

Common Programming Error 11.8


Not providing an overloaded assignment operator
and a copy constructor for a class when objects of
that class contain pointers to dynamically
allocated memory is a logic error.

2008 Pearson Education, Inc. All rights reserved.

55

Software Engineering Observation 11.6


It is possible to prevent one object of a class
from being assigned to another. This is done
by declaring the assignment operator as a
private member of the class.

2008 Pearson Education, Inc. All rights reserved.

56

Software Engineering Observation 11.7


It is possible to prevent class objects from
being copied; to do this, simply make both the
overloaded assignment operator and the copy
constructor of that class private.
private

2008 Pearson Education, Inc. All rights reserved.

57

11.9 Converting between Types


Casting
Traditionally, cast integers to floats, etc.
May need to convert between user-defined types

Cast operator (conversion operator)


Convert from
One class to another
A Class to a built-in type (int
int,
int char,
char etc.)

Must be non-static
static member function
Do not specify return type
Implicitly returns type to which you are converting

2008 Pearson Education, Inc. All rights reserved.

58

11.9 Converting between Types (Cont.)


Cast operator (conversion operator) (Cont.)
Example
Prototype
A::operator
A::operator char *() const;
const;
Casts class A to a temporary char *
static_cast<
static_cast< char * >( s ) calls
s.operator char *()
Also
A::operator
int()
() const;
A::operator int
const;
A::
A::operator
operator OtherClass()
OtherClass() const;
const;

2008 Pearson Education, Inc. All rights reserved.

59

11.9 Converting between Types (Cont.)


Casting can prevent need for overloading
Suppose class String can be cast to char *
cout << s; // s is a String
Compiler implicitly converts s to char * for output
Do not have to overload <<

2008 Pearson Education, Inc. All rights reserved.

60

11.10 Case Study: String Class


Build class String
String creation, manipulation
Similar to class string in standard library (Chapter 18)

Conversion constructor
Any single-argument constructor
Turns objects of other types into class objects
Example
String s1( "happy" );
Creates a String from a char *

Overloading function call operator


Powerful (functions can take arbitrarily long and complex
parameter lists)

2008 Pearson Education, Inc. All rights reserved.

// Fig. 11.9: String.h

// String class definition.

#ifndef STRING_H

#define STRING_H

Outline

61

5
6

#include <iostream>

using std::ostream;

using std::istream;

9
10 class String

String.h

(1 of 3)
Conversion constructor to make
a String from a char *

11 {
12

friend ostream &operator<<( ostream &, const


const String & );

13

friend istream &operator>>( istream &, String & );

14 public:
public:
15

String( const char * = "" ); // conversion/default constructor

16

String( const String & ); // copy constructor

17

~String(); // destructor
destructor

18

s1 += s2 will be interpreted
as s1.operator+=(s2)

19

const String &operator


&operator=(
operator=( const String & ); // assignment operator

20

const String &operator


&operator+=(
operator+=( const String & ); // concatenation operator

21
22

bool operator!()
operator!() const;
const; // is String
String empty?

23

bool

24

bool

25

Can also concatenate a String and a


operator==(
operator==( const String & ) const;
const; // test s1 == s2 char * because the compiler will cast
operator<(
operator<( const String & ) const;
const; // test s1 < s2
the char * argument to a String

2008 Pearson Education,


Inc. All rights reserved.

26

// test s1 != s2

27

bool operator!=(
operator!=( const String &right ) const

28

{
return !( *this
*this == right );

29
30

Outline

62

} // end function operator!=

31

String.h

32

// test s1 > s2

33

bool operator>(
operator>( const String &right ) const

34

35

return right < *this


*this;
this;

36

} // end function operator>


operator>

37
38

// test s1 <= s2

39

bool operator<=(
operator<=( const String &right ) const

40

41

return !( right < *this


*this );

42

} // end function
function operator <=

(2 of 3)
Overload equality and
relational operators

43
44

// test s1 >= s2

45

bool operator>=(
operator>=( const String &right ) const

46

47
48

return !( *this
*this < right );
} // end function operator>=

2008 Pearson Education,


Inc. All rights reserved.

49
50

char &operator
&operator[](
operator[]( int ); // subscript operator (modifiable lvalue)

51

char operator[](
operator[]( int ) const;
const; // subscript operator (rvalue)

52

String operator()(
operator()( int,
int, int = 0 ) const; // return a substring

53

int getLength() const


const;
nst; // return string length

54 private:
private:
55

int length; // string length (not counting null terminator)

56

char *sPtr; // pointer to start of pointerpointer-based string

57
58

Outline

63

Two overloaded subscript


operators, for const and
non-const objects
String.h

Overload the function call


(3 of 3)
operator () to return a substring

void setString( const char * ); // utility function

59 }; // end class String


60
61 #endif

2008 Pearson Education,


Inc. All rights reserved.

// Fig. 11.10: String.cpp

// MemberMember-function definitions for class String.

#include <iostream>

using std::cerr;

using std::cout;

using std::endl;

String.cpp

#include <iomanip>

(1 of 7)

using std::setw;

Outline

64

10
11 #include <cstring> // strcpy
strcpy and strcat prototypes
12 using std::strcmp;
13 using std::strcpy;
14 using std::strcat;
15
16 #include <cstdlib> // exit prototype
17 using std::exit;
18
19 #include "String.h" // String class definition
20
21 // conversion (and default)
default) constructor converts char * to String
22 String::String( const char *s )
23

: length( ( s != 0 ) ? strlen( s ) : 0 )

24 {
25

cout << "Conversion (and default) constructor: " << s << endl;

26

setString( s ); // call utility function


function

27 } // end String conversion constructor


28

2008 Pearson Education,


Inc. All rights reserved.

29 // copy constructor
30 String::String( const String &copy )
31

: length( copy.length )

Outline

65

32 {
33

cout << "Copy constructor: " << copy.sPtr << endl;

34

setString( copy.sPtr ); // call utility function

String.cpp

35 } // end String copy constructor


constructor
36

(2 of 7)

37 // Destructor
38 String::~String()
39 {
40

cout << "Destructor: " << sPtr << endl;

41

delete [] sPtr; // release pointerpointer-based string memory

42 } // end ~String destructor


43
44 // overloaded = operator; avoids self assignment
assignment
45 const String &String::operator=( const String &right )
46 {
47

cout << "operator= called" << endl;

48
49

if ( &right != this ) // avoid self assignment

50

51

delete [] sPtr; // prevents memory leak

52

length = right.length; // new String length

53

setString( right.sPtr ); // call utility function

54

} // end if

55

else

56

cout << "Attempted assignment of a String to itself" << endl;

57

2008 Pearson Education,


Inc. All rights reserved.

return *this
*this;
this; // enables cascaded assignments
59 } // end function operator=
60
58

61 // concatenate right operand to this object and store in this object


62 const String &String::operator+=( const String &right )
63 {
64
65

size_t newLength
newLength = length + right.length; // new length
char *tempPtr = new char[
char[ newLength + 1 ]; // create memory

66
67
68

Outline

66

String.cpp

(3 of 7)

strcpy( tempPtr, sPtr ); // copy sPtr


strcpy( tempPtr + length, right.sPtr ); // copy right.sPtr

69
70
71
72
73

delete [] sPtr;
sPtr; // reclaim old space
sPtr = tempPtr; // assign new array to sPtr
length = newLength; // assign new length to length
*this
this;
return *
this
; // enables cascaded calls

74 } // end function operator+=


75
76 // is this String empty?
77 bool String::operator!() const
78 {
return length == 0;
80 } // end function operator!
81
79

82 // Is this String equal to right String?


83 bool String::operator==( const String &right ) const
84 {
return strcmp( sPtr, right.sPtr
right.sPtr ) == 0;
86 } // end function operator==
87
85

2008 Pearson Education,


Inc. All rights reserved.

88 // Is this String less than right String?


89 bool String::operator<( const String &right ) const
90 {
return strcmp( sPtr, right.sPtr ) < 0;
92 } // end function operator<
93

Outline

67

91

94 // return reference to character in String as a modifiable


modifiable lvalue
95 char &String::operator
&String::operator[](
operator[]( int subscript )
96 {
97
98
99
100
101
102
103

String.cpp

(4 of 7)

// test for subscript out of range


if ( subscript < 0 || subscript >= length )
{
cerr << "Error: Subscript " << subscript
<< " out of range"
range" << endl;
exit( 1 ); // terminate program
} // end if

104
return sPtr[ subscript ]; // nonnon-const return; modifiable lvalue
106 } // end function operator[]
107
105

108 // return reference to character in String as rvalue


rvalue
109 char String::operator[]( int subscript ) const
110 {
111
112
113
114
115
116
117

// test for subscript out of range


if ( subscript < 0 || subscript >= length )
{
cerr << "Error: Subscript " << subscript
<< " out of range" << endl;
exit( 1 ); // terminate program
} // end if

2008 Pearson Education,


Inc. All rights reserved.

118
119

return sPtr[ subscript ]; // returns copy of this element

120 } // end function operator[]

Outline

68

121
122 // return a substring beginning at index and of length subLength
123 String String::operator()( int index, int subLength ) const

String.cpp

124 {
125

// if index is out of range or substring length < 0,

126

// return an empty String object

127

if ( index < 0 || index >= length || subLength < 0 )

128

(5 of 7)

return "";
""; // converted to a String object automatically

129
130

// determine length of substring

131

int len;

132
133
134
135
136

if ( ( subLength == 0 ) || ( index + subLength > length ) )


len = length - index;
else
len = subLength;

137
138

// allocate temporary array for substring


substring and

139

// terminating null character

140

char *tempPtr = new char[


char[ len + 1 ];

141
142

// copy substring into char array and terminate string

143

strncpy( tempPtr, &sPtr[ index ], len );

144

tempPtr[ len ] = '\0';


0';

2008 Pearson Education,


Inc. All rights reserved.

145
146
147
148

// create temporary String object containing the substring


String tempString( tempPtr );
delete [] tempPtr; // delete temporary array

return tempString; // return copy of the temporary String


150 } // end functio
function
n operator()
151
152 // return string length

Outline

69

149

153 int String::getLength() const


154 {

String.cpp

(6 of 7)

return length;
156 } // end function getLength
157
155

158 // utility function called by constructors and operator=


159 void String::setString( const char *string2 )
160 {
161

sPtr = new char[


char[ length + 1 ]; // allocate memory

162
163
164

if ( string2 != 0 ) // if string2 is not null pointer, copy contents


strcpy( sPtr, string2 ); // copy literal to object

else // if string2 is a null pointer, make this an empty string


166
sPtr[ 0 ] = '\0';
0'; // empty string
167 } // end function setString
168
165

169 // overloaded output operator


170 ostream &operator<<( ostream &output, const String &s )
171 {
output << s.sPtr;
173
return output; // enables cascading
174 } // end function operator<<
172

2008 Pearson Education,


Inc. All rights reserved.

175
176 // overloaded input operator
177 istream &operator>>( istream &input, String &s )

Outline

70

178 {
179

char temp[ 100 ]; // buffer to store input

180

input >> setw( 100 ) >> temp;

181

s = temp; // use String class assignment operator

182

return input; // enables cascading

String.cpp

(7 of 7)

183 } // end function operator>>

2008 Pearson Education,


Inc. All rights reserved.

1
2
3

// Fig. 11.11: fig11_11.cpp


// String class test program.
#include <iostream>

4
5
6
7
8
9
10

using std::cout;
using std::endl;
using std::boolalpha;

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include "String.h"

Outline

71

fig11_11.cpp

(1 of 5)

int main()
{
String s1( "happy" );
String
Stri
ng s2( " birthday" );
String s3;
// test overloaded equality and relational operators
Use overloaded stream insertion
cout << "s1 is \"" << s1 << "\"; s2 is \"" << s2
operator for Strings
<< "\"; s3 is \"" << s3 << '\"'
<< boolalpha << "\n\nThe results of comparing s2 and s1:"
<< "\ns2 == s1 yields " << ( s2 == s1 )
"\
<< "
\ns2 != s1 yields " << ( s2 != s1 )
"\
<< "
\ns2 > s1 yields " << ( s2 > s1 )
Use overloaded equality and
<< "\ns2 < s1 yields " << ( s2 < s1 )
relational operators for Strings
<< "\ns2 >= s1 yields " << ( s2 >= s1 )
<< "\
"\ns2 <= s1 yields " << ( s2 <= s1 );

// test overloaded String empty (!) operator


cout << "\n\nTesting !s3:" << endl;

2008 Pearson Education,


Inc. All rights reserved.

31
32

if ( !s3 )
{

33
34

cout << "s3 is empty; assigning s1 to s3;" << endl;


s3 = s1; // test overloaded assignment
cout << "s3 is \"" << s3 << "\
"\"";
Use
} // end if

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

Use overloaded negation


Outline
operator for Strings

72

overloaded assignment
operator for Strings fig11_11.cpp

// test overloaded String concatenation


concatenation operator
cout << "\n\ns1 += s2 yields s1 = ";
";
s1 += s2; // test overloaded concatenation
cout << s1;
// test conversion constructor
cout << "\n\ns1 += \" to you\
you\" yields" << endl;
you";
s1 += " to you"
; // test conversion constructor
"\
n";
cout << "s1 = " << s1 << "
\n\n"
;

(2 of 5)
Use overloaded addition assignment
operator for Strings

// test overloaded function call operator () for substring


cout << "The substring of s1 starting at\
at\n"
<< "location 0 for 14 characters, s1(0, 14), is:\
is:\n"

char * string is converted to a


String before using the overloaded
addition assignment operator

<< s1( 0, 14 ) << "\


"\n\n";
n";
// test substring "to"to-endend-ofof-String" option
cout << "The substring of s1 starting at\
at\n"
<< "location 15, s1(15), is: "

Use overloaded function call


operator for Strings

<< s1( 15 ) << "\n\n";


n";
// test copy constructor
constructor
String *s4Ptr = new String( s1 );
cout << "\n*s4Ptr = " << *s4Ptr << "\n\n";
n";

2008 Pearson Education,


Inc. All rights reserved.

61
62

// test assignment (=) operator with selfself-assignment

63

cout << "assigning *s4Ptr to *s4Ptr" << endl;

64

*s4Ptr = *s4Ptr; // test overloaded assignment

65

cout << "*s4Ptr = " << *s4Ptr << endl;

Outline

73

fig11_11.cpp

66
67

// test destructor

68

delete s4Ptr;

(3 of 5)

69
70

// test using subscript operator to create a modifiable lvalue

71

s1[ 0 ] = 'H';
'H';

72

s1[ 6 ] = 'B';
'B';

73

cout << "\ns1 after s1[0] = 'H' and s1[6] = 'B' is: "

74

Use overloaded subscript


operator for Strings

<< s1 << "\n\n";


n";

75
76

// test subscript out of range

77

cout << "Attempt to assign 'd' to s1[30] yields:" <<


<< endl;

78

s1[ 30 ] = 'd'; // ERROR: subscript out of range

79

return 0;

80 } // end main

Attempt to access a subscript


outside of the valid range

2008 Pearson Education,


Inc. All rights reserved.

Conversion (and default) constructor: happy


Conversion (and default) constructor: birthday
Conversion (and default) constructor:
s1 is "happy"; s2 is " birthday"; s3 is ""

Outline

The results of comparing s2 and s1:


s2 == s1 yields false
s2 != s1 yields true
s2 > s1 yields false
s2 < s1 yields true
s2 >= s1 yields false
s2 <= s1 yields true

74

fig11_11.cpp

(4 of 5)

Testing !s3:
s3 is empty; assigning s1 to s3;
operator= called
s3 is "happy"
s1 += s2 yields s1 = happy birthday
s1 += " to you" yields
Conversion (and default) constructor:
constructor:
Destructor: to you
s1 = happy birthday to you

to you

The constructor and destructor are


called for the temporary String

Conversion (and default) constructor: happy birthday


Copy constructor: happy birthday
Destructor: happy birthday
The substring of s1 starting at
location 0 for 14 characters, s1(0, 14), is:
happy birthday
(continued at top of next slide... )

2008 Pearson Education,


Inc. All rights reserved.

(... continued from bottom of previous slide)


Destructor: happy birthday
Conversion (and default) constructor: to you
Copy constructor: to you
Destructor: to you
The substring of s1 starting at
location 15, s1(15),
s1(15), is: to you
Destructor: to you
Copy constructor: happy birthday to you

Outline

75

fig11_11.cpp

(5 of 5)

*s4Ptr = happy birthday to you


assigning
operator=
Attempted

*s4Ptr to *s4Ptr
called
assignment of a String to itself

*s4Ptr = happy birthday to you


Destructor: happy birthday to you
s1 after s1[0] = 'H' and s1[6] = 'B' is: Happy Birthday to you
Attempt to assign 'd' to s1[30] yields:
Error: Subscript 30 out of range

2008 Pearson Education,


Inc. All rights reserved.

76

Software Engineering Observation 11.8


When a conversion constructor is used to perform
an implicit conversion, C++ can apply only one
implicit constructor call (i.e., a single user-defined
conversion) to try to match the needs of another
overloaded operator. The compiler will not match
an overloaded operators needs by performing a
series of implicit, user-defined conversions.

2008 Pearson Education, Inc. All rights reserved.

77

Performance Tip 11.2


Overloading the += concatenation operator with
an additional version that takes a single argument
of type const char * executes more efficiently
than having only a version that takes a String
argument. Without the const char * version of
the += operator, a const char * argument
would first be converted to a String object with
class Strings
String conversion constructor, then the
+= operator that receives a String argument
would be called to perform the concatenation.

2008 Pearson Education, Inc. All rights reserved.

78

Software Engineering Observation 11.9


Using implicit conversions with overloaded
operators, rather than overloading operators
for many different operand types, often
requires less code, which makes a class easier
to modify, maintain and debug.

2008 Pearson Education, Inc. All rights reserved.

79

Software Engineering Observation 11.10


By implementing member functions using
previously defined member functions, the
programmer reuses code to reduce the amount
of code that must be written and maintained.

2008 Pearson Education, Inc. All rights reserved.

80

Error-Prevention Tip 11.2


Returning a nonnon-const char reference from an
overloaded subscript operator in a String class
is dangerous. For example, the client could use
this reference to insert a null ('\
('\0') anywhere
in the string.

2008 Pearson Education, Inc. All rights reserved.

81

11.11 Overloading ++ and - Increment/decrement operators can be


overloaded
Suppose we want to add 1 to a Date object, d1
Prototype (member function)
Date &operator
&operator++();
operator++();
++d1 becomes d1.operator
d1.operator++()
operator++()

Prototype (global function)


Date &operator
&operator++(
operator++( Date & );
++d1 becomes operator++(
operator++( d1 )

2008 Pearson Education, Inc. All rights reserved.

82

11.11 Overloading ++ and -- (Cont.)


To distinguish prefix and postfix increment
Postfix increment has a dummy parameter
An int with value 0

Prototype (member function)


Date operator++(
operator++( int );
d1++ becomes d1.operator
operator++( 0 )
d1.operator++(

Prototype (global function)


Date operator++(
operator++( Date &, int );
d1++ becomes operator++(
operator++( d1, 0 )

2008 Pearson Education, Inc. All rights reserved.

83

11.11 Overloading ++ and -- (Cont.)


Return values
Prefix increment
Returns by reference (Date
Date &)
&
lvalue (can be assigned)

Postfix increment
Returns by value
Returns temporary object with old value
rvalue (cannot be on left side of assignment)

All this applies to decrement operators as well

2008 Pearson Education, Inc. All rights reserved.

84

Performance Tip 11.3


The extra object that is created by the postfix
increment (or decrement) operator can result in a
significant performance problemespecially when
the operator is used in a loop. For this reason, you
should use the postfix increment (or decrement)
operator only when the logic of the program
requires postincrementing (or postdecrementing).

2008 Pearson Education, Inc. All rights reserved.

85

11.12 Case Study: A Date Class


Example Date class
Overloaded increment operator
Change day, month and year

Overloaded += operator
Function to test for leap years
Function to determine if day is last of month

2008 Pearson Education, Inc. All rights reserved.

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

// Fig.
// Date
#ifndef
#define

11.12: Date.h
class definition.
DATE_H
DATE_H

#include <iostream>
using std::ostream;
class Date
{
friend ostream &operator<<( ostream &, const Date & );
public:
public
:
Date( int m = 1, int d = 1, int y = 1900 ); // default constructor
int,
int,
void setDate( int
, int
, int ); // set month, day, year
Date &operator++(); // prefix increment operator
Date operator++( int ); // postfix increment operator
const Date &operator+=( int ); // add days, modify object
const;
bool leapYear( int ) const
; // is date in a leap year?
const;
bool endOfMonth( int ) const
; // is date at the end of month?
private:
private
:
int month;
int day;

Outline

86

Date.h

(1 of 1)

Note the difference between


prefix and postfix increment

23
int year;
24
25
static const int days[]; // array of days per month
26
void helpIncrement(); // utility function for incrementing date
27 }; // end class Date
28
29 #endif

2008 Pearson Education,


Inc. All rights reserved.

// Fig. 11.13: Date.cpp

// Date class membermember-function definitions.

#include <iostream>

#include "Date.h"

Outline

87

5
6

// initialize static member at file scope; one classwide copy

const int Date::days[] =

{ 0, 31,
31, 28,
28, 31,
31, 30,
30, 31,
31, 30,
30, 31,
31, 31,
31, 30,
30, 31,
31, 30,
30, 31 };

Date.cpp

(1 of 4)

9
10 // Date constructor
11 Date::Date( int m, int d, int y )
12 {
13

setDate( m, d, y );

14 } // end Date constructor


15
16 // set month, day and year
17 void Date::setDate( int mm, int dd, int yy )
18 {
19

month = ( mm >= 1 && mm <= 12 ) ? mm : 1;

20

year = ( yy >= 1900 && yy <= 2100 ) ? yy : 1900;


1900;

21
22

// test for a leap year

23

if ( month == 2 && leapYear( year ) )

24
25
26

day = ( dd >= 1 && dd <= 29 ) ? dd : 1;


else
else
day = ( dd >= 1 && dd <= days[ month ] ) ? dd : 1;

27 } // end function setDate

2008 Pearson Education,


Inc. All rights reserved.

28
29 // overloaded prefix increment operator
30 Date &Date::operator++()

Outline

88

31 {
32

helpIncrement(); // increment date

33

return *this
*this;
this; // reference return to create an lvalue

Date.cpp

34 } // end function operator++


35

(2 of 4)

36 // overloaded postfix increment operator; note that the


37 // dummy integer parameter does not have a parameter name
38 Date Date::operator++( int )
39 {
40

Date temp = *this


*this;
this; // hold current state of object

41

helpIncrement();

Postfix increment updates object


and returns a copy of the original

42
43

// return unincremented, saved, temporary object

44

return temp; // value return; not a reference return

45 } // end function operator++


46
47 // add specified number of days to date
48 const Date &Date::operator+=( int additionalDays )
49 {
50
51

Do not return a reference to


temp, because it is a local
variable that will be destroyed

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


helpIncrement();

52
53

return *this
*this;
this; // enables cascading

54 } // end function operator+=


operator+=
55

2008 Pearson Education,


Inc. All rights reserved.

56 // if the year is a leap year, return true; otherwise, return false


57 bool Date::leapYear( int testYear ) const
58 {
59

if ( testYear % 400 == 0 ||

60

( testYear % 100 != 0 && testYear % 4 == 0 ) )

61

return true;
true; // a leap year
year

62
63

Outline

89

Date.cpp

else
return false;
false; // not a leap year

(3 of 4)

64 } // end function leapYear


65
66 // determine whether the day is the last day of the month
67 bool Date::endOfMonth( int testDay ) const
68 {
69
70
71
72

if ( month == 2 && leapYear( year


year ) )
return testDay == 29;
29; // last day of Feb. in leap year
else
return testDay == days[ month ];

73 } // end function endOfMonth


74

2008 Pearson Education,


Inc. All rights reserved.

75 // function to help increment the date


76 void Date::helpIncrement()
77 {
78

// day is not end of month

79

if ( !endOfMonth( day ) )

80
81

day++; // increment day

Date.cpp

else

82

if ( month < 12 ) // day is end of month and month < 12

83

84

month++; // increment month

85

day = 1; // first day of new month

86

} // end if

87

else // last day of year

88

89

year++; // increment year

90

month = 1; // first
first month of new year

91

day = 1; // first day of new month

92

Outline

90

(4 of 4)

} // end else

93 } // end function helpIncrement


94
95 // overloaded output operator
96 ostream &operator<<( ostream &output, const Date &d )
97 {
98

static char *monthName[ 13 ] = { "",


"", "January",
"January", "February",
"February",

99

"March",
"March", "April",
"April", "May",
"May", "June",
"June", "July",
"July", "August",
"August",

100

"September",
"September", "October",
"October", "November",
"November", "December" };

101

output << monthName[ d.month ] << ' ' << d.day << ", " << d.year;

102

return output; // enables cascading

103 } // end function operator<<

2008 Pearson Education,


Inc. All rights reserved.

// Fig. 11.14: fig11_14.cpp

// Date class test program.

#include <iostream>

using std::cout;

using std::endl;

Outline

6
7

fig11_14.cpp
#include "Date.h" // Date class definition

8
9

91

(1 of 2)
int main()

10 {
11

Date d1; // defaults to January 1, 1900

12

Date d2( 12,


12, 27,
27, 1992 ); // December 27, 1992

13

Date d3( 0, 99,


99, 8045 ); // invalid date

14
15

cout << "d1 is " << d1 << "\nd2 is " << d2 << "\nd3 is " << d3;

16

cout << "\n\nd2 += 7 is " << ( d2 += 7 );

17
18

d3.setDate( 2, 28,
28, 1992 );

19

cout << "\n\n

20

cout << "\n++d3 is " << ++d3 << " (leap year allows 29th)";
29th)";

d3 is " << d3;

21
22

Date d4( 7, 13,


13, 2002 );

23
24
25

cout << "\n\nTesting the prefix increment operator:\


operator:\n"
<< "

d4 is " << d4 << endl;

26

cout << "++d4 is " << ++d4 << endl;

27

cout << "

d4 is " << d4;

28

Demonstrate prefix increment


2008 Pearson Education,
Inc. All rights reserved.

cout << "\n\nTesting the postfix increment operator:\


operator:\n"

29

<< "

30
31

cout << "d4++ is " << d4++ << endl;

32

cout << "

33

return 0;

is
is
is

92

d4 is " << d4 << endl;

34 } // end main
d1
d2
d3

Outline

d4 is " << d4 << endl;

January 1, 1900
December 27, 1992
January 1, 1900

Demonstrate postfix increment


fig11_14.cpp

(2 of 2)

d2 += 7 is January 3, 1993
d3 is February 28, 1992
++d3 is February 29, 1992 (leap year allows 29th)
Testing
d4 is
++d4 is
d4 is

the prefix increment operator:


July 13, 2002
July 14, 2002
July 14, 2002

Testing
d4 is
d4++ is
d4 is

the postfix increment operator:


July 14, 2002
July 14, 2002
July 15, 2002

2008 Pearson Education,


Inc. All rights reserved.

93

11.13 Standard Library Class string


Class built into C++
Available for anyone to use
Class string
Similar to our String class

Redo our String example using string

2008 Pearson Education, Inc. All rights reserved.

11.13 Standard Library Class string


(Cont.)

94

Class string
Header <string>,
<string> namespace std
Can initialize string s1( "hi" );
Overloaded << (as in cout << s1)
s1
Overloaded relational operators
==,
== !=,
!= >=,
>= >, <=,
<= <

Assignment operator =
Concatenation (overloaded +=)
+=

2008 Pearson Education, Inc. All rights reserved.

11.13 Standard Library Class string


(Cont.)

95

Class string (Cont.)


Substring member function substr
s1.substr( 0, 14 );
Starts at location 0, gets 14 characters
s1.substr( 15 );
Substring beginning at location 15, to the end

Overloaded []
Access one character
No range checking (if subscript invalid)

Member function at
Accesses one character
Example
s1.at( 10 );
Has bounds checking, throws an exception if subscript is invalid
Will end program (learn more in Chapter 16)
2008 Pearson Education, Inc. All rights reserved.

// Fig. 11.15: fig11_15.cpp

// Standard Library string class test program.

#include <iostream>

using std::cout;

using std::endl;

Outline

96

fig11_15.cpp

#include <string>

using std::string;

of 4)
Passing strings to the string (1
constructor

9
10 int main()
11 {
12

string s1( "happy" );

13

string s2( " birthday" );

14

string s3;

Create empty string

15
16

// test overloaded equality and relational operators

17

cout << "s1 is \"" << s1 << "\"; s2 is \"" << s2

18

<< "\"; s3 is \"" << s3 << '\"'

19

<< "\n\nThe results of comparing s2 and s1:"

20

<< "\ns2 == s1 yields " << ( s2 == s1 ? "true" : "false" )

21

<< "\ns2 != s1 yields " << ( s2 != s1 ? "true" : "false" )

22

<< "\ns2 >

s1 yields " << ( s2 > s1 ? "true" : "false" )

23

<< "\ns2 <

s1 yields " << ( s2 < s1 ? "true" : "false" )

24

<< "\ns2 >= s1 yields " << ( s2 >= s1 ? "true" : "false" )

25

<< "\ns2 <= s1 yields " << ( s2 <= s1 ? "true" : "false" );

26
27

// test string membermember-function empty

28

cout << "\n\nTesting s3.empty():" << endl;

2008 Pearson Education,


Inc. All rights reserved.

29
30

if ( s3.empty() )

31

32

cout << "s3 is empty; assigning s1 to s3;" << endl;

33

s3 = s1;
s1; // assign s1 to s3

34

cout << "s3 is \"" << s3 << "\"";


"";

35

Outline
Member function empty
tests
if the string is empty

97

fig11_15.cpp

} // end if

(2 of 4)

36
37

// test overloaded string concatenation


concatenation operator

38

cout << "\n\ns1 += s2 yields s1 = ";


";

39

s1 += s2; // test overloaded concatenation

40

cout << s1;

41
42

// test overloaded string concatenation operator with CC-style string

43

cout << "\n\ns1 += \" to you\


you\" yields"
yields" << endl;

44

s1 += " to you";
you";

45

cout << "s1 = " << s1 << "\n\n";


n";

46
47

// test string member function substr

48

cout << "The substring of s1 starting at location 0 for\


for\n"

49

<< "14 characters, s1.substr(0, 14), is:\


is:\n"

50

<< s1.substr( 0, 14 ) << "\n\n";


n";

51
52

// test substr "to"to-endend-ofof-string" option

53

cout << "The substring of s1 starting at\


at\n"

54

<< "location 15, s1.substr(15), is:\


is:\n"

55

<< s1.substr( 15 ) << endl;

Member function substr obtains


a substring from the string

2008 Pearson Education,


Inc. All rights reserved.

56
57

// test copy constructor

58

string *s4Ptr = new string( s1 );

59

cout << "\n*s4Ptr = " << *s4Ptr << "\


"\n\n";
n";

Outline

98

60
61

// test assignment (=) operator with selfself-assignment

62

cout << "assigning *s4Ptr to *s4Ptr" << endl;

63

*s4Ptr = *s4Ptr;

64

cout << "*s4Ptr = " << *s4Ptr << endl;

fig11_15.cpp

(3 of 4)

65
66

// test destructor

67

delete s4Ptr;

68
69

// test using subscript operator to create lvalue

70

s1[ 0 ] = 'H';
'H';

71

s1[ 6 ] = 'B';
'B';

72

cout << "\ns1 after s1[0] = 'H' and s1[6] = 'B' is: "

73

Accessing specific character in string

<< s1 << "\n\n";


n";

74
75

// test subscript out of range with string member function "at"

76

cout << "Attempt to assign 'd' to s1.at( 30 ) yields:" << endl;

77

s1.at( 30 ) = 'd'; // ERROR:


ERROR: subscript out of range

78

return 0;

79 } // end main

Member function at
provides range checking

2008 Pearson Education,


Inc. All rights reserved.

s1 is "happy"; s2 is " birthday"; s3 is ""


The results of comparing s2 and s1:
s2 == s1 yields false
s2 != s1 yields true
s2 > s1 yields false
s2 < s1 yields true
s2 >= s1 yields false
s2 <= s1 yields true
Testing s3.empty():
s3 is empty; assigning s1 to s3;
s3 is "happy"

Outline

99

fig11_15.cpp

(4 of 4)

s1 += s2 yields s1 = happy birthday


s1 += " to you" yields
s1 = happy birthday to you
The substring of s1 starting at location 0 for
14 characters, s1.substr(0, 14), is:
happy birthday
The substring of s1 starting at
location 15, s1.substr(15), is:
to you
*s4Ptr = happy birthday to you
assigning *s4Ptr to *s4Ptr
*s4Ptr = happy birthday to you
s1 after s1[0] = 'H' and s1[6] = 'B' is: Happy Birthday to you
Attempt to assign 'd' to s1.at( 30 ) yields:
abnormal program termination
termination

2008 Pearson Education,


Inc. All rights reserved.

100

11.14 explicit Constructors


Implicit conversions
Performed by compiler using single-argument constructors
Sometimes, implicit conversions are undesirable or errorprone
Keyword explicit
Suppresses implicit conversions via conversion
constructors

2008 Pearson Education, Inc. All rights reserved.

101

Common Programming Error 11.9


Unfortunately, the compiler might use implicit
conversions in cases that you do not expect,
resulting in ambiguous expressions that generate
compilation errors or resulting in execution-time
logic errors.

2008 Pearson Education, Inc. All rights reserved.

// Fig. 11.16: Fig11_16.cpp

// Driver for simple class Array.

#include <iostream>

using std::cout;

using std::endl;

Outline

fig11_16.cpp

6
7

#include "Array.h"

(1 of 2)

8
9

102

void outputArray( const Array & ); // prototype

10
11 int main()
12 {
13

Array integers1( 7 ); // 77-element array

14

outputArray( integers1 ); // output Array integers1

15

outputArray( 3 ); // convert 3 to an Array and output Arrays contents

16

return 0;

17 }

// end main

Would logically want


this to generate an error

2008 Pearson Education,


Inc. All rights reserved.

18
19 // print Array contents
20 void outputArray( const Array &arrayToOutput )

Outline

103

21 {
22
23

cout << "The Array received has " << arrayToOutput.getSize()


<< " elements. The contents are:\
are:\n" << arrayToOutput << endl;

fig11_16.cpp

24 } // end outputArray
outputArray
The Array received has 7 elements. The contents are:
0
0
0
0
0
0
0

(2 of 2)

The Array received has 3 elements. The contents are:


0
0
0

2008 Pearson Education,


Inc. All rights reserved.

// Fig. 11.17: Array.h

// Array class for storing arrays of integers.

#ifndef ARRAY_H

#define ARRAY_H

Outline

104

5
6

#include <iostream>

using std::ostream;

using std::istream;

Array.h

(1 of 1)

9
10 class Array
11 {
12

friend ostream &operator<<(


&operator<<( ostream &, const Array & );

13

friend istream &operator>>( istream &, Array & );

14 public:
public:
15

explicit Array( int = 10 ); // default constructor

16

Array( const Array & ); // copy constructor

17

~Array(); // destructor

18

int getSize() const;


const; // return size

Use explicit keyword to


avoid implicit conversions
when inappropriate

19
20

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

21

bool operator==( const Array & ) const;


const; // equality operator

2008 Pearson Education,


Inc. All rights reserved.

22
23

// inequality operator; returns opposite of == operator

24

bool operator!=( const Array &right ) const

25

26

Outline

return ! ( *this
*this == right ); // invokes
invokes Array::operator==
} // end function operator!=

Array.h

29

// subscript operator for nonnon-const objects returns lvalue

(2 of 2)

30

int &operator[]( int );

27

105

28

31
32

// subscript
subscript operator for const objects returns rvalue

33

const int &operator[]( int ) const;


const;

34 private:
private:
35

int size; // pointerpointer-based array size

36

int *ptr; // pointer to first element of pointerpointer-based array

37 }; // end class Array


38
39 #endif

2008 Pearson Education,


Inc. All rights reserved.

106

Common Programming Error 11.10


Attempting to invoke an explicit constructor
for an implicit conversion is a compilation error.

2008 Pearson Education, Inc. All rights reserved.

107

Common Programming Error 11.11


Using the explicit keyword on data members
or member functions other than a singleargument constructor is a compilation error.

2008 Pearson Education, Inc. All rights reserved.

// Fig. 11.18: Fig11_18.cpp

// Driver for simple class Array.

#include <iostream>

using std::cout;

using std::endl;

Outline

Fig11_18.cpp

6
7

#include "Array.h"

8
9

108

void outputArray( const Array & ); // prototype

10

(1 of 2)on the
Using keyword explicit
conversion constructor disallows this line to
erroneously call the conversion constructor

11 int main()
12 {
13

Array integers1( 7 ); // 77-element array

14

outputArray( integers1 ); // output Array integers1

15

outputArray( 3 ); // convert 3 to an Array and output Arrays contents

16

outputArray( Array( 3 ) ); // explicit singlesingle-argument constructor call

17

return 0;

18 }

// end main

An explicit call to the conversion


constructor is still allowed

2008 Pearson Education,


Inc. All rights reserved.

19
20 // print array contents
21 void outputArray( const Array &arrayToOutput )

Outline

109

22 {
23
24

cout << "The Array received has " << arrayToOutput.getSize()


<< " elements. The contents are:\
are:\n" << arrayToOutput << endl;

Fig11_18.cpp

25 } // end outputArray
outputArray

(2 of 2)
c:\
c:\cpphtp6_examples\
cpphtp6_examples\ch11\
ch11\Fig11_17_18\
Fig11_17_18\Fig11_18.cpp(15) : error C2664:
'outputArray' : cannot convert parameter 1 from 'int' to 'const Array &'
Reason: cannot convert from 'int' to 'const Array'
Constructor for class 'Array' is declared 'explicit'

2008 Pearson Education,


Inc. All rights reserved.

110

Error-Prevention Tip 11.3


Use the explicit keyword on single-argument
constructors that should not be used by the
compiler to perform implicit conversions.

2008 Pearson Education, Inc. All rights reserved.

You might also like