Find   


An Affiliate of AIHorizon




This snippet submitted by prog-bman on 2005-01-26. It has been viewed 17685 times.
Rating of 3.0319 with 188 votes

Template Calculator

#include <iostream>

//A simple calculator using templates


template <class T>
T add(T a, T b)
{
  T result;
  result = a + b;
  return result;
}

template <class T>
T subtract(T a, T b)
{
  T result;
  result = a - b;
  return result;
}

template <class T>
T divide(T a, T b)
{
  T result;
  result = a / b;
  return result;
}

template <class T>
T multiply(T a, T b)
{
  T result;
  result = a * b;
  return result;
}

int main(void)
{
  
  std::cout<<add<int>(10,20)<<std::endl;
  std::cout<<subtract<float>(5.4,2.3)<<std::endl;
  std::cout<<multiply<int>(4,5)<<std::endl;
  std::cout<<divide<int>(9,3)<<std::endl;
  std::cin.get();
  
  return 0;
}  
 




More snippets


Add a snippet!




-----
Interested in advertising with us?
Please read our privacy policy.
Copyright © 1997-2005 Cprogramming.com. All rights reserved.