Don't understand this C++ exercise

S

Suzie

Hi guys,

I've been coding in C for several years and I'm getting started with
C++. I was given the following exercise to do and I don't quite
understand it. I was wondering if any of you guys could help.

#include <iostream>

using namespace std;

int main() {
cout << "hello world";
return 0;
}

Why is cout being shifted left "hello world" times?
 
D

David Lindauer

Suzie said:
Hi guys,

I've been coding in C for several years and I'm getting started with
C++. I was given the following exercise to do and I don't quite
understand it. I was wondering if any of you guys could help.

#include <iostream>

using namespace std;

int main() {
cout << "hello world";
return 0;
}

Why is cout being shifted left "hello world" times?

C++ has something called 'operator overloading' which lets you redefine
various operators in terms of defined classes. << and >> are commonly
overloaded for I/O purposes to cause output or input to a stream. In
particular the STL defines these for stream classes and basic data
types. Since cout is defined in the STL as an output stream,
specifically the console output, the line in question uses the STL
overloading to put the string "hello world" out to the console.

David
 
I

Ioannis Vranos

Suzie said:
Hi guys,

I've been coding in C for several years and I'm getting started with
C++. I was given the following exercise to do and I don't quite
understand it. I was wondering if any of you guys could help.

#include <iostream>

using namespace std;

int main() {
cout << "hello world";
return 0;
}

Why is cout being shifted left "hello world" times?



<< is both a bitwise operator and an output stream operator.




Since you have former programming experience I suggest you read
"Accelerated C++" by Andrew Koenig, Barbara Moo.


For book reviews for this and other books, check http://www.accu.org at
the book reviews section.



Also have a look to this:

http://www23.brinkster.com/noicys/learningcpp.htm
 
?

=?iso-8859-1?Q?Lin=F8nut?=

Suzie poked his little head through the XP firewall and said:
I've been coding in C for several years and I'm getting started with
C++. I was given the following exercise to do and I don't quite
understand it. I was wondering if any of you guys could help.

#include <iostream>
using namespace std;
int main() {
cout << "hello world";
return 0;
}

Why is cout being shifted left "hello world" times?

Oh goodie, I'm the first one to discover a cross-posting C++/Linux
troll!

Good thing all the hot coding goes on in comp.lang.c++.moderated.
 
T

The Ghost In The Machine

In comp.os.linux.advocacy, Suzie
<[email protected]>
wrote
Hi guys,

I've been coding in C for several years and I'm getting started with
C++. I was given the following exercise to do and I don't quite
understand it. I was wondering if any of you guys could help.

#include <iostream>

using namespace std;

int main() {
cout << "hello world";
return 0;
}

Why is cout being shifted left "hello world" times?

David Lindauer posted an excellent response to this, but
neglected to mention that the function signature you're
confused about can be written in two forms, and some
usage examples (if I'm not too far off).

cout is a std::eek:stream_withassign (I think); most will see it
as a std::eek:stream, and it can be passed around as such. The
'_withassign' I'd have to research; presumably it has to do with
overloading of the *assignment* operator.

So the first form is the one above, or, written in "longhand inline"
('using namespace std' allows one to drop the 'std::' prefix):

std::cout << "Hello, world!" << std::endl;

where std::endl is a convenient (and standard) way to end a line,
if one doesn't like to use "\n".

But there's a second, more ungainly (but more in accord with standard
C syntax) form:

operator<<( std::cout, "Hello, world!");

if I'm not mistaken. This corresponds to the function signature

std::eek:stream & operator << (std::eek:stream & stream, const char * str_ptr);

There are corresponding operators for int, double, and const void *
(void * simply prints out a hex representation of the pointer, and
is primarily for debugging use), and a fair number of others
as well. Their signatures are as expected:

std::eek:stream & operator << (std::eek:stream & stream, int intval);
std::eek:stream & operator << (std::eek:stream & stream, double doubleval);
std::eek:stream & operator << (std::eek:stream & stream, const void * arb_ptr);

or something like that; I'd have to look.

The metaphor of "shifting" is rather apt, as it turns out; the usual
shift operator

1 << 4

shifts the left value 4 bits, and returns the result (16). The

std::eek:stream & operator << (std::eek:stream & stream, const char * str_ptr);

might be said to "shift" the right operand into the stream -- such
usage is occasionally seen in contexts such as parsers, albeit in
the other direction -- and yes, there's a std::cin (or cin) and
one can use std::cin >> inputval, although not nearly as frequently.

And now, of course, one can do things such as:

std::cout << "We now have " << count << " widgets in inventory, "
<< numDamaged << " of which were damaged." << std::endl;

which prints out things like

We now have 10 widgets in inventory, 2 of which were damaged. [endline]

If one has a class, one can declare '<<' and '>>' on it, as in
the following example:

class PrisonerOfWar ...
{
public:
std::string name() const { ... }
std::string rank() const { ... }
long serialNumber() const { ... }
...
};

std::eek:stream & operator << (std::eek:stream & out, PrisonerOfWar const & val)
{
out << val.name() << " " << val.rank() << " "
<< val.serialNumber();
return out;
}

Followups reset to the C++ group; this is not a Linux-specific issue
(although there are minor differences in compilers).
 
R

red floyd

Phlip said:
We recommend you read the works of Herbert Schildt, to help overcome your
blondeness.

Schildt???? SCHILDT???? You *are* joking, aren't you? The rest of us
recommend you read the works of Andrew Koenig.
 
S

Suzie

(e-mail address removed) (Suzie) wrote:

thanks for the replies but i still don't fully understand

where is cout declared? do i have to declare it?

i heard the creator of c++ posts here. is that true? maybe he could
help. he's really good - i've read his book 'c++: the complete
reference - second edition'.

