Write a program to overload the function max() to find the maximum of two ints, two chars and two doubles.

Aug 31, 2018

Program:

#include<iostream>
using namespace std;

// function prototypes
int max(int n1, int n2);
char max(char ch1, char ch2);
double max(double d1, double d2); 

int main(){
	int i1 = 100, i2 = 300;
	char c1 = 'A', c2 = 'a';
	double db1 = 34.444, db2 = 45.232;
	cout<<"Between "< n2){
		return n1;
	}else{
		return n2;
	}
}
char max(char ch1, char ch2){
	if(ch1 > ch2){
		return ch1;
	}else{
		return ch2;
	}
}
double max(double d1, double d2){
	//using ternary operator
	return d1 > d2 ? d1 : d2;
}

Sample Run:

Between 100 and 300 larger is :300
Between A and a larger is :a
Between 34.444 and 45.232 larger is :45.232

 

Related


Tools/Apps





© Nepal Exchange Rates