is 0020 program design and software tools introduction to c++ programming

42
2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Review slides Mar 15, 2005

Upload: domani

Post on 24-Jan-2016

43 views

Category:

Documents


0 download

DESCRIPTION

IS 0020 Program Design and Software Tools Introduction to C++ Programming. Review slides Mar 15, 2005. Review topics. Lvalue/rvalue Reference variable + Calling functions by reference Passing arrays to functions Function pointers Arrays of pointers to functions (e.g. menus) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

1

IS 0020 Program Design and Software Tools

Introduction to C++ Programming

Review slidesMar 15, 2005

Page 2: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

2

Review topics

1. Lvalue/rvalue2. Reference variable + Calling functions by reference 3. Passing arrays to functions 4. Function pointers 5. Arrays of pointers to functions (e.g. menus) 6. Using member selection operators ('.' '->') 7. Returning a reference to a private data member 8. *this pointer

1. implicit & explicit use of *this pointer 2. *this pointer & cascaded function calls

9. When are destructors used, why not yet in the classes we're creating? 10. When is it appropriate to use 'new' and 'delete' from the <new>

library? 11. Classes?12. Polymorphism?13. Multiple Inheritance?

Page 3: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

3

Confusing Equality (==) and Assignment (=) Operators

• Lvalues– Expressions that can appear on left side of equation

– Can be changedx = 4;

• Rvalues– Only appear on right side of equation

– Constants, such as numbers (i.e. cannot write 4 = x;)

• Lvalues can be used as rvalues, but not vice versa

Page 4: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

4

References and Reference Parameters

• Call by value– Copy of data passed to function– Changes to copy do not change original– Prevent unwanted side effects

• Call by reference – Function can directly access data– Changes affect original

• Reference parameter– Alias for argument in function call

• Passes parameter by reference

– Use & after data type in prototype• void myFunction( int &data )• Read “data is a reference to an int”

– Function call format the same• However, original can now be changed

Page 5: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

5

References and Reference Parameters

• Pointers– Another way to pass-by-refernce

• References as aliases to other variables– Refer to same variable

– Can be used within a functionint count = 1; // declare integer variable count

int &cRef = count; // create cRef as an alias for count

++cRef; // increment count (using its alias)

• References must be initialized when declared– Otherwise, compiler error

– Dangling reference• Reference to undefined variable

Page 6: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

6

Passing Arrays to Functions

• Specify name without brackets – To pass array myArray to myFunction

int myArray[ 24 ];

myFunction( myArray, 24 );

– Array size usually passed, but not required• Useful to iterate over all elements

• Arrays passed-by-reference – Functions can modify original array data

– Value of name of array is address of first element• Function knows where the array is stored

• Can change original memory locations

Page 7: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

7

Passing Arrays to Functions

• Functions taking arrays– Function prototype

• void modifyArray( int b[], int arraySize );• void modifyArray( int [], int );

– Names optional in prototype

• Both take an integer array and a single integer

– No need for array size between brackets• Ignored by compiler

– If declare array parameter as const• Cannot be modified (compiler error)• void doNotModify( const int [] );

Page 8: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

8

Using const with Pointers

• const qualifier– Value of variable should not be modified– const used when function does not need to change a variable– Principle of least privilege

• const pointers– Always point to same memory location– Default for array name– Must be initialized when declared

• Four ways to pass pointer to function– Nonconstant pointer to nonconstant data

• Highest amount of access

– Nonconstant pointer to constant data– Constant pointer to nonconstant data– Constant pointer to constant data

• Least amount of access

Page 9: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline9

fig05_13.cpp(1 of 1)

fig05_13.cppoutput (1 of 1)

1 // Fig. 5.13: fig05_13.cpp2 // Attempting to modify a constant pointer to3 // non-constant data.4 5 int main()6 {7 int x, y;8 9 // ptr is a constant pointer to an integer that can 10 // be modified through ptr, but ptr always points to the11 // same memory location. 12 int * const ptr = &x; 13 14 *ptr = 7; // allowed: *ptr is not const15 ptr = &y; // error: ptr is const; cannot assign new address16 17 return 0; // indicates successful termination18 19 } // end main

