An overloaded function could be like this. You could have two functions with the same name but which take different parameters. For example, you could have a function called "calculate_balance" that takes no parameters and a function called "calculate_balance" that takes an integer, for example. Something like this:
void calculate_balance (); // takes noparameters
void calculate_balance (int number); // takes an integer
The program knows which function to call by the parameters it is passed. So something like this:
#include <iostream>
using namespace std;
void calculate_balance (); // takes no parameters
void calculate_balance (int number); // takes an integer
int main ()
{
calculate_balance (); // will call the first function
calculate_balance (5); // will call the second function
return 0;
}
void calculate_balance ()
{
cout << "I am in calculate_balance. I was passed no parameters" << endl;
}
void calculate_balance (int number)
{
cout << "I am in calculate_balance. I was passed a " << number << endl;
}
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
For a simple mathematical equation (in other words, something that can be done in one or two lines), why do you need a function?
To overload a function you have to have at least one of the followinga different number of parameters
at least one corresponding parameter of different types
(The result type doesn't make a difference.)
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
The only way I can think of overloading a function that takes two doubles as parameters like that and overload them so they are different is something like this:
double calculate_balance (double, double);
double calculate_balance (double*, double);
double calculate_balance (double, double*);
double calculate_balance (double*, double*);
That's four different functions, all taking two doubles and returning a double. They are slightly different in that some of the parameters are double and some are pointers to doubles. Either that or something kind of silly like this:
double calculate_balance (double, double);
double calculate_balance (double, double, bool);
double calculate_balance (double, double, bool, bool);
double calculate_balance (double, double, int);
There you are adding meaningless parameters to the end that you don't need or use, but are just passing them in order to overload the function.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
That would work, but it is not recommended.
The purpose of overloading a function is to allow the same operation to be done on different data.
For example, if you want to print a string you might have a couple of overloaded functions:
void print( const char *s ); void print( const std::string s );
This allows you to account for the different ways in which you may think of a "string".
However, no matter how the string is represented the underlying operation is the same: the string is printed. That is to say that the purpose of overloading a function isnot to give functions that do different things the same name. The functions may have to accomplish their goal differently, because the data given them are different, but the end result is the same: something gets printed.
In your case, then, I don't think you ought to overload anything. You are doing four distinct things: 1) depositing to checking, 2) withdrawing from checking, 3) depositing to savings, 4) withdrawing from savings. Since you are doing four things, make four functions.
Given that the only variance between depositing and withdrawing is the sign of the amount of money added, you could combine the checking functions into one, and the saving functions into another:
double calculate_checking_balance( double capital, double principal ); double calculate_savings_balance( double capital, double principal );
So to deposit $12.00 into savings, you would say: savings = calculate_savings_balance( savings, 12.00 );
And to withdraw $21.99 from checking, you would say: checking = calculate_checking_balance( checking, -21.99 );
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
Show your professor my post, ask him what we have misunderstood about your assignment, and post back with the response. :yawn:
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
Since adding an interest rate to SOMETHING is a similar task for both checking and savings, it could be presented as an overloaded function. Especially if there were conditions placed on the interest vs. balance in the account, would make sense to do it that way.
The simplest invocation might be :
double total(savings, rate)
{
total = savings + (savings * rate);
return total;
}
double total(checking, rate)
{
total=checking + (checking * rate);
return total;
}
(I just did this to illustrate a point, not as "plug-in "code)
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
Yes, I know that. But that doesn't overload the function, it just adds another parameter.
double total( double captial, double adjust, double rate ) {
return capital +adjust +(capital *rate);
}
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
> If everything is declared the same - as double - how do I make the parameters different?
don't declare everything the same; create different types
your code will also be more typesafe.
#include <iostream>
struct temperature
{
double value ;
inline temperature( double t ) : value(t) {}
inline operator double() const { return value ; }
};
struct pressure
{
double value ;
inline pressure( double t ) : value(t) {}
inline operator double() const { return value ; }
};
void process_value_changed( temperature old_value, temperature new_value )
{
std::cout << "temperature changed from " << old_value
<< " to " << new_value << '\n' ;
}
void process_value_changed( pressure old_value, pressure new_value )
{
std::cout << "pressure changed from " << old_value
<< " to " << new_value << '\n' ;
}
int main()
{
temperature t1= 26.7, t2 = 20.45 ;
pressure p1 = 756.72, p2 = 761.04 ;
process_value_changed( t1, t2 ) ;
process_value_changed( p1, p2 ) ;
// t1 = p2 ; // error; can't assign pressure to temperature
t1 = double(p2) ; // ok; via explicit cast
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
OK, JRM, you've completely lost me. The link you gave gives the exact definition of an overloaded function that I've been giving throughout this whole thread.
Calling the same function with different variables is not overloading.
There must be a difference in type or number of static arguments to overload more than one function to have the same name.
Have I misunderstood what you are trying to say?
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
OK, JRM, you've completely lost me. The link you gave gives the exact definition of an overloaded function that I've been giving throughout this whole thread.
Calling the same function with different variables is not overloading.
There must be a difference in type or number of static arguments to overload more than one function to have the same name.
Have I misunderstood what you are trying to say?
So, you are saying since both checking and savings are doubles passed by value, the function is not overloaded? Then what would be the definition of double total(savings, rate)?
( not a rhetorical question)
Now that I think about this a bit more, I understand that the compiler could not distinguish between the two variables, as they are both doubles.
OK, so make one a reference and the other a value-DONE.
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
Exactly. However, making one a reference and one a value doesn't help. If I were to say:
double x, y;
x = foo( y );
How would the complier choose between double foo( double );
and double foo( double & );
?
More infuriatingly, even if I were to say:
double x;
x = foo( 12.0 );
the compiler would give me a hard time about choosing one. (The literal could be promoted to a temporary!)
This is because resolving between overloaded functions is not academic. The C++ standard is notoriously wishy-washy about this stuff (though, that can't really be helped due to implicit conversions and type promotions). The basic way it is done is to choose based on the least-bad worst-choice.
If you want more reading, here're a couple short, easy-to-read articles I just googled that touch on this very problem, including trying to differentiate based on value or reference. C++ Week 20: Function Overload Resolution
ACCU: Overload Resolution
(I learned some cool stuff reading them myself.)
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
Thanks.
The number of little C++ "gotchas" just boggles the mind!
Reading that article brought to mind a poker game where one hand beats another...
;-)
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75