Write a program to find the biggest from two variables using template.
#include<iostream> using namespace std; template <typename T1,typename T2> T1 bigger(T1&,T2&); int main() { int i1 = 45,i2 = 54; char c1 = 'A',c2 = 'a'; float f1 = 5.7,f2 = 7.5; cout << "max = " << bigger(i1,i2) << endl; cout << "max = " << bigger(c1,c2) << endl; cout << "max = " << bigger(f1,f2) << endl; cout << "max = " << bigger(i1,c2) << endl; cout << "max = " << bigger(c1,f2) << endl; cout << "max = " << bigger(f1,c2) << endl; cout << "max = " << bigger(f2,i1) << endl; return 0; } template <typename T1,typename T2> T1 bigger(T1 &x,T2 &y) { cout << "v1 = " << x << " v2 = " << y << " "; if(x == y) return 0; else if(x > y) return x; else return y; }
Output
v1 = 45 v2 = 54 max = 54 v1 = A v2 = a max = a v1 = 5.7 v2 = 7.5 max = 7.5 v1 = 45 v2 = a max = 97 v1 = A v2 = 7.5 max = A v1 = 5.7 v2 = a max = 97 v1 = 7.5 v2 = 45 max = 45