d:\cpphtp4_examples\ch05\Fig05_13.cpp(15) : error C2166: l-value specifies const object

ptr is constant pointer to integer.Can modify x (pointed to by

ptr) since x not constant.Cannot modify ptr to point to new address since ptr is constant.

Line 15 generates compiler error by attempting to assign new address to constant pointer.

Page 10: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline10

fig05_14.cpp(1 of 1)

1 // Fig. 5.14: fig05_14.cpp2 // Attempting to modify a constant pointer to constant data.3 #include <iostream>4 5 using std::cout;6 using std::endl;7 8 int main()9 {10 int x = 5, y;11 12 // ptr is a constant pointer to a constant integer. 13 // ptr always points to the same location; the integer14 // at that location cannot be modified.15 const int *const ptr = &x; 16 17 cout << *ptr << endl;18 19 *ptr = 7; // error: *ptr is const; cannot assign new value 20 ptr = &y; // error: ptr is const; cannot assign new address21 22 return 0; // indicates successful termination23 24 } // end main

ptr is constant pointer to integer constant.

Cannot modify x (pointed to by ptr) since *ptr declared constant.Cannot modify ptr to point to new address since ptr is constant.

Page 11: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

11

Relationship Between Pointers and Arrays

• Arrays and pointers closely related– Array name like constant pointer

– Pointers can do array subscripting operations

• Accessing array elements with pointers– Element b[ n ] can be accessed by *( bPtr + n )

• Called pointer/offset notation

– Addresses• &b[ 3 ] same as bPtr + 3

– Array name can be treated as pointer• b[ 3 ] same as *( b + 3 )

– Pointers can be subscripted (pointer/subscript notation)• bPtr[ 3 ] same as b[ 3 ]

Page 12: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

12

Function Pointers

• Calling functions using pointers– Assume parameter:

• bool ( *compare ) ( int, int )

– Execute function with either• ( *compare ) ( int1, int2 )

– Dereference pointer to function to execute

OR• compare( int1, int2 )

– Could be confusing

• User may think compare name of actual function in program

Page 13: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline13

fig05_25.cpp(1 of 5)

1 // Fig. 5.25: fig05_25.cpp2 // Multipurpose sorting program using function pointers.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 // prototypes14 void bubble( int [], const int, bool (*)( int, int ) );15 void swap( int * const, int * const ); 16 bool ascending( int, int );17 bool descending( int, int );18 19 int main()20 {21 const int arraySize = 10;22 int order; 23 int counter;24 int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };25

Parameter is pointer to function that receives two integer parameters and returns bool result.

Page 14: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline14

fig05_25.cpp(2 of 5)

26 cout << "Enter 1 to sort in ascending order,\n" 27 << "Enter 2 to sort in descending order: ";28 cin >> order;29 cout << "\nData items in original order\n";30 31 // output original array32 for ( counter = 0; counter < arraySize; counter++ )33 cout << setw( 4 ) << a[ counter ];34 35 // sort array in ascending order; pass function ascending 36 // as an argument to specify ascending sorting order37 if ( order == 1 ) {38 bubble( a, arraySize, ascending );39 cout << "\nData items in ascending order\n";40 }41 42 // sort array in descending order; pass function descending43 // as an agrument to specify descending sorting order44 else {45 bubble( a, arraySize, descending );46 cout << "\nData items in descending order\n";47 }48

Page 15: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline15

fig05_25.cpp(3 of 5)

