home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX1503.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
- //--------------------------------------------------------------
- #include <iostream.h> // stream input and output
-
- //--------------------------------------------------------------
- // template function swap()
- //--------------------------------------------------------------
- template <class T>
- void swap ( T& a, T& b)
- { T temp;
- temp = a;
- a = b;
- b = temp;
- }
- //--------------------------------------------------------------
- // a nontemplate swap function
- //--------------------------------------------------------------
-
- void swap ( float& x, float& y);
- //-- -----------------------------------------------------------
- // main() - to exercise swap
- //--------------------------------------------------------------
- void main()
- {
- float x, y;
- // swapping float values
- 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;
- }
- //--------------------------------------------------------------
-
- void swap ( float& x, float& y)
- {
- cout << "(non template swap) ";
- x = x + y;
- y = x - y;
- x = x - y;
- }
-