f( int ), f( char )? ?? ??? ?? f? ???? ?? ??? int? char? ?? ?? ??? ??? ???? ??? ? ??. ??? ??? f( 1 )? ?? int ??? ?? ???? f( int )? ????, f( 'a' )? ?? char ??? ?? ???? f( char )? ????. - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

f( int ), f( char )? ?? ??? ?? f? ???? ?? ??? int? char? ?? ?? ??? ??? ???? ??? ? ??. ??? ??? f( 1 )? ?? int ??? ?? ???? f( int )? ????, f( 'a' )? ?? char ??? ?? ???? f( char )? ????.

Description:

Title: PowerPoint Author: wishfree Last modified by: Created Date: 11/23/2003 4:04:57 AM Document presentation format – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 28
Provided by: wish150
Category:

less

Transcript and Presenter's Notes

Title: f( int ), f( char )? ?? ??? ?? f? ???? ?? ??? int? char? ?? ?? ??? ??? ???? ??? ? ??. ??? ??? f( 1 )? ?? int ??? ?? ???? f( int )? ????, f( 'a' )? ?? char ??? ?? ???? f( char )? ????.


1
(No Transcript)
2
chapter 07. ????
3
????? ??
  • ????? ??? ??? ? ?? ??? ???? ?? ??? ?? ??? ?? ?
    ???? ?
  • f( int ), f( char )? ?? ??? ?? f? ???? ?? ???
    int? char? ?? ?? ??? ??? ???? ??? ? ??. ??? ???
    f( 1 )? ?? int ??? ?? ???? f( int )? ????, f( 'a'
    )? ?? char ??? ?? ???? f( char )? ????.
  • ???(Polymorphism)? ???? ?? ???? ??

4
????? ??
  • ?? ????? ?? ? ??? ???? ??
  • ?
  • 3 4
  • 3.0 4.0
  • 'a' 4
  • 0xFF 4.0f
  • ?? ???? ??? ??? ?? ??? ? ??? (3 _for_int 4)?
    plus_of_int ( 3, 4 )? ?? ??? ???? ?.
  • ? ??? ???? ???? ?? ??? ?? ???? ??? ??? ??? ? ???
    ?? ?? ????
  • print_int( 1 )
  • print_char( 'c' )
  • print_string( abc )
  • print_double( 1.0 )
  • ?
  • print ( 1 )

5
?? ????
  • ?? ??? ???? ?? ??? ??? ?? ??? ???? ?? ??? ???
    ???? ??
  • include ltiostreamgt
  • // ?? f? ????
  • void f () stdcout ltlt "f()" ltlt stdendl
  • void f ( int i ) stdcout ltlt "f( int )" ltlt
    stdendl
  • void f ( char c ) stdcout ltlt "f( char )" ltlt
    stdendl
  • void f ( double d ) stdcout ltlt "f( double )"
    ltlt stdendl
  • void f ( float f ) stdcout ltlt "f( float )" ltlt
    stdendl
  • int main ()
  • f ()
  • f ( 1 )
  • f ( 'a' )
  • f ( 1.0 )
  • f ( 1.0f )

????
f() f( int ) f( char ) f( double ) f( float )
6
?????? ??? ?
  • ??? ??? ??? ??? ???? ??? ? ??? ???? ?? ????? ????
    ? ?? ??? ??
  • ?? ??? ???? ????? ???
  • typedef, ??? ?? ??? ??
  • Inline
  • const, volatile ?? ?? ???(Type-Qualifier)
  • ??? ???? ????? ?? ?? ???? ??
  • ?? ??? ???

7
????? ??? ??(Overloading Resolution) I
  • ??? ???? ?? ???? ?? ??? ??? ???? ???? ?? ????,
    ??? ?? ??? ?? ??
  • ?? ???? ??? ? ?? ?? ??
  • ?
  • void f( int )
  • void f( double )
  • void f( char )
  • void f( long )
  • void f( long, char )
  • void f( long, char 0 )
  • f( 'a ' ) // ?? ??? ???? ????

8
????? ??? ??(Overloading Resolution) II
  • ?? ??
  • 1. ?? ?? ??(Candidate Functions) ?? ?? ??? ??,
    ?? ????? ??? ??? ??? ???? ?? ?? ???? ????.
  • 2. ?? ?? ?? ??(Viable Functions) ? ?? ??? ??, ??
    ?? ??? ?? ??? ??? ????.
  • 3. ?? ?? ??(Best Fit) ??? ?? ??? ?? ????? ? ???
    ? ?? ??? ????.

