Question #224819

Create a generic function max that gives the maximum value of three generic type arguments that are passed to it. Then test this function by calling it with char, int, and float type.


Expert's answer

#include<iostream>
using namespace std;
template <typename P>
P Max(P a, P b, P c){
	P  max = a;
	if (b > a && b > c){
		max = b;
	}
	else if(c>a && c>b){
	max = c;
	}
	return max;
}
int main(){
	int a = 6, b=8, c=2;
	char first = 'a', second = 'b', third='d';
	float beginner = 12.8849, guru = 9.09283, coder = 8.98893;
	cout<<"The greatest integer is:  "<<Max(a,b,c)<<endl; //Testing generic function with integers
	cout<<"The maximum character is:  "<<Max(first,second, third)<<endl;//Testing generic function with charactares
	cout<<"The maximum float is:   "<<Max(beginner, guru, coder);////Testing generic function with float numbers
}

LATEST TUTORIALS
APPROVED BY CLIENTS