How can I get a program like this using Function Overloading in c++?
Example 1:
Enter
A - for comparing integers
B - for comparing float
C - for comparing characters
D - for comparing strings
Enter your choice: a
Enter two integers: 5 10
10 is greater
Example2:
Enter
A - for comparing integers
B - for comparing float
C - for comparing characters
D - for comparing strings
Enter your choice: d
Enter two integers: hello Hi
hello is greater
#include <iostream>
using namespace std;
void printI(){//friend function
int a,b;
cout<<"Enter two intergers: ";
cin>>a>>b;
if(a>b){
cout<<a<<" is greater";
}
else{
cout<<b<<" is greater";
}
}
void printF(){
float m,n;
cout<<"Enter two float numbers: ";
cin>>m,n;
if(m>n){
cout<<m<<" is greater";
}
else{
cout<<n<<" is greater";
}
}
void printC() {
string x,y;
cout<<"Enter two strings: ";
cin>>x>>y;
if(x>y){
cout<<y<<" is greater";
}
else{
cout<<x<<" is greater";
}
}
void printD(){
char o,p;
cout<<"Enter two characters: ";
cin>>o>>p;
if(o>p){
cout<<p<<" is greater";
}
else{
cout<<o<<" is greater";
}
}
int main() {
char option;
cout<<"A - for comparing integers\nB - for comparing float\nC - for comparing characters\nD - for comparing strings";
cout<<"\nEnter your option: ";
cin>>option;
if(option=='B'||option=='b'){
printF();
}
else if(option=='A'||option=='a'){
printI();
}
else if(option=='C'||option=='c'){
printD();
}
else if(option=='D'||option=='d'){
printC();
}
}
Comments
Leave a comment