How Can C++ Function Overloading Simplify Gravity Calculations?

Click For Summary

Discussion Overview

The discussion revolves around the implementation of a C++ function to calculate the force of gravity using function overloading and default parameters. Participants explore how to structure the function, the appropriate use of parameters, and the separation of input/output from the function logic.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants suggest that the function should not handle input or output, which should be managed in the main function instead.
  • Others argue that the problem statement requires two functions with the same name but different parameter lists, one with a default parameter for gravity.
  • A participant notes that the initial code contains syntax errors, such as missing parentheses and semicolons, which would prevent compilation.
  • Some participants question the necessity of passing parameters by reference, suggesting that passing by value may be more appropriate for built-in types like doubles.
  • There is a discussion about the clarity of the problem statement, with some feeling it is misleading regarding the need for function overloading.
  • A participant expresses gratitude for the feedback received, indicating reliance on the forum for assistance in their programming course.

Areas of Agreement / Disagreement

Participants generally disagree on the interpretation of the problem statement regarding function overloading and the structure of the function. There is no consensus on the best approach to implement the function as described.

Contextual Notes

Some limitations include unresolved syntax issues in the provided code, the ambiguity in the problem statement regarding the need for multiple functions, and the appropriateness of parameter passing methods.

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   Reactions: 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   Reactions: 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   Reactions: 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:

count << fForceOfGravity (75); //75 kg on Earth
count << 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   Reactions: 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   Reactions: 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   Reactions: 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 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K