1
0

choices.cpp 631 B

1234567891011121314151617181920212223242526272829303132
  1. // choices.cpp -- choosing a template
  2. #include <iostream>
  3. template<class T>
  4. T lesser(T a, T b) // #1
  5. {
  6. return a < b ? a : b;
  7. }
  8. int lesser (int a, int b) // #2
  9. {
  10. a = a < 0 ? -a : a;
  11. b = b < 0 ? -b : b;
  12. return a < b ? a : b;
  13. }
  14. int main()
  15. {
  16. using namespace std;
  17. int m = 20;
  18. int n = -30;
  19. double x = 15.5;
  20. double y = 25.9;
  21. cout << lesser(m, n) << endl; // use #2
  22. cout << lesser(x, y) << endl; // use #1 with double
  23. cout << lesser<>(m, n) << endl; // use #1 with int
  24. cout << lesser<int>(x, y) << endl; // use #1 with int
  25. // cin.get();
  26. return 0;
  27. }