Page 311 - DCAP404 _Object Oriented Programming
P. 311

Object-oriented Programming




                    Notes
                                          Example:

                                          Template  <  class    X  >
                                          X min ( X a , X b )
                                          {
                                          return (a  < b  ) ?  a :  b ;
                                          }
                                   This list of parameter types is called the formal parameter list of the template, and it cannot be
                                   empty. Each formal parameter consists of the keyword, type name, followed by an identifier.
                                   The identifier can be built-in or user-defined data type, or the identifier type. When the function
                                   is invoked with actual parameters, the identifier type is substituted with the actual type of the
                                   parameter. This allows the use of any data type. The template declaration immediately precedes
                                   the definition of the function for which the template is being defined. The template declaration,
                                   followed by the function definition, constitutes the template definition. The template of the max
                                   ( ) function is coded below:
                                          #  include    <  iostream.h  >
                                          template  <  class  type  >
                                          type  max  (    type  x  ,  type  y)
                                          {
                                                     return x > y ? x : y ;

                                          }
                                          int  main  (  )
                                          {
                                          cout    <<  “  max  (  ‘A’,  ‘a’) :  “  <<  max  (  ‘A’,  ‘a’)    <<  endl  ;
                                          cout    <<    “ max  (  30,  40  ) :  “  <<  max  (  30  ,  40)  <<    endl;
                                          cout    <<  “  max  (  45  .  67F,  12  .  32  F)  :  “  <<  max  (45.  67F,  12  .
                                   32  F)    <,
                                          endl;
                                          return      0  ;
                                          }
                                   Output
                                          max ( ‘A‘ , ‘a‘)                      : a
                                          max ( 30 , 40 )                      : 40

                                          max ( 45.67F, 12 . 32F )       : 45 . 67
                                   In the example, the list of parameters is composed of only one parameter. The next line specifies
                                   that the  function takes two arguments  and returns a value,  all of the defined in the formal
                                   parameter list. See what  happens if the following command is issued, keeping in mind  the
                                   template definition for max ( ) :
                                          max ( a , b ) ;






          304                               LOVELY PROFESSIONAL UNIVERSITY
   306   307   308   309   310   311   312   313   314   315   316