home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
tmplt.zip
/
TMPLT1.CPP
< prev
next >
Wrap
Text File
|
1994-03-21
|
2KB
|
49 lines
/*----------------------------------------------------------------------------*/
/* tmplt1.cpp */
/* */
/* template example - raise a number to a specified power. Demonstrates */
/* creating a function using templates. */
/* */
/* (c) Larry Morley, 1994 */
/*----------------------------------------------------------------------------*/
#include <iostream.h>
int main(void);
/*----------------------------------------------------------------------------*/
template<class NUMBER, class POWER> // improper types will be caught
NUMBER power(NUMBER num,POWER exp) // by the compiler
{
int i,origNum = num;
if (!exp)
return 1;
for (i=0;i<exp-1;i++)
num *= origNum;
return num;
};
/*----------------------------------------------------------------------------*/
int main()
{
int number, exponent;
cout << "What is the number? ";
cin >> number;
cout << "Raise to what power? ";
cin >> exponent;
cout << "The result is "
<< power(number,exponent)
<< ".\n";
return 0;
}
/*----------------------------------------------------------------------------*/