9
????? ??? ???
  • ????? ??? ???(Binding)? ??? ??? ??(Compile Time)
    ? ????.
  • ???? ???? ?? ?????? ??? ? ???? ????, ?? ??? ??
    ??? ?? ??? ?? ??? ???? ???? ?? ??? ??.

10
?? ??? ????
  • ?? ??? ?????? ?? ?? ??? ?????? ?? ?? ? ?? ????
    ??.
  • ?? ??? ?????? ??, ??? ??? ?????? ?? ? ??? ??? ???
    ? ??.
  • ??? const? ??? ???? ? ????? ???? ????? ??
  • ?? ??????? ??? static?? ???? ??? ?? ?? ??? ???
    ??? ????? ???

11
??? ????
  • ??? ?? ???? ???? ???? ??? ??? ? ??
  • fraction r
  • fraction f1( 3, 4 ) // 3/4
  • fraction f2( 7, 5 ) // 7/5
  • r.assign( f1.sum( f2 ) )
  • ?
  • fraction r
  • fraction f1( 3, 4 ) // 3/4
  • fraction f2( 7, 5 ) // 7/5
  • r f1 f2 // 3/4 7/5

12
??? ????? ?? I
  • ???? ??? ??? ????, ???? ?? ??? ????? ? ? ??? ???
    ? ??? ?? ?
  • ??? ?? ??
  • ??_??
  • operator operator ( ??_??_??? ) cv_???opt
  • ?? ??????? ?? ???? ???? int operator ? ()? ?
  • ?
  • f1 f2
  • ? fraction sum( fraction f1, fraction f2 )
  • ? fraction operator ( fraction f1, fraction f2 )
  • ? fraction operator ( const fraction f1, const
    fraction f2 )

13
??? ????? ?? II
  • ??? ?? ?? ??? ?? ?? ????? ?? ??
  • ? ??? ??? ?? ????(Left Operand)? ?? ????? ??? ?
    ?? ? ???? ?? ????(Right Operand)? ?? ??? ?? ???
    ?? ?? ? ? ??? ???? ??? ????? ??? ??
  • ? fraction fraction operator ( const fraction
    r )

14
??? ????? ?? III
  • ???? ??? ??? ?? ??? ???? ?? ??? ??? ??
  • ??? ??? ???? ????? ? ?? ???? ? ?? ????? ??? ??
    ??? ??? ??? ?. ??? (a ? b)? ?? ???? a? ??? ???
    ????? ?? ???? ?? ???? ??.
  • ?? ??? , ?? ??? , ?? ??? (), ?? ?? ??? -gt? ???
    ??? ??? ??? ?.

15
?? ???
  • ???? ?
  • ??_??
  • operator operator (const ?? , const ?? )
    cv_???opt
  • ?? ??? fraction? ?? ???? ??
  • class fraction
  • public
  • ...
  • fraction operator ( const fraction r )
  • numerator r.numerator denominator
    numerator r.denominator
  • denominator r.denominator
  • return this // ?? ??? ??

16
?? ??? I
  • ???? ?
  • ???_ID
  • ???_IDoperator (const ?? ) cv_???opt
  • ???? ???? ?? ???
  • ?? ?? ???? ????? ???? ???? ?? ???? ???? ?? ?
  • ???? ???? ?? ???? ???? ???? ???(In-line),
    public?? ???
  • ? ?? ?? ??? ?? ??? ??? ????.
  • ??? ???? ??? ?? ?? ???? ?? ???? ?? ????.
  • ??? ??? ?? ?? ??? ??? ??? ????.
  • ?? ??(Scalar Type)? ?? ??? ?? ???(Built-in
    Assignment Operator)? ?? ????.

17
?? ??? II
  • ??? fraction? ?? ??? 1
  • class fraction
  • public
  • void operator ( const fraction r )
  • numerator r.numerator
  • denominator r.denominator
  • (r1 r2 f)? ?? ???? ??? ???
  • ??? fraction? ?? ??? 2
  • fraction operator ( const fraction r )
  • fraction rst
  • ??? fraction? ?? ??? 3
  • fraction operator ( const fraction r )
  • numerator r.numerator
  • denominator r.denominator
  • return this

