Page 313 - DCAP404 _Object Oriented Programming
P. 313

Object-oriented Programming




                    Notes          Self Assessment

                                   Fill in the blanks:
                                   1.  A template function may be defined as an ...................... .
                                   2.  Template functions and classes are usable for any ...................... .

                                   3.  ...................... would consist of instantiating the template definition for specific data type.
                                   4.  ...................... errors are  relatively easy  to find and correct, even if the resulting  error
                                       messages are unclear.

                                   5.  The identifier can be built-in or user-defined data type, or the ...................... type.
                                   14.2 Classes of Template


                                   Template classes may be defined as the layout and operations for an unbounded set of related
                                   classes. Built in data types can be given as template arguments in the list for template classes.

                                   Syntax
                                          Template  <  class  type  1  ,  ...  >
                                          Class    class  -  name
                                          {
                                          public
                                          type  1  var,

                                          ...
                                          };
                                   Suppose you want two different classes for the same data type this can be achieved through the
                                   following code:

                                          #  include  <  iostream.h  >
                                          template  <  class  T,  int  z  >
                                          class  W
                                          {
                                                 public  :
                                                 T a ;
                                                 W ( T q )

                                                 {
                                                        a = z + q ;
                                                        cout  <<  “  a  =  “  <<  a  <<  endl  ;
                                                 }
                                          }  ;
                                          int  main  (  )
                                          {
                                                 W < int,10 > one ( 100) ;            // Displays a = 110




          306                               LOVELY PROFESSIONAL UNIVERSITY
   308   309   310   311   312   313   314   315   316   317   318