49 // output sorted array50 for ( counter = 0; counter < arraySize; counter++ )51 cout << setw( 4 ) << a[ counter ];52 53 cout << endl;54 55 return 0; // indicates successful termination56 57 } // end main58 59 // multipurpose bubble sort; parameter compare is a pointer to60 // the comparison function that determines sorting order61 void bubble( int work[], const int size, 62 bool (*compare)( int, int ) )63 {64 // loop to control passes65 for ( int pass = 1; pass < size; pass++ )66 67 // loop to control number of comparisons per pass68 for ( int count = 0; count < size - 1; count++ )69 70 // if adjacent elements are out of order, swap them71 if ( (*compare)( work[ count ], work[ count + 1 ] ) )72 swap( &work[ count ], &work[ count + 1 ] );

compare is pointer to function that receives two integer parameters and returns bool result.

Parentheses necessary to indicate pointer to function

Call passed function compare; dereference pointer to execute function.

Page 16: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline16

fig05_25.cpp(4 of 5)

73 74 } // end function bubble75 76 // swap values at memory locations to which 77 // element1Ptr and element2Ptr point78 void swap( int * const element1Ptr, int * const element2Ptr )79 {80 int hold = *element1Ptr;81 *element1Ptr = *element2Ptr;82 *element2Ptr = hold;83 84 } // end function swap85 86 // determine whether elements are out of order87 // for an ascending order sort 88 bool ascending( int a, int b ) 89 { 90 return b < a; // swap if b is less than a91 92 } // end function ascending 93

Page 17: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline17

fig05_25.cpp(5 of 5)

fig05_25.cppoutput (1 of 1)

94 // determine whether elements are out of order 95 // for a descending order sort 96 bool descending( int a, int b ) 97 { 98 return b > a; // swap if b is greater than a99 100 } // end function descending

Enter 1 to sort in ascending order,Enter 2 to sort in descending order: 1 Data items in original order 2 6 4 8 10 12 89 68 45 37Data items in ascending order 2 4 6 8 10 12 37 45 68 89

Enter 1 to sort in ascending order,

Enter 2 to sort in descending order: 2

 

Data items in original order

2 6 4 8 10 12 89 68 45 37

Data items in descending order

89 68 45 37 12 10 8 6 4 2

Page 18: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

18

Function Pointers

• Arrays of pointers to functions– Menu-driven systems

– Pointers to each function stored in array of pointers to functions

• All functions must have same return type and same parameter types

– Menu choice subscript into array of function pointers

Page 19: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline19

fig05_26.cpp(1 of 3)

1 // Fig. 5.26: fig05_26.cpp2 // Demonstrating an array of pointers to functions.3 #include <iostream>4 5 using std::cout;6 using std::cin;7 using std::endl;8 9 // function prototypes10 void function1( int );11 void function2( int );12 void function3( int );13 14 int main()15 {16 // initialize array of 3 pointers to functions that each 17 // take an int argument and return void 18 void (*f[ 3 ])( int ) = { function1, function2, function3 };19 20 int choice;21 22 cout << "Enter a number between 0 and 2, 3 to end: ";23 cin >> choice;24

Array initialized with names of three functions; function names are pointers.

Page 20: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline20

fig05_26.cpp(2 of 3)

25 // process user's choice26 while ( choice >= 0 && choice < 3 ) {27 28 // invoke function at location choice in array f29 // and pass choice as an argument 30 (*f[ choice ])( choice ); 31 32 cout << "Enter a number between 0 and 2, 3 to end: ";33 cin >> choice;34 }35 36 cout << "Program execution completed." << endl;37 38 return 0; // indicates successful termination39 40 } // end main41 42 void function1( int a )43 {44 cout << "You entered " << a 45 << " so function1 was called\n\n";46 47 } // end function148

Call chosen function by dereferencing corresponding element in array.

Page 21: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline21

fig05_26.cpp(3 of 3)

fig05_26.cppoutput (1 of 1)

49 void function2( int b )50 {51 cout << "You entered " << b 52 << " so function2 was called\n\n";53 54 } // end function255 56 void function3( int c )57 {58 cout << "You entered " << c 59 << " so function3 was called\n\n";60 61 } // end function3

Enter a number between 0 and 2, 3 to end: 0

You entered 0 so function1 was called

Enter a number between 0 and 2, 3 to end: 1

