473,714 Members | 2,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1637
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
"Accelerate d 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++.m oderated.

--
Penguins love icebergs.
Jul 22 '05 #5
In comp.os.linux.a dvocacy, 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_wi thassign (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.serialNumbe r();
return out;
}

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

--
#191, ew****@earthlin k.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.goo gle.com...
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

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

Similar topics

5
2531
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 pg 213: Define an array of int. Take the starting address of that array and use static_cast to convert it into an void*. Write a function that takes a void*, a number (indicating a number of bytes), and a value (indicating the value to which...
10
1785
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 finding very difficult to understand what is required and how exactly to do it.. Any thoughts/idea's will be greatfully recieved! Global_Inferno
12
2324
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; while(c != EOF ) {
10
3466
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 the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every n columns. Should n be a variable or a symbolic parameter?" What does this mean? Do they want me to figure out the amount of spaces a tab...
16
2272
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 blanks as a single one */ int main() { int c;
5
3034
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 calculate the number of rice grains on the last square of a 64 square chess board given that there is 1 grain of rice on the first square, 2 on the second square, 4 on the third square and so on. Use a while loop to double the number of rice grains and...
8
1413
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 in depth understanding of C. Also while going through some of the old posts, essentially as an exercise to understand C better, I read the following from one Mr. Keith. "One concrete example is the C implementation on Cray vector machines.
5
2812
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 myself, but I have a feeling I won't get far. Please help me complete the exercise. I am posting what needs to be done and will be updating with pieces of code that I come up with. Thank you all for your attention. ...
26
2122
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: ----------------- PROGRAMME ---------------------------- /* Steve Summit's C programming Section 3 :: exercise 2
0
8796
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9307
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7946
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6627
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4462
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4715
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2105
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.