1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <iostream>
using namespace std;
template <typename T> //or "class T"
void display(T x, T y, int n=1);
template <typename T>
void swap(T x, T y);
int main()
{
int a = 10, b = 20;
double x = 50, y = 1000;
display(a, b);
swap(a, b);
display(a, b);
display(x, y, 2);
swap(x, y);
display(x, y, 5);
while (cin.get() == '\n')
continue;
}
template <typename T>
void display(T x, T y, int n)
{
if (n == 1)
cout << "a = " << x
<< "\nb = " << y << "\n\n";
else
cout << "x = " << x
<< "\ny = " << y << "\n\n";
}
template <typename T>
void swap(T x, T y)
{
T temp;
temp = x;
x = y;
y = temp;
}
|