Mail Archives: djgpp/2000/02/15/21:44:15
Harry Hiratos <harrydhh AT ozemail DOT com DOT au> wrote:
> I have written a program which includes the following lines:
> Employee Sarah;
> cout << "Her salary is : " << Sarah.getMessage();
> where Sarah is an object of class Employee. The function getMessage is
> defined inline as follows:
> char getMessage() { puts( message ); }
At the very base of your problem, this routine is incorrect. It does
not do what its name and usage suggests it will. A routine to be used
in the way you use 'Sara.getMessage()' above would have to return a
string or char *, which in turn contains the message string. It should
definitely *not* generate any direct output, itself. I.e. the correct
definition of 'getMessage()' would probably have been:
char *getMessage() { return message; }
> When I run this program, it prints the words "Her salary is : " on the line
> immediately after the output of getMessage, instead of before it on the same
> line.
That's a second-order consequence. puts() and cout come from two
separate families of output functions, that don't mix well. But as I
said, the real problem is that getMessage shouldn't have called puts()
at all, in the first place.
--
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.
- Raw text -