Mysterious default argument.
Posted on 2003-10-27
I'm a beginner in C++, so even default argument gives me troubles! Heh!
Basically I have this little test program:
defarg(int iArg, char dArg = 'p', float fArg = 5.0)
i = iArg;
d = dArg;
f = fArg;
defarg test(5, 9.8);
cout << test.i << " " << test.d << " " << test.f;
Now, before we begin, we all know that a default argument gets used only when we don't supply our own argument right? In other words, if we supply our own argument, then the default argument is disregarded, and if we don't supply, then the default argument gets used. Am I right here or what?
Now, my problem with the above program is, I want to supply my own i, use the default d (which is 'p'), and supply my own f.
In the main method I gave my own i (5), supplied nothing for d, and gave my own f (9.8), and so I expect the program to output 5 p 9.8.
However, that's not the case. It compiles fine, but its output is either 5 [blank] 9.8 or sometimes 5 [a weird looking character] 9.8.
Can anyone help me get the output I want? Or is it impossible?