home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
tmplt.zip
/
TMPLT2.CPP
< prev
Wrap
Text File
|
1994-03-21
|
2KB
|
64 lines
/*----------------------------------------------------------------------------*/
/* tmplt2.cpp */
/* */
/* template example - class with a template, conversion constructor, and an */
/* example of initialization with the "=" operator. */
/* */
/* (c) Larry Morley, 1994 */
/*----------------------------------------------------------------------------*/
#include <stdio.h>
int main(void);
/*----------------------------------------------------------------------------*/
template<class DataType>
class MathClass
{
private:
DataType Value; // The actual value of the object.
public:
MathClass(DataType InitialValue) // Constructor - allows the "=" in
{ // main() to pass a value instead
Value = InitialValue; // of having to use an explicit
} // classname(value) constructor.
operator DataType() // Allows variable to be used as if it were
{ // of the specified data type.
return Value;
}
multiply(DataType Num1,DataType Num2) // Define some member functions.
{
return Num1 * Num2;
}
divide(DataType Num1,DataType Num2)
{
return Num1 / Num2;
}
// etc.
};
/*----------------------------------------------------------------------------*/
int main()
{
MathClass<int> x = 5; // The "=" invokes the constructor.
int y = 3;
x = x.multiply(x,y); // Notice that x is used both as an integer, and
// as a class.
printf("The value of x is %d.\n",x);
return 0;
}
/*----------------------------------------------------------------------------*/