Comp Sci How Can C++ Function Overloading Simplify Gravity Calculations?

AI Thread Summary
The discussion focuses on creating a C++ function, fForceOfGravity, to calculate gravitational force using the formula F=mg, with mass and gravity as parameters. The function should be designed to accept two parameters, where gravity defaults to 9.81, and should not handle input/output directly; this should occur in the main function instead. Participants point out that the initial code contains syntax errors and does not properly implement function overloading, as it attempts to use one function instead of two distinct ones. The consensus emphasizes that the function should be simplified to just perform calculations based on the parameters passed from main. Proper implementation will enhance clarity and functionality in the program.
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
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
@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
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
@.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
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
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
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!
 
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.
 

Similar threads

Replies
3
Views
1K
Replies
2
Views
3K
Replies
23
Views
3K
Replies
9
Views
2K
Replies
6
Views
3K
Replies
13
Views
2K
Replies
5
Views
2K
Replies
14
Views
5K
Replies
3
Views
2K
Back
Top