How Can C++ Function Overloading Simplify Gravity Calculations?

In summary, C++ overloading function allows for multiple functions to have the same name but different parameters, providing flexibility and efficiency in coding. This feature is useful in creating functions that perform similar tasks on different data types, making the code more concise and readable. Overloading functions can also improve code organization and reduce the need for multiple functions with similar functionality.
  • #1
elly_55

Homework Statement


[/B]
Write a function fForceOfGravity, with return type double and with two double parameters for mass (m) and gravity (g). Parameter gravity must be initialized to 9.81. The force of gravity is given by the formula F=mg. Call these functions in the main () as follows:

cout << fForceOfGravity (75); //75 kg on Earth
cout << fForceOfGravity (75, 1.62); //75 kg on the Moon

Homework Equations


F = mg

knowledge of :
calling by reference
overloading
default arguments

The Attempt at a Solution


[/B]
Hi, I am a novice programmer trying to write a function to calculate the force of gravity on an object using a function that incorporates the concepts of calling by reference, overloading, and default arguments. I would like this function to execute from the main when called upon with specific entered values, and would like to rid my function of the output and input values required within the function. This is what I have so far, how can I move forward to achieve my goal? Thank you in advance :) !

C:
double fForceOfGravity(double & mass, double & gravity = 9.81);

{

    double y, F, temp;

    cout << "please enter the object's mass in kilograms: " << endl;

    cin >> mass >> endl;

    cout << "would you like to calculate the force of gravity on Earth (yes or no)? " << endl;

    cin >> answer >> endl;

    if answer == 'yes'

    {

        F = mass * gravity

    }

    else

    {

        cout << "please enter the acceleration due to gravity in m/s^2 :" << endl;

        cin >> y >> endl;

        temp = gravity;

        gravity = y;

        y = gravity;

        F = mass * gravity;

    }
return F
 
Physics news on Phys.org
  • #2
You are suppose to call those functions from main - with values provided by main.
So these lines of code need to be moved to main:
Code:
    cout << "please enter the object's mass in kilograms: " << endl;

    cin >> mass >> endl;

    cout << "would you like to calculate the force of gravity on Earth (yes or no)? " << endl;

    cin >> answer >> endl;
 
  • Like
Likes elly_55
  • #3
@elly_55, the code you include shows the definition for one function. The problem statement is asking for two functions, both with the same name, but with different parameter lists. One of these functions should have one parameter, and the other should have two parameters.

Also, as @.Scott notes, these functions should not ask for input, nor do output. Input and output should happen in main().
 
  • Like
Likes elly_55
  • #4
Mark44 said:
@elly_55, the code you include shows the definition for one function. The problem statement is asking for two functions, both with the same name, but with different parameter lists. One of these functions should have one parameter, and the other should have two parameters.
The problem statement:
Write a function fForceOfGravity, with return type double and with two double parameters for mass (m) and gravity (g). Parameter gravity must be initialized to 9.81. The force of gravity is given by the formula F=mg. Call these functions in the main () as follows:
The function is specified in the singular in the first case and plural in the second case. So your solution:
Code:
double fForceOfGravity(double & mass, double & gravity = 9.81);
is reasonable. It is exactly what the first sentence of the problem statement specifies and, except for the plural, it is consistent with the last sentence.
 
  • Like
Likes elly_55
  • #5
@.Scott, I was going by the thread title -- "C++ overloading function" -- and this part of the problem statement. (Underlining added by me.)
elly_55 said:
Call these functions in the main () as follows:

cout << fForceOfGravity (75); //75 kg on Earth
cout << fForceOfGravity (75, 1.62); //75 kg on the Moon
IMO, the solution shown is NOT reasonable. It has two parameters, but neither one is used in the code to pass information that the function uses.
In addition, there are numerous syntax errors, such as missing parentheses and missing semicolons at the end of statements. The code shown would not compile.
 
  • Like
Likes elly_55
  • #6
Mark44 said:
@.Scott, I was going by the thread title -- "C++ overloading function" -- and this part of the problem statement. (Underlining added by me.)

IMO, the solution shown is NOT reasonable. It has two parameters, but neither one is used in the code to pass information that the function uses.
In addition, there are numerous syntax errors, such as missing parentheses and missing semicolons at the end of statements. The code shown would not compile.
I was referring to the implementation decision to use one function, not two. I didn't mention the semicolon because it brings up what needs to be in the definition vs. declaration, I wanted to be concise, and if he didn't get it right before the compile, he certainly would have noticed in the compiler errors.
 
  • Like
Likes elly_55
  • #7
The thread title and problem statement are confusing. As I read this problem, it's not about function overloads at all, so the phrase "Call these functions ..." in the problem statement is misleading. Apparently the goal is to write a single function with two parameters, with one of them being a default parameter.

@elly_55, nearly all of the code in the function definition you posted can be removed. When the function is called, the first parameter will be the mass of the object. If the second parameter is present when the function is called, its value will be the gravitation constant. If the second parameter isn't present, the default value of 9.81 is what will be used for gravity. In both cases, the force will be the mass times the gravity value.

Also, I don't see any reason to make the parameters reference parameters (i.e., as in double & mass).
 
  • Like
Likes elly_55
  • #8
Thank you all SO much for the follow ups, this forum is the saving grace of my gpa in this class. My professor, though a very intelligent and kind individual has never taught an intro programming course before and english is his second language so I am finding myself relying on the textbook and this forum, along with our university's comp sci centre for the majority of my questions. Every pointer or critique helps!
 
  • #9
And get rid of that semicolon at the end of the fist line of the function.
Code:
double fForceOfGravity(double & mass, double & gravity = 9.81);
{
     [...]
}

You'll such a semicolon in the function's declaration, but not in its implementation. Having the semicolon in the implementation, as above, stops the body of the function from ever being executed. (Your compiler might warn you of this. However, if the function was of type void, it might not; so it's something to be careful about.)

On a different note, passing parameters by reference is a good idea if you are passing large objects. But in this case you are merely passing built-in types (namely doubles). Arguably, you might want to just pass them by value.
 

What is function overloading in C++?

Function overloading is a feature in C++ that allows multiple functions to have the same name but different parameters. This allows for more flexibility and code reusability as different versions of the same function can be used for different data types or argument lists.

How is function overloading different from function overriding?

Function overloading is when multiple functions with the same name exist within the same scope but with different parameters. Function overriding, on the other hand, is when a derived class redefines a function with the same name and parameters as a function in the base class. Function overriding is a feature of inheritance, while function overloading is not.

Can functions be overloaded based on return type?

No, functions cannot be overloaded based on return type. Function overloading is based on the function name and parameter list, so changing only the return type does not create a different function signature.

What are the benefits of function overloading?

Function overloading offers several benefits, including code reusability, improved readability, and flexibility in handling different data types and argument lists. It also allows for more efficient and concise coding, as multiple functions can be combined into one with overloaded parameters.

How does the compiler determine which function to call in function overloading?

The compiler determines which function to call based on the number, types, and order of the arguments passed to the function. It will choose the function with the closest match to the arguments provided. If there is no exact match, the compiler will try to find a conversion sequence to match the arguments to one of the overloaded functions.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
6K
Back
Top