You entered 1 so function2 was called

Enter a number between 0 and 2, 3 to end: 2

You entered 2 so function3 was called

Enter a number between 0 and 2, 3 to end: 3

Program execution completed.

Page 22: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

22

Accessing Structure Members

• Member access operators– Dot operator (.) for structure and class members

– Arrow operator (->) for structure and class members via pointer to object

– Print member hour of timeObject:

cout << timeObject.hour;

OR

timePtr = &timeObject; cout << timePtr->hour;

– timePtr->hour same as ( *timePtr ).hour• Parentheses required

– * lower precedence than .

Page 23: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

23

Subtle Trap: Returning a Reference to a private Data Member

• Reference to object– &pRef = p;

– Alias for name of object

– Lvalue• Can receive value in assignment statement

– Changes original object

• Returning references– public member functions can return non-const

references to private data members• Client able to modify private data members

Page 24: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline24

time4.h (1 of 1)

1 // Fig. 6.21: time4.h2 // Declaration of class Time.3 // Member functions defined in time4.cpp4 5 // prevent multiple inclusions of header file6 #ifndef TIME4_H 7 #define TIME4_H 8 9 class Time {10 11 public:12 Time( int = 0, int = 0, int = 0 );13 void setTime( int, int, int );14 int getHour();15 16 int &badSetHour( int ); // DANGEROUS reference return17 18 private:19 int hour;20 int minute;21 int second;22 23 }; // end class Time24 25 #endif

Function to demonstrate effects of returning reference to private data member.

Page 25: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline25

time4.cpp (2 of 2)

25 // return hour value26 int Time::getHour() 27 { 28 return hour; 29 30 } // end function getHour31 32 // POOR PROGRAMMING PRACTICE: 33 // Returning a reference to a private data member.34 int &Time::badSetHour( int hh ) 35 { 36 hour = ( hh >= 0 && hh < 24 ) ? hh : 0; 37 38 return hour; // DANGEROUS reference return 39 40 } // end function badSetHour

Return reference to private data member hour.

Page 26: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline26

fig06_23.cpp(1 of 2)

1 // Fig. 6.23: fig06_23.cpp2 // Demonstrating a public member function that3 // returns a reference to a private data member.4 #include <iostream>5 6 using std::cout;7 using std::endl;8 9 // include definition of class Time from time4.h10 #include "time4.h"11 12 int main()13 {14 Time t;15 16 // store in hourRef the reference returned by badSetHour17 int &hourRef = t.badSetHour( 20 ); 18 19 cout << "Hour before modification: " << hourRef;20 21 // use hourRef to set invalid value in Time object t22 hourRef = 30; 23 24 cout << "\nHour after modification: " << t.getHour();25

badSetHour returns reference to private data member hour.

Reference allows setting of private data member hour.

Page 27: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline27

fig06_23.cpp(2 of 2)

fig06_23.cppoutput (1 of 1)

26 // Dangerous: Function call that returns27 // a reference can be used as an lvalue!28 t.badSetHour( 12 ) = 74; 29 30 cout << "\n\n*********************************\n"31 << "POOR PROGRAMMING PRACTICE!!!!!!!!\n"32 << "badSetHour as an lvalue, Hour: "33 << t.getHour()34 << "\n*********************************" << endl;35 36 return 0;37 38 } // end main

Hour before modification: 20

Hour after modification: 30

 

*********************************

POOR PROGRAMMING PRACTICE!!!!!!!!

badSetHour as an lvalue, Hour: 74

*********************************

Can use function call as lvalue to set invalid value.

Returning reference allowed invalid setting of private data member hour.

Page 28: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

28

Default Memberwise Assignment

• Assigning objects– Assignment operator (=)

• Can assign one object to another of same type

• Default: memberwise assignment

– Each right member assigned individually to left member

• Passing, returning objects– Objects passed as function arguments

– Objects returned from functions

– Default: pass-by-value• Copy of object passed, returned

– Copy constructor

• Copy original values into new object

