home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX1501.CPP
- //--------------------------------------------------------------
- // Example template function
- // The Microsoft Visual C++ compiler does not support templates.
- // This program does not compile under Microsoft Visual C++.
- //--------------------------------------------------------------
-
- // files in this example:
- // EX1501.CPP this file
- // %F,15,EX1501.H%EX1501.H definition of String class
-
- //--------------------------------------------------------------
- #include <iostream.h> // stream input and output
- #include "EX1501.H" // definition of string class
-
- //--------------------------------------------------------------
- // template function swap()
- //--------------------------------------------------------------
- template <class T>
- void swap ( T& a, T& b)
- { T temp;
- temp = a;
- a = b;
- b = temp;
- };
-
- //--------------------------------------------------------------
- // main() - to exercise swap
- //--------------------------------------------------------------
- void main()
- { int i, j;
- float x, y;
- char buffer[80]; // for Strings
- // swapping integers
- cout << "Enter 2 integers: ";
- cin >> i >> j;
- cout << "These numbers in the opposite order are: ";
- swap( i, j);
- cout << i << '\t' << j;
- cout << endl;
- // swapping floats
- cout << "Enter 2 floating point numbers: ";
- cin >> x >> y;
- cout << "These numbers in the opposite order are: ";
- swap( x, y);
- cout << x << '\t' << y;
- cout << endl;
- // swapping strings
- cout << "Enter 2 words: ";
- cin >> buffer;
- String m(buffer);
- cin >> buffer;
- String n(buffer);
- cout << "These words in the opposide order are: ";
- swap( m, n);
- cout << m << '\t' << n;
- cout << endl;
- }
- //--------------------------------------------------------------
-