Overloading I loverloadl.cpp overloaded function 2 Sinclude kiostream> using nam
ID: 3810098 • Letter: O
Question
Overloading I loverloadl.cpp overloaded function 2 Sinclude kiostream> using namespace std 4 prototypes declare two functions with In C++ two different functions S //same names s dirferent perane ter types int operate (int a, int b); can have the same name if 7 float operate (float a float b): 8 int main their parameter types or 10 //declare initialize variables number are different. 11 int x 5, 2 float n 5.0, m 2.0: That means that you can give 13 //use float operate to print 14 cout operate (x,y) l cout "In the same name to more than 15 16 //use int operate to print one function if they have cout operate 18 cout "In either a different number of return 20 21 this function has integer paraneters parameters or different types 22 and returns an integer 23 int operate (int a, int b) in their parameters. 25 return (a b) This is called Overloading 27 //this function has float parameters 28 //and returns a floting point number 29 float operate (float a float b) 30 31 return (a/b) 32 L Programming Logic and Design, Seventh Edition 19Explanation / Answer
PROGRAM CODE:
overloaded2.cpp
#include <iostream>
using namespace std;
int operate(int a, int b);
float operate(int a, int b);
int main() {
int x = 5, y=2;
float n = 5.0, m=2.0;
cout<<operate(x,y);
cout<<" ";
cout<<operate(n,m);
cout<<" ";
return 0;
}
int operate(int a, int b)
{
return a*b;
}
float operate(int a, int b)
{
return (a/b);
}
OUTPUT:
There is compiler error that says
overload3.cpp
#include <iostream>
using namespace std;
int operate(int a, int b);
float operate(float a, int b);
int main() {
int x = 5, y=2;
float n = 5.0, m=2.0;
cout<<operate(x,y);
cout<<" ";
cout<<operate(n,y);
cout<<" ";
return 0;
}
int operate(int a, int b)
{
return a*b;
}
float operate(float a, int b)
{
return (a/b);
}
OUTPUT:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.