Page 29: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

29

Dynamic Memory Management with Operatorsnew and delete

• Dynamic memory management– Control allocation and deallocation of memory– Operators new and delete

• Include standard header <new>• new

Time *timePtr;timePtr = new Time;

– Creates object of proper size for type Time• Error if no space in memory for object

– Calls default constructor for object– Returns pointer of specified type– Providing initializers

double *ptr = new double( 3.14159 );Time *timePtr = new Time( 12, 0, 0 );

– Allocating arraysint *gradesArray = new int[ 10 ];

Page 30: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

30

Dynamic Memory Management with Operators new and delete

• delete– Destroy dynamically allocated object and free space– Consider

delete timePtr;– Operator delete

• Calls destructor for object• Deallocates memory associated with object

– Memory can be reused to allocate other objects

– Deallocating arraysdelete [] gradesArray;

– Deallocates array to which gradesArray points• If pointer to array of objects

• First calls destructor for each object in array• Then deallocates memory

Page 31: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

31

static Class Members

• static class variable– “Class-wide” data

• Property of class, not specific object of class

– Efficient when single copy of data is enough • Only the static variable has to be updated

– May seem like global variables, but have class scope• Only accessible to objects of same class

– Initialized exactly once at file scope

– Exist even if no objects of class exist

– Can be public, private or protected

Page 32: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

32

static Class Members

• Accessing static class variables– Accessible through any object of class– public static variables

• Can also be accessed using binary scope resolution operator(::) Employee::count

– private static variables• When no class member objects exist: Can only be accessed via public static member function

Employee::getCount()

• static member functions– Cannot access non-static data or functions – No this pointer for static functions

• static data members and static member functions exist independent of objects

Page 33: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline33

employee2.h (1 of 2)

1 // Fig. 7.17: employee2.h2 // Employee class definition.3 #ifndef EMPLOYEE2_H4 #define EMPLOYEE2_H5 6 class Employee {7 8 public:9 Employee( const char *, const char * ); // constructor10 ~Employee(); // destructor11 const char *getFirstName() const; // return first name12 const char *getLastName() const; // return last name13 14 // static member function 15 static int getCount(); // return # objects instantiated16 17 private:18 char *firstName;19 char *lastName;20 21 // static data member 22 static int count; // number of objects instantiated23 24 }; // end class Employee25

static member function can only access static data members and member functions.

static data member is class-wide data.

Page 34: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline34

employee2.h (2 of 2)

employee2.cpp(1 of 3)

26 #endif

1 // Fig. 7.18: employee2.cpp2 // Member-function definitions for class Employee.3 #include <iostream>4 5 using std::cout;6 using std::endl;7 8 #include <new> // C++ standard new operator9 #include <cstring> // strcpy and strlen prototypes10 11 #include "employee2.h" // Employee class definition 12 13 // define and initialize static data member14 int Employee::count = 0; 15 16 // define static member function that returns number of17 // Employee objects instantiated 18 int Employee::getCount() 19 { 20 return count; 21 22 } // end static function getCount

Initialize static data member exactly once at file scope.

static member function accesses static data member count.

Page 35: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline35

employee2.cpp(2 of 3)

23 24 // constructor dynamically allocates space for 25 // first and last name and uses strcpy to copy 26 // first and last names into the object 27 Employee::Employee( const char *first, const char *last )28 { 29 firstName = new char[ strlen( first ) + 1 ];30 strcpy( firstName, first );31 32 lastName = new char[ strlen( last ) + 1 ]; 33 strcpy( lastName, last );34 35 ++count; // increment static count of employees36 37 cout << "Employee constructor for " << firstName38 << ' ' << lastName << " called." << endl;39 40 } // end Employee constructor41 42 // destructor deallocates dynamically allocated memory43 Employee::~Employee()44 {45 cout << "~Employee() called for " << firstName46 << ' ' << lastName << endl;47

new operator dynamically allocates space.

Use static data member to store total count of employees.

Page 36: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline36

