// C++ Function template
#include <iostream>
using namespace std;
template <typename T>
T maximum(T& a, T& b)
{
return a > b ? a : b;
}
int main()
{
int m = 1, n = 2;
cout << m << '\t' << n << endl;
cout << "maximum(int, int) = " << maximum(m, n) << endl;
double x = 3.4, y = 5.6;
cout << x << '\t' << y << endl;
cout << "maximum(double, double) = " << maximum(x, y) << endl;
char A = 'A', B = 'B';
cout << A << '\t' << B << endl;
cout << "maximum(char, char) = " << maximum(A, B) << endl;
return 0;
}
Приведение к старшему типу
Код:
Код:
// C++ Function template
#include <iostream>
using namespace std;
template <typename T1, typename T2>
T1 maximum(T1 a, T2 b)
{
return a > T1(b) ? a : T1(b);
}
template <typename T1, typename T2>
T2 minimum(T1 a, T2 b)
{
return T2(a) < b ? T2(a) : b;
}
int main()
{
int m = 1, n = 2;
double x = 3.4, y = 5.6;
cout << m << '\t' << n << endl;
cout << "maximum(int, int) = " << maximum(m, n) << endl;
cout << x << '\t' << y << endl;
cout << "maximum(double, double) = " << maximum(x, y) << endl;
cout << m << '\t' << x << endl;
cout << "maximum(int, double) = " << maximum(m, x) << endl;
cout << "maximum(double, int) = " << maximum(x, m) << endl;
cout << n << '\t' << y << endl;
cout << "minimum(int, double) = " << minimum(n, y) << endl;
cout << "minimum(double, int) = " << minimum(y, n) << endl;
return 0;
}
#include <iostream>
using namespace std;
template <typename T1, typename T2>
T1 maximum(T1 a, T2 b)
{
return a > T1(b) ? a : T1(b);
}
template <typename T1, typename T2>
T2 minimum(T1 a, T2 b)
{
return T2(a) < b ? T2(a) : b;
}
int main()
{
int m = 1, n = 2;
double x = 3.4, y = 5.6;
cout << m << '\t' << n << endl;
cout << "maximum(int, int) = " << maximum(m, n) << endl;
cout << x << '\t' << y << endl;
cout << "maximum(double, double) = " << maximum(x, y) << endl;
cout << m << '\t' << x << endl;
cout << "maximum(int, double) = " << maximum(m, x) << endl;
cout << "maximum(double, int) = " << maximum(x, m) << endl;
cout << n << '\t' << y << endl;
cout << "minimum(int, double) = " << minimum(n, y) << endl;
cout << "minimum(double, int) = " << minimum(y, n) << endl;
return 0;
}
На этих примерах нужно сделать с операцией сложения или умножения(или вычитания) а результат долен приводиться к старшему типу. Подскажите как это сделать?