NOTE: PLEASE READ CAREFULLY AND DO WHATEVER THEY ASKING. THANK YOU. Lab 8 Overlo
ID: 3621085 • Letter: N
Question
NOTE: PLEASE READ CAREFULLY AND DO WHATEVER THEY ASKING. THANK YOU.Lab 8
Overloading Functions
In C++, you can write multiple functions with the same name provided the parameters are different. A good example of overloading is the division operator. If both operands are integers, integer division occurs. If at least one of the parameters is a floating point number, floating point division occurs.
In functions this can be nice. Let us say we want a function called max() which will return the larger of two numbers. Since parameters for functions must have a particular data type assigned with them (int, float, double, etc.), a function to find the larger integer value could not be used to find the larger float value. If we could not overload functions, we would have to name one something like "maxInt()" and other "maxFloat()". With overloading, we would still need to write multiple functions, but they can all have the same name. Thus when we want to use this function, we just have to remember it is called "max()" no matter what data type is used.
We can also write a function max() to return the larger of two numbers and another function max() to return the largest of three numbers and another function max() to return the largest of four numbers....
Lab Assignment
Due: before your lab of October 26th.
Create a C++ program that will overload the function max() to allow it to return the largest of three, four, or five numbers. You should have functions for numbers that are integers and different functions for numbers that are floating point. Write a main that prompts the user for whether they want to use integer or floating point numbers, then prompt for the number of numbers, then get the numbers, call the appropriate function, and return the appropriate maximum.
All of the functions should be max(), but they will have a differing number and type of parameters as well as different return types.
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
int max(int,int,int);
int max(int,int,int,int);
int max(int,int,int,int,int);
float max(float,float,float);
float max(float,float,float,float);
float max(float,float,float,float,float);
int main()
{int type,num,a,b,c,d,e,maxInt;
float f,g,h,i,j,maxFloat;
cout<<"Enter 1 for integers, 2 for floating point: ";
cin>>type;
while(type<1||type>2)
{cout<<"Invalid entry ";
cout<<"Enter 1 for integers, 2 for floating point: ";
cin>>type;
}
cout<<"Enter number of numbers between 3 and 5: ";
cin>>num;
while(num<3||num>5)
{cout<<"Invalid entry ";
cout<<"Enter number of numbers between 3 and 5: ";
cin>>num;
}
if(type==1)
{cout<<"Enter number 1: ";
cin>>a;
cout<<"Enter number 2: ";
cin>>b;
cout<<"Enter number 3: ";
cin>>c;
if(num==3)
maxInt=max(a,b,c);
else if(num==4)
{cout<<"Enter number 4: ";
cin>>d;
maxInt=max(a,b,c,d);
}
else
{cout<<"Enter number 4: ";
cin>>d;
cout<<"Enter number 5: ";
cin>>e;
maxInt=max(a,b,c,d,e);
}
cout<<"The largest number is "<<maxInt<<endl;
}
else
{cout<<"Enter number 1: ";
cin>>f;
cout<<"Enter number 2: ";
cin>>g;
cout<<"Enter number 3: ";
cin>>h;
if(num==3)
maxFloat=max(f,g,h);
else if(num==4)
{cout<<"Enter number 4: ";
cin>>i;
maxFloat=max(f,g,h,i);
}
else
{cout<<"Enter number 4: ";
cin>>i;
cout<<"Enter number 5: ";
cin>>j;
maxFloat=max(f,g,h,i,j);
}
cout<<"The largest number is "<<maxFloat<<endl;
}
system("pause");
return 0;
}
int max(int a,int b,int c,int d,int e)
{int max;
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
if(d>max)
max=d;
if(e>max)
max=e;
return max;
}
int max(int a,int b,int c,int d)
{int max;
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
if(d>max)
max=d;
return max;
}
int max(int a,int b,int c)
{int max;
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
return max;
}
float max(float a,float b,float c,float d ,float e)
{float max;
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
if(d>max)
max=d;
if(e>max)
max=e;
return max;
}
float max(float a,float b,float c,float d)
{float max;
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
if(d>max)
max=d;
return max;
}
float max(float a,float b,float c)
{float max;
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
return max;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.