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

  1. // \EXAMPLES\EX1501.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. // %F,15,EX1501.H%EX1501.H         definition of String class
  11.  
  12. //--------------------------------------------------------------
  13. #include <iostream.h>              // stream input and output
  14. #include "EX1501.H"                // definition of string class
  15.  
  16. //--------------------------------------------------------------
  17. // template function swap()
  18. //--------------------------------------------------------------
  19. template <class T>
  20. void swap ( T& a, T& b)
  21. {  T temp;
  22.    temp = a;
  23.    a = b;
  24.    b = temp;
  25. };
  26.  
  27. //--------------------------------------------------------------
  28. // main() - to exercise swap
  29. //--------------------------------------------------------------
  30. void main()
  31. {  int i, j;
  32.    float x, y;
  33.    char buffer[80];                    // for Strings
  34.    // swapping integers
  35.    cout << "Enter 2 integers:  ";
  36.    cin >> i >> j;
  37.    cout << "These numbers in the opposite order are:  ";
  38.    swap( i, j);
  39.    cout << i << '\t' << j;
  40.    cout << endl;
  41.    // swapping floats
  42.    cout << "Enter 2 floating point numbers:  ";
  43.    cin >> x >> y;
  44.    cout << "These numbers in the opposite order are:  ";
  45.    swap( x, y);
  46.    cout << x << '\t' << y;
  47.    cout << endl;
  48.    // swapping strings
  49.    cout << "Enter 2 words:  ";
  50.    cin >> buffer;
  51.    String m(buffer);
  52.    cin >> buffer;
  53.    String n(buffer);
  54.    cout << "These words in the opposide order are:  ";
  55.    swap( m, n);
  56.    cout << m << '\t' << n;
  57.    cout << endl;
  58. }
  59. //--------------------------------------------------------------
  60.