home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX1503.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-27  |  1.6 KB  |  51 lines

  1. // \EXAMPLES\EX1503.CPP
  2. //--------------------------------------------------------------
  3. // example template function
  4. // The Microsoft Visual C++ compiler does not support templates.
  5. // This program does not compile under Microsoft Visual C++.
  6. //--------------------------------------------------------------
  7. //--------------------------------------------------------------
  8. //  files in this example:
  9. // EX1501.CPP      this file
  10. //--------------------------------------------------------------
  11. #include <iostream.h>              // stream input and output
  12.  
  13. //--------------------------------------------------------------
  14. // template function swap()
  15. //--------------------------------------------------------------
  16. template <class T>
  17. void swap ( T& a, T& b)
  18. {  T temp;
  19.    temp = a;
  20.    a = b;
  21.    b = temp;
  22. }
  23. //--------------------------------------------------------------
  24. // a nontemplate swap function
  25. //--------------------------------------------------------------
  26.  
  27. void swap ( float& x, float& y);
  28. //-- -----------------------------------------------------------
  29. // main() - to exercise swap
  30. //--------------------------------------------------------------
  31. void main()
  32. {
  33.   float x, y;
  34.   // swapping float values
  35.   cout << "Enter 2 floating point numbers:  ";
  36.   cin >> x >> y;
  37.   cout << "These numbers in the opposite order are:  ";
  38.   swap( x, y);
  39.   cout << x << '\t' << y;
  40.   cout << endl;
  41. }
  42. //--------------------------------------------------------------
  43.  
  44. void swap ( float& x, float& y)
  45. {
  46.   cout << "(non template swap)  ";
  47.   x = x + y;
  48.   y = x - y;
  49.   x = x - y;
  50. }
  51.