employee2.cpp(3 of 3)

48 delete [] firstName; // recapture memory49 delete [] lastName; // recapture memory50 51 --count; // decrement static count of employees52 53 } // end destructor ~Employee54 55 // return first name of employee56 const char *Employee::getFirstName() const57 {58 // const before return type prevents client from modifying59 // private data; client should copy returned string before60 // destructor deletes storage to prevent undefined pointer61 return firstName;62 63 } // end function getFirstName64 65 // return last name of employee66 const char *Employee::getLastName() const67 {68 // const before return type prevents client from modifying69 // private data; client should copy returned string before70 // destructor deletes storage to prevent undefined pointer71 return lastName;72 73 } // end function getLastName

Operator delete deallocates memory.

Use static data member to store total count of employees.

Page 37: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

37

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

• Example 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

Page 38: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

38

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

Page 39: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc. All rights reserved.

39

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

Page 40: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline40

fig08_03.cpp(1 of 3)

1 // Fig. 8.3: fig08_03.cpp2 // Overloading the stream-insertion and 3 // stream-extraction operators.4 #include <iostream>5 6 using std::cout;7 using std::cin;8 using std::endl;9 using std::ostream;10 using std::istream;11 12 #include <iomanip>13 14 using std::setw;15 16 // PhoneNumber class definition17 class PhoneNumber {18 friend ostream &operator<<( ostream&, const PhoneNumber & );19 friend istream &operator>>( istream&, PhoneNumber & ); 20 21 private:22 char areaCode[ 4 ]; // 3-digit area code and null23 char exchange[ 4 ]; // 3-digit exchange and null24 char line[ 5 ]; // 4-digit line and null25 26 }; // end class PhoneNumber

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 << objectcout >> object

Page 41: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline41

fig08_03.cpp(2 of 3)

27 28 // overloaded stream-insertion operator; cannot be 29 // a member function if we would like to invoke it with 30 // cout << somePhoneNumber; 31 ostream &operator<<( ostream &output, const PhoneNumber &num )32 { 33 output << "(" << num.areaCode << ") " 34 << num.exchange << "-" << num.line; 35 36 return output; // enables cout << a << b << c; 37 38 } // end function operator<< 39 40 // overloaded stream-extraction operator; cannot be 41 // a member function if we would like to invoke it with 42 // cin >> somePhoneNumber; 43 istream &operator>>( istream &input, PhoneNumber &num ) 44 { 45 input.ignore(); // skip ( 46 input >> setw( 4 ) >> num.areaCode; // input area code 47 input.ignore( 2 ); // skip ) and space48 input >> setw( 4 ) >> num.exchange; // input exchange 49 input.ignore(); // skip dash (-) 50 input >> setw( 5 ) >> num.line; // input line 51 52 return input; // enables cin >> a >> b >> c;

The expression:cout << phone; is interpreted as the function call:operator<<(cout, phone);

output is an alias for cout.

This allows objects to be cascaded.cout << phone1 << phone2;first calls operator<<(cout, phone1), and returns cout.

Next, cout << phone2 executes.Stream manipulator setw restricts number of characters read. setw(4) allows 3 characters to be read, leaving room for the null character.

Page 42: IS 0020  Program Design and Software Tools Introduction to C++ Programming

2003 Prentice Hall, Inc.All rights reserved.

Outline42

fig08_03.cpp(3 of 3)

fig08_03.cppoutput (1 of 1)

53 54 } // end function operator>> 55 56 int main()57 {58 PhoneNumber phone; // create object phone59 60 cout << "Enter phone number in the form (123) 456-7890:\n";61 62 // cin >> phone invokes operator>> by implicitly issuing63 // the non-member function call operator>>( cin, phone )64 cin >> phone; 65 66 cout << "The phone number entered was: " ;67 68 // cout << phone invokes operator<< by implicitly issuing69 // the non-member function call operator<<( cout, phone )70 cout << phone << endl; 71 72 return 0;73 74 } // end main

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

(800) 555-1212

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