I don't exactly understand this "display_it" command. I get that it's used as "display_it(double,int)" but I don't understand its purpose. I got it from part of a homework assignment...
1) An example of a void function is a function that takes a real value and displays it with a specific number of decimal points.
void display_it(double x, int precision)
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(precision);
cout << x << endl;
}
Use overloading to write a function that displays two real values with a specific precision.
Turn in the program and its output.
Well I'm using dev, so I had to change it a little because it wouldn't compile...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
double x;
void display_it(double x=3.14159265358979323846, int precision);{
cout.precision(3);
cout<<x<<endl;
}
system("PAUSE");
return 0;
}
and I mean it does what it's supposed to do (I think >.>) but I don't get what this display_it thing is. I tried googling to no avail, and doing a search here on cplusplus.com didn't give me any results either. So can someone explain it to me?
I bet you won't find anything about the term "display_it" no matter where you look... It's a user defined function, YOU are the one supposed to create that function! And I can see here that your teacher gave you a lot of help by giving you the definition. All you have to do is declare it in your program and define it using the code he gave you! Now, I use Dev myself and the piece of code you threw here won't compile (I could tell that by just looking at it :D but I tried to compile it just in case Dev accidentally compiles it due to some very weird bug...). Since your homework is about functions, don't you think that, at least, you should read how to declare, define and call one??? I ll do you a favor this time, but not the next! And be sure that no one else will!
When your teacher says
"Use overloading to write a function that displays two real values with a specific precision."
he simply means to declare and define 2 functions, with the same name ("display_it ") but with different arguments. Then, when u use the common name to call one of them, the compiler will decide which one to call depending on the number and type of the arguments u give. Let's see how this is done: