show_op_over

show_op_over - §10.1–10.2 Function & Operator...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: §10.1–10.2 Function & Operator Overloading March 31, 2008 Overloaded Functions Overloaded Functions Overloaded Functions Function Signatures 1 / ** 2 * Main f u n c t i o n comment d e s c r i b e s what t h e f u n c t i o n does , s p e c i a l p r o p e r t i e s 3 * o f i t s arguments , what i t r e t u r n s , and i t s r e t u r n i n t e r p r e t a t i o n . 4 * / 5 r e t u r n _ t y p e f u n ct i o n _ n am e ( a r g 1 _ t y p e arg1_name , a r g 2 _ t y p e arg2_name ) 6 { 7 v a r i a b l e d e c l a r a t i o n s ; 8 9 f u n c t i o n s t a t e m e n t s ; 10 11 r e t u r n v a l u e ; 12 } function signature = function_name + function’s scope + argument types + const ’ness ◮ The argument names ( arg1_name, arg2_name ) are NOT part of the function signature. ◮ The return_type and its const ’ness is NOT part of the function signature. ◮ The const ’ness of reference arguments matters. ◮ The const ’ness of of a class member function matters. Overloaded Functions Function Signatures 1 / ** 2 * Main f u n c t i o n comment d e s c r i b e s what t h e f u n c t i o n does , s p e c i a l p r o p e r t i e s 3 * o f i t s arguments , what i t r e t u r n s , and i t s r e t u r n i n t e r p r e t a t i o n . 4 * / 5 r e t u r n _ t y p e f u n ct i o n _ n am e ( a r g 1 _ t y p e arg1_name , a r g 2 _ t y p e arg2_name ) 6 { 7 v a r i a b l e d e c l a r a t i o n s ; 8 9 f u n c t i o n s t a t e m e n t s ; 10 11 r e t u r n v a l u e ; 12 } function signature = function_name + function’s scope + argument types + const ’ness ◮ Internally, the C++ compiler and linker distinguishes between like-named functions that have different signatures. Overloaded Functions A Simple Overloading Example 18 / / prompt f o r a double , i g n o r e c i n f a i l u r e 19 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 , 20 d o u b l e & r e s p o n s e ) 21 { 22 c o u t << prompt << e n d l ; 23 c i n >> r e s p o n s e ; 24 r e t u r n ; 25 } 26 27 / / prompt f o r a double , i g n o r e c i n f a i l u r e 28 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 , 29 s t r i n g & r e s p o n s e ) 30 { 31 c o u t << prompt << e n d l ; 32 c i n >> r e s p o n s e ; 33 r e t u r n ; 34 } 35 36 / / prompt f o r a double , i g n o r e c i n f a i l u r e 37 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 , 38 i n t & r e s p o n s e ) 39 { 40 c o u t << prompt << e n d l ; 41 c i n >> r e s p o n s e ; 42 r e t u r n ; 43 } 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. Overloaded Functions A Simple Overloading Example 18 / / prompt f o r a double , i g n o r e c i n f a i l u r e 19 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 , 20 d o u b l e & r e s p o n s e ) 21 { 22 c o u t << prompt << e n d l...
View Full Document

This note was uploaded on 04/07/2008 for the course CSCI 261 taught by Professor Hellman during the Spring '08 term at Mines.

Page1 / 34

show_op_over - §10.1–10.2 Function & Operator...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online