18
?? ??? III ?? ??
  • (f1 f1) ? ?? ??? ???? ???? ??
  • ?? ??? ???. ?? ???? ??? ??? ? ??.
  • ?? ?? ?? this ??? ??
  • fraction operator ( const fraction r )
  • if( this ! r) // ??? ?? ?????
  • numerator r.numerator
  • denominator r.denominator
  • return this // ??? ??

19
??, ?? ?? ??? I
  • i // ?? ??
  • i // ?? ??
  • ??? ??
  • ?/?? ??? ?? ??? ??? ??? ??? ??? ? ?? ?? ??
  • ??? ??? ? ??? operator ()? ??? ??? ?? ?? int
    ??? ?? ??? ?? ??
  • ??? ?
  • ??
  • ?? ?? ???_ID ???_IDoperator () ??_??
  • ?? ?? ???_ID ???_IDoperator ( int ) ??_??
  • ??
  • ?? ?? ???_ID operator ( const ???_ID )
    ??_??
  • ?? ?? ???_ID operator (const ???_ID , int )
    ??_??

20
??, ?? ?? ??? II
  • ?
  • list listoperator () // ???
  • if( p_current ! p_head )
  • p_current p_current-gtnext
  • return this
  • list listoperator (int) // ??? ?? ??
    int? ???? ??
  • if( p_current ! p_head )
  • p_current p_current-gtnext
  • return this
  • ???? ??? ??? ??? ?? ?? ?? ??? ???.
  • ???
  • ?? ????? ??? ?? ?? ?? ????, ??? ?? ?? ?? ??

21
??, ?? ?? ??? II
  • ??? ?? ??
  • list operator (int)
  • list current
  • current this // ?? ?? ??
  • if( p_current ! p_head ) // ??
  • p_current p_current.next
  • return current // ??? ?? ??

22
?? ???
  • ?? ???? ????? ??? ??? ??? ?.
  • ???? ?
  • ???_ID ???_IDoperator ( size_t index )
    ??_??
  • ?
  • element variable_arrayoperator ( size_t i )
  • return p_bufferi // ??? ?? ??

23
?? ?? ??? I
  • C ????? ??? ?? ?? ???? ??? ????? ??? ????? ???
    ?? ??(Function Object)? ??
  • ?? ??? ??? ????(Functional Paradigm)? ??? ? ?? ??
  • ?? ???????? ?? ltfunctionalgt?? plus, minus,
    greater, less, equal ?? ?? ????? ?? ??? ???? ???
    ??
  • ???? ?
  • ???_ID operator () ( ?? ??_?? ) ??_??

24
?? ?? ??? II
  • ??? sort? ?? ?? ???
  • include ltiostreamgt
  • struct sort
  • int operator () ( int p_data, size_t size )
  • for ( size_t i0 iltsize-1 i )
  • for ( size_t ji jltsize j)
  • if ( p_dataj lt p_datai )
  • int temp p_datai
  • p_dataip_dataj
  • p_datajtemp
  • return p_data
  • int main ()
  • sort s

?? ??
before 735134 sorted 133457
25
?? ?? ???? ??? ???
  • ?? ?? ??? ??? ?? ??? ????? ? ? ??
  • ??? -gt? . ? ???. ??? ?? .? ????? ???.
  • ??? -gt? ?????? ???? ????? ?? ????? ??? ? ?? ???
    ???? ??? ? ?? ???. ??? ?? ?? ??? ??? ? ?????? ??
    ???.

26
?? ?? ???? ??? ???
  • ?? ?? ???? ?
  • ?? operator -gt ( void ) ??_??
  • ??? ???? ?
  • ?? operator ( void ) ??_??
  • ?
  • class dynamic
  • public
  • element operator -gt ( void )
  • return p_data
  • element operator ( void )
  • return p_data
  • private
  • ...
  • element p_data // ???

27
????? ??? ???? ???? ?? ???
????? ??? ??? ????? ???? ?? ???
new delete new delete / ! lt gt / ltlt gtgt gtgt ltlt ! lt gt , gt gt () . . ?
Write a Comment
User Comments (0)
About PowerShow.com