Docsity

A Simple Overloading Example - Lecture Slides | CSCI 261, Study notes of Computer Science

Partial preview of the text

Download A Simple Overloading Example - Lecture Slides | CSCI 261 and more Study notes Computer Science in PDF only on Docsity!

Operator Overloading

July 29, 2009

A Simple Overloading Example

15 / / p ro mp t f o r a d o u b l e , i g n o r e c i n f a i l u r e 16 v o i d p r o m p t _ f o r _ i n p u t ( c o n s t s t r i n g & prompt , 17 d o u b l e& r e s p o n s e ) 18 { 19 c o u t << p r o m p t << e n d l ; 20 c i n >> r e s p o n s e ; 21 r e t u r n ; 22 } 23 24 / / p ro mp t f o r a s t r i n g , i g n o r e c i n f a i l u r e 25 v o i d p r o m p t _ f o r _ i n p u t ( c o n s t s t r i n g & prompt , 26 s t r i n g & r e s p o n s e ) 27 { 28 c o u t << p r o m p t << e n d l ; 29 c i n >> r e s p o n s e ; 30 r e t u r n ; 31 } 32 33 / / p ro mp t f o r a i n t , i g n o r e c i n f a i l u r e 34 v o i d p r o m p t _ f o r _ i n p u t ( c o n s t s t r i n g & prompt , 35 i n t & r e s p o n s e ) 36 { 37 c o u t << p r o m p t << e n d l ; 38 c i n >> r e s p o n s e ; 39 r e t u r n ; 40 }

Overloaded Input Functions

ā—® Define three overloaded

functions for user input.

ā—® Each takes a prompt

argument, and a reference

to a variable location

where user input will be

written.

A Simple Overloading Example

Using the Overloaded Functions

46 āˆ— A r a i l r o a d e f f i c i e n c y s i m u l a t o r 47 āˆ— / 48 49 d o u b l e e n g i n e _ h o r s e _ p o w e r ; 50 d o u b l e e n g i n e _ c o s t _ e f f i c i e n c y ; 51 52 i n t n u m b e r _ o f _ c a r s ; 53 d o u b l e m e t r i c _ t o n s _ p e r _ c a r ; 54 55 s t r i n g s t a r t C i t y ; 56 s t r i n g e n d C i t y ; 57 58 / / i n p u t f ro m u s e r 59 p r o m p t _ f o r _ i n p u t ( " Enter the engine horse power : " , e n g i n e _ h o r s e _ p o w e r ) ; 60 p r o m p t _ f o r _ i n p u t ( " Enter the engine efficiency : " , e n g i n e _ c o s t _ e f f i c i e n c y ) ; 61 p r o m p t _ f o r _ i n p u t ( " Enter the number of cars : " , n u m b e r _ o f _ c a r s ) ; 62 p r o m p t _ f o r _ i n p u t ( " Enter the mass of each car : " , m e t r i c _ t o n s _ p e r _ c a r ) ; 63 p r o m p t _ f o r _ i n p u t ( " Enter the city where the train starts : " , s t a r t C i t y ) ; 64 p r o m p t _ f o r _ i n p u t ( " Enter the destination city : " , e n d C i t y ) ;

Simply call the overloaded function with the correct prompt and variable.

What’s an Operator?

A specially named function that defines the logic for for C++ operations.

a + b a - b a Ɨ b a Ć· b

a++ ++a a-- --a

Ā« Ā» a % b ...

Lecture Question

Answer question 1.

Operator Overview

Three Different Kinds of Operator Implementations

1. Global operators implemented by the non-class author.

2. Global operators implemented by the class author.

3. friend-Global operators implemented by the class author.

(There are actually four, but we don’t cover member function operators in CSCI 261.)

Operator Examples Global Op written by Class Author

Global Ops by the Class Author

1. Prototyped in the class header file, after the class declaration.

2. Since these are not class member functions, they must use the public

interface to the class to ā€œget the job done.ā€

3. Implemented in the class source file.

Operator Examples Global Op written by Class Author

Global Ops by the Class Author

These types of operators are awkward for the class author to write: they

cannot access the private data members directly, instead they must use the

public accessors!

So sad. :^(

Operator Examples friend-Global Op written by Class Author

friend-Global Ops by the Class Author

1. Cool! friend-global operators that has priviledged access to a class’

private data members and member functions.

2. It is not written in class scope (there is no Classname:: before

operator function name).

3. There is no calling object (class data members are available only through

a function parameter and ā€œ.ā€-member access.

Global Operator Overloading Nitty-Gritty Signature Patterns

Global Operators

fooClass x, y; int z;

In Code Function Implementation Header

z + x int operator+( int lhs, const fooClass& rhs )

xz fooClass operator( const fooClass& lhs, int lhs )

cout « x ostream& operator«( ostream& os, const fooClass& rhs )

cin Ā» y istream& operatorĀ»( istream& is, fooClass& rhs )

objA @ objB typeTBD operator@( const typeA& lhs, const typeB& rhs )

TBD ≔ To Be Determined (by the programmer!)

Two arguments representing either side of the binary operator.

Global Operator Overloading Nitty-Gritty Signature Patterns

Question 3 a–e.

Global Operator Overloading Nitty-Gritty friended Operators

Everyone Needs a friend

ā—® May be used only inside of class declarations.

ā—® Always modifies a non-member (global) function.

ā—® friend goes before function’s return type.

ā—® friend is not used in function definition.

ā—® A friended function has access to class instance members as if it were in

class scope.

Global Operator Overloading Nitty-Gritty friended Operators

Question 5.

Global Operator Overloading Nitty-Gritty friended Operators

Input & Output Suggestions

ā—® Opt for symmetry in << and >> operators.

ā—® Use operator<< and operator>> for datafile i/o ; perhaps not easily read

by human.

ā—® Use operator<< and operator>> should be symmetric.

They should read and write the same data format.

ā—® Don’t embed newlines into these output methods.

ā—® print( ostream& os ) for human readable output (log files, debug

files, printer, ...)

Feel like you’ve heard all of this before? You have, I mention it again, here, because these are widely considered best programming techniques. So you should hear about them more than once.