473,385 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Don't understand this C++ exercise

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?
Jul 22 '05 #1
22 1608
Suzie wintroll wrote:
Hi guys,
Hi wintroll.
cout << "hello world"; Why is cout being shifted left "hello world" times?


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

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #2


Suzie 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?


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
Jul 22 '05 #3
Suzie 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?


<< 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

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #4
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.

--
Penguins love icebergs.
Jul 22 '05 #5
In comp.os.linux.advocacy, Suzie
<wi******@yahoo.com>
wrote
on 22 Oct 2004 22:41:21 -0700
<ec**************************@posting.google.com >:
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::ostream_withassign (I think); most will see it
as a std::ostream, 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::ostream & operator << (std::ostream & 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::ostream & operator << (std::ostream & stream, int intval);
std::ostream & operator << (std::ostream & stream, double doubleval);
std::ostream & operator << (std::ostream & 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::ostream & operator << (std::ostream & 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::ostream & operator << (std::ostream & 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).

--
#191, ew****@earthlink.net
It's still legal to go .sigless.
Jul 22 '05 #6
Phlip wrote:
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.

Jul 22 '05 #7
wi******@yahoo.com (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
Jul 22 '05 #8

"Suzie" <wi******@yahoo.com> wrote in message
news:ec**************************@posting.google.c om...
wi******@yahoo.com (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

Jul 22 '05 #9
Suzie wrote:
wi******@yahoo.com (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.

Jul 22 '05 #10
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:2u*************@uni-berlin.de...

"Suzie" <wi******@yahoo.com> wrote in message
news:ec**************************@posting.google.c om...
wi******@yahoo.com (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.


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

Jul 22 '05 #11
red floyd wrote:
Suzie wrote:
wi******@yahoo.com (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".


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.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #12
red floyd wrote:
Phlip wrote:
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.


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.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces

Jul 22 '05 #13
gotta love your email address, by the way.

David

Suzie wrote:
wi******@yahoo.com (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

Jul 22 '05 #14
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.

"Suzie" <wi******@yahoo.com>
??????:ec**************************@posting.google .com...
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?

Jul 22 '05 #15
Phlip wrote:
red floyd wrote:

Phlip wrote:

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.

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).

Jul 22 '05 #16
red floyd wrote:
Yes. I had the misfortune to own both of his C++ books (The Complete
Reference, and the Pocket Reference).


I'm sorry. I had no idea.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #17
Phlip wrote:
red floyd wrote:

Phlip wrote:

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.

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?
Jul 22 '05 #18
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

"Suzie" <wi******@yahoo.com> wrote in message
news:ec**************************@posting.google.c om...
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?

Jul 22 '05 #19
In comp.os.linux.advocacy, Brad Herald
<bh*****@vt.edu>
wrote
on Mon, 25 Oct 2004 21:19:41 -0400
<cl**********@solaris.cc.vt.edu>:
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::ostream & operator<< (std::ostream &, 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.)

Brad

"Suzie" <wi******@yahoo.com> wrote in message
news:ec**************************@posting.google.c om...
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?


--
#191, ew****@earthlink.net
It's still legal to go .sigless.
Jul 22 '05 #20
On Fri, 22 Oct 2004 22:41:21 -0700, Suzie wrote:

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

A better question is, why you are you posting here, rather then the proper
group? There are many good books on the subject, not to mention a billion
web sites which explain C++ 101 programming. Lastly, there are really
skilled developers in some of the develoer groups. This is not the place
to be asking this question.
Cheers,

Greg

Jul 22 '05 #21
"Greg Copeland" <no****@nowhere.com> wrote...
On Fri, 22 Oct 2004 22:41:21 -0700, Suzie wrote:

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

A better question is, why you are you posting here, rather then the proper
group? There are many good books on the subject, not to mention a billion
web sites which explain C++ 101 programming. Lastly, there are really
skilled developers in some of the develoer groups. This is not the place
to be asking this question.


Next time you could probably clarify which of the cross-posted
newsgroups is "not the place".
Jul 22 '05 #22
begin Victor Bazarov wrote:
"Greg Copeland" <no****@nowhere.com> wrote...
On Fri, 22 Oct 2004 22:41:21 -0700, Suzie wrote:

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

A better question is, why you are you posting here, rather then the
proper
group? There are many good books on the subject, not to mention a
billion
web sites which explain C++ 101 programming. Lastly, there are really
skilled developers in some of the develoer groups. This is not the place
to be asking this question.


Next time you could probably clarify which of the cross-posted
newsgroups is "not the place".


How about "all of them"
--
I doubt, therefore I might be.

Jul 22 '05 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Charles | last post by:
I am going through the exercises and Bruce Eckel's Thinking in C++ and I ran into an exercise that wasn't included in his solutions that I think I could use some assistance with. Exercise 3-26...
10
by: Simon Mansfield | last post by:
I have been given a list of exercises to do by my tutor, one of which is this: http://www.cems.uwe.ac.uk/~amclymer/Software%20Design%20and%20C++/Exercises/Exercise%208/Exercise.html Which i am...
12
by: Merrill & Michele | last post by:
It's very difficult to do an exercise with elementary tools. It took me about fifteen minutes to get exercise 1-7: #include <stdio.h> int main(int orange, char **apple) { int c; c=-5;...
10
by: Patrick M. | last post by:
I don't quite understand what K&R want me to do in Exercise 1-20 in "The C Programming Language, 2nd Edition". Here's the description: "Write a program detab that replaces tabs in the input with...
16
by: Josh Zenker | last post by:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to me. Is there a more elegant way to do this? #include <stdio.h> /* copies input to output, printing */ /* series of...
5
by: ebrimagillen | last post by:
Hello mates, Just needed a solution on the exercises below. Exercise 2 A classic problem in introductory programming is the grains of rice on a chess board problem. Your program should...
8
by: Indian.croesus | last post by:
Hi, I apologise if this is not the forum for my question. I am a starter in C and I have come to a point where I can not figure out if knowledge of assembly language is really essential for an...
5
by: Richard Gromstein | last post by:
Hello, I have an exercise that I need to finish very soon and I really need help understanding what to do and how exactly to do it. I am working on reading the chapter right now and working on it...
26
by: arnuld | last post by:
this is the programme i created, for exercise 2, assignment 3 at http://www.eskimo.com/~scs/cclass/asgn.beg/PS2.html it runs fine. i wanted to know if it needs any improvement: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.