This is a simple class to add things up. They can be anything for which the += operator is defined (or overloaded )
Code:
template < class T >
class Sum
{
public:
Sum( ) { total = 0; }
Sum( T* typeArray, int size );
T calcTotal( );
private:
T* array; //pointer to array
T total; //total
int size; //size of array passed via constructor
};
T Sum::calcTotal( )
{
total = 0;
for( int i = 0; i < size; i++ )
{
total += array[ i ];
}
return total;
}
This may not be the best example,its just a simple explanation of a template class. This now means that any user defined type (or standard C++ type) for which the += operator is defined can be used in this class. Its very useful when dealing with things such as sorting algorithms and data structures.
If thats not clear ask again and I'll try to explain it better....