Answer to Question #265403 in C++ for Bulala

Question #265403

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



1
Expert's answer
2021-11-13T12:40:54-0500
#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();
   
  }
  
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS