How to write generic max function to get max from different type numbers in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

template <class T>
T max(T a, T b) {
	return a > b ? a : b;
}

int main()
{
	int a = 7, b = 3, c;
	c = max<int>(a, b);
	cout << c << endl;

	double x = 3.14, y = 2.16, z;
	z = max<double>(x, y);
	cout << z << endl;

	return 0;
}


/*
run:

7
3.14

*/

 



answered Aug 1, 2018 by avibootz
edited Aug 1, 2018 by avibootz
...