write a program that will accept two different integers, or two different floatingpoint numbers, or two different characters or two different strings then output the value that is greater between the two values entered by the user. use function overloading to solve this.
#include <iostream>
#include <string>
using namespace std;
void PrintGreater(int i, int j) {
if (i < j) {
cout << j << " is greater than " << i << endl;
}
else {
cout << i << " is greater than " << j << endl;
}
}
void PrintGreater(double x, double y) {
if (x < y) {
cout << y << " is greater than " << x << endl;
}
else {
cout << x << " is greater than " << y << endl;
}
}
void PrintGreater(char ch1, char ch2) {
if (ch1 < ch2) {
cout << ch2 << " is greater than " << ch1 << endl;
}
else {
cout << ch1 << " is greater than " << ch2 << endl;
}
}
void PrintGreater(string str1, string str2) {
if (str1 < str2) {
cout << '\"' << str2 << "\" is greater than \"" << str1 << '\"' << endl;
}
else {
cout << '\"' << str1 << "\" is greater than \"" << str2 << '\"' << endl;
}
}
int main() {
int i1, i2;
double x1, x2;
char ch1, ch2;
string str1, str2;
cout << "Enter two integer values: ";
cin >> i1 >> i2;
PrintGreater(i1, i2);
cout << endl << "Enter two double values: ";
cin >> x1 >> x2;
PrintGreater(x1, x2);
cout << endl << "Enter two chars: ";
cin >> ch1 >> ch2;
PrintGreater(ch1, ch2);
cout << endl << "Enter two strings: ";
cin >> str1 >> str2;
PrintGreater(str1, str2);
}
Comments
Leave a comment