Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 10th, 2012  |  syntax: None  |  size: 0.88 KB  |  hits: 3  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. working with templates in c   calculator
  2. template <class A, class B >
  3. A Sum(A Fnum, B Snum){
  4. return Fnum + Snum;
  5. }
  6.  
  7. template <class S, class W>
  8. S Subtraction(S Fnum, W Snum){
  9. return Fnum - Snum;
  10. }
  11. template <class M, class E>
  12. M Multiplication(M Fnum, E Snum){
  13. return Fnum * Snum;
  14.  }
  15.   template <class D, class C>
  16.  D Division(D Fnum, C Snum){
  17. return Fnum / Snum;
  18. }
  19.        
  20. T&  operator+( T number );
  21.        
  22. float, int
  23.        
  24. #include<iostream>
  25.  
  26. using namespace std;
  27.  
  28. struct Add {
  29. template < typename A, typename B > A operator() (const A & lhs, const B & rhs) { //note return type is A , what if A, B are diff? , you need promote one of them using specialization
  30. return lhs + rhs;
  31. }
  32. };
  33.  
  34. //some other ops like above
  35.  
  36. template < typename op, class A, class B > A operate(A Fnum, B Snum)
  37. {
  38. op oper;
  39. return oper(Fnum, Snum);
  40. }
  41.  
  42. int main()
  43. {
  44. int a = 20, b = 30;
  45. std::cout << operate < Add > (a, b) << std::endl;
  46. }