I understand the general concept of function overloading but I am at a loss right now. Let me provide you with the background information and where I am confused.
I am in a c++ class in college and am a CS major. Our professor wants us to use function overloading for this assignment.
The part of the assignment where I am confused is as following:
The assignment is to compare two pizzas where one pizza might be circular and one might be rectangular. we are supposed to calculate which pizza is the better buy
We are to create an overload function that accepts the width and length of a rectangular pizza. what I don't get is how i am supposed to distinguish between if its the width of the first or second length since it is the same data type for both. We are supposed to ask the user for the length and width of the (1st) pizza if it was the 1st pizza or (2nd) if it was the second pizza. I understand all the syntax but the logic has really got me on this one. Personally I would create two different functions. But he wants an overload function for this one to accept the diameter and and I'm assuming pi (which i made a float and the rest of the variables doubles). The variables are already set in his starter code.
Sorry to be redundant but again, the part I'm confused about is how to return the respected widths and lengths using these overload functions.
I started passing by reference but quickly realized that I cant make an overload function out of this because he has all these values set as doubles. I feel like I need to pass 4 values in by reference and create an if statement inside of this function.
I hope this was not too confusing and I appreciate any help.
Both functions have the same name and return the same thing, but do something different. If you call pizzaSize with only one argument it will use the first function and calculate the area based on the radius, otherwise it will calculate the area based on length and width.
oh that makes a lot more since thank you. My other question thought is how i would distinguish the difference between the 1st and 2nd pizza in both cases. I have variables length1, length2, width1, width2, diameter1, and diameter 2. I can only return one value with a nonvoid function and the function needs to also ask the user for either the dimensions of the 1st or second pizza. Does that make sense?
So you could call the pizzaSize function with the appropriate number of parameters (as Yay295 illustrated above) by reference to collect the radius or length/width.
Then you'd calculate the price of their selected pizza based on some set price guide, is it per square inch? I'm trying to understand how you're supposed to compare the price of a second pizza.
yes that helped a lot. What i did was did exactly as you said and just made a third parameter called whichPizza. Then made an if statement to determine which pizza to ask the user for. I think I got it all figured out logically. Thank you so much for the help.