home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tmplt.zip / TMPLT1.CPP < prev    next >
Text File  |  1994-03-21  |  2KB  |  49 lines

  1. /*----------------------------------------------------------------------------*/
  2. /* tmplt1.cpp                                                                 */
  3. /*                                                                            */
  4. /* template example - raise a number to a specified power.  Demonstrates      */
  5. /*                    creating a function using templates.                    */
  6. /*                                                                            */
  7. /* (c) Larry Morley, 1994                                                     */
  8. /*----------------------------------------------------------------------------*/
  9.  
  10. #include <iostream.h>
  11.  
  12. int main(void);
  13.  
  14. /*----------------------------------------------------------------------------*/
  15.  
  16. template<class NUMBER, class POWER>    // improper types will be caught
  17.    NUMBER power(NUMBER num,POWER exp)  // by the compiler
  18.    {
  19.       int i,origNum = num;
  20.  
  21.       if (!exp)
  22.          return 1;
  23.  
  24.       for (i=0;i<exp-1;i++)
  25.          num *= origNum;
  26.  
  27.       return num;
  28.    };
  29.  
  30. /*----------------------------------------------------------------------------*/
  31.  
  32. int main()
  33. {
  34.    int number, exponent;
  35.  
  36.    cout << "What is the number? ";
  37.    cin  >> number;
  38.    cout << "Raise to what power? ";
  39.    cin  >> exponent;
  40.  
  41.    cout << "The result is "
  42.         << power(number,exponent)
  43.         << ".\n";
  44.  
  45.    return 0;
  46. }
  47.  
  48. /*----------------------------------------------------------------------------*/
  49.