Page
Specify the class finder with following auerloaded
functious :
i) int findMax (int n1, int n2)- to decide and return largest from n1, n2.If both the numbers are the same then return 0.
ii) int findMax (int n1, int n2, int n3) - to decide and
return largest from n1, n2, n3. If all the numbers are same then return 0.
Write main() function to input three integers x, y and z, and by invoking above functions
and also from
print largest from x, y and also from x, y, z. Use function overloading concept.
Answers
Answer:
#include <iostream>
using namespace std;
int findMax(int x,int y){
return x==y?0:max(x,y);
}
int findMax(int x,int y,int z){
if(x==y&&x==z) return 0;
if(x>=y&&x>=z) return x;
if(y>=x&&y>=z) return y;
if(z>=x&&z>=y) return z;
}
int main()
{
int x,y,z;
cout<<"Enter 3 integers:"<<endl;
cin>>x>>y>>z;
int ans2=findMax(x,y);
int ans3=findMax(x,y,z);
cout<<"Max of 2 numbers: "<<ans2<<endl;
cout<<"Max of 3 numbers: "<<ans3<<endl;
cout << "Hello World" << endl;
return 0;
}
Explanation:
You don't have to do anything extra to do overloading, just write two different functions with same names but different arguments(arguments are the x,y and z being passed here) here you get the maximum of 2 numbers as well as 3 numbers with the functions of same name.