tia
 
J

Jonathan Turkanis

Suzie said:
(e-mail address removed) (Suzie) wrote:

thanks for the replies but i still don't fully understand

where is cout declared? do i have to declare it?

i heard the creator of c++ posts here. is that true? maybe he could
help. he's really good - i've read his book 'c++: the complete
reference - second edition'.

He generally only posts here when someone asks an unusually interesting question
or when someone makes an outlandishly false claim about the history of C++. You
haven't done either, yet. Keep trying.

Jonathan
 
R

red floyd

Suzie said:
(e-mail address removed) (Suzie) wrote:

thanks for the replies but i still don't fully understand

where is cout declared? do i have to declare it?

i heard the creator of c++ posts here. is that true? maybe he could
help. he's really good - i've read his book 'c++: the complete
reference - second edition'.

tia

No, Dr. Stroustrup did not write "C++: The Complete REference". He
wrote "The C++ Programming Language".

cout is declared in a standard header. If you #include <iostream>, then
you will have the declaration. It is either declared in <iostream> or
some other file included by iostream.
 
M

Mike Austin

Jonathan Turkanis said:
He generally only posts here when someone asks an unusually interesting question
or when someone makes an outlandishly false claim about the history of C++. You
haven't done either, yet. Keep trying.

It's declared in your rectum, wintroll. :)
 
P

Phlip

red said:
Suzie wrote:

No, Dr. Stroustrup did not write "C++: The Complete REference". He
wrote "The C++ Programming Language".

And H. Schildt didn't invent C++, either. ;-)
cout is declared in a standard header. If you #include <iostream>, then
you will have the declaration. It is either declared in <iostream> or
some other file included by iostream.

"Suzie" has struck again! Woo hoo you go "girl"!

Watch her get you to write what "void" means next.
 
P

Phlip

red said:
Schildt???? SCHILDT???? You *are* joking, aren't you? The rest of us
recommend you read the works of Andrew Koenig.

Have you ever actually read a Schildt book? or are you just going with the
opinion that has been rolling around the C++ newsgroup for the last decade?

I have read one, and therefor _I_ am allowed to report he sucks. He has a
warm and friendly style, at the expense of depth and accuracy. (Writing a
programming book is _really_ hard when you must push in many paragraphs that
a colleague, if you were speaking to them, would insist you shut up about.)

Now please review Suzie's posting history here, and her @yahoo.com account
name. Take a deep breath, and return to my post above.
 
Z

Zeng Dinghao

The operator '<<' have anothor meaning in this context, which is different
from 'shifting left'(the original meaning of '<<'). That is to say, '<<' has
been 'overloaded'(C++ term). I suggest you refer to some basis C++ books. I
recommend 'thinking in c++' strongly since you have had some experience with
C.
 
R

red floyd

Phlip said:
red floyd wrote:




Have you ever actually read a Schildt book? or are you just going with the
opinion that has been rolling around the C++ newsgroup for the last decade?

Yes. I had the misfortune to own both of his C++ books (The Complete
Reference, and the Pocket Reference).
 
D

Donn Miller

Phlip said:
red floyd wrote:




Have you ever actually read a Schildt book? or are you just going with the
opinion that has been rolling around the C++ newsgroup for the last decade?

I have one of his books here, "C, The Complete Reference", fourth
edition. I've heard the fourth edition isn't as bad as the others. I
know he does write from the perspective of a Windows programmer. Still
that shouldn't matter for C programming.
I have read one, and therefor _I_ am allowed to report he sucks. He has a
warm and friendly style, at the expense of depth and accuracy. (Writing a
programming book is _really_ hard when you must push in many paragraphs that
a colleague, if you were speaking to them, would insist you shut up about.)

I'd think that Marshall Brain would be very good at writing a book in a
warm and friendly style that doesn't compromise much in depth and
accuracy. Also, anyone have any books written by Dale Nell? If so, how
are they?
 
B

Brad Herald

more specifically, cout is part of a class "iostream" which accesses streams
to the OS. The << is an operator which sends the string (like a char* array)
to the cout stream.

I'm not too familiar with C, but what you wrote 'cout << "hello world;' is
much like 'write(cout, 'hello world');'

The coolness of C++ is classes. They are like 'struct' but much more
powerful and user definable.

Brad
 
T

The Ghost In The Machine

In comp.os.linux.advocacy, Brad Herald
<[email protected]>
wrote
more specifically, cout is part of a class "iostream" which accesses streams
to the OS. The << is an operator which sends the string (like a char* array)
to the cout stream.

I'm not too familiar with C, but what you wrote 'cout << "hello world;' is
much like 'write(cout, 'hello world');'

Pedant point: fprintf(stdout, "Hello world") or simply printf("Hello World").
However, you're more or less right, although the actual declaration
of the routine is

std::eek:stream & operator<< (std::eek:stream &, const char *);

or similar. I don't know where the actual declaration lives.
The coolness of C++ is classes. They are like 'struct' but much more
powerful and user definable.

And they allow for some neat tricks -- some of which could do
nasty things to one's pointers if one's not careful. But
there are some nice overloads one can do; for example,
one can define:

class Matrix { ... };

Matrix operator + (Matrix const &, Matrix const &);
Matrix operator - (Matrix const &, Matrix const &);
Matrix operator * (Matrix const &, Matrix const &);
Matrix operator * (Matrix const &, double);
Matrix operator / (Matrix const &, double);
Matrix operator * (double, Matrix const &);
Matrix operator / (double, Matrix const &); /* 1/m = inverse of m */

and have a somewhat rudimentary approximation to a matrix algebra.

(The 'const &' is primarily for efficiency.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top