Tomás said:
Looking at the "Hello World" example:
#include <iostream>
int main()
{
std::cout << "Hello World!";
}
I've always thought this was a horific piece of code to throw in front of a
beginner. I started out programming in Visual Basic when I was about twelve,
and then when I moved on to C++, I was greeted with this really weird way of
printing things to the screen. I hadn't a clue what the craic was with the
"<<".
To trully understand how "cout" works, one has to understand operator
overloading. Only then can one realise, "Oh yeah, they overloaded the bit-
shift operator to make it look like you're putting things toward cout".
This is the basis of our disagreement.
Who cares about truly understanding how cout works? The point is that
by utilising the tools provided by the standard library from the outset
a C++ student can immediately be more productive in creating correct,
clear programs, which is the goal of all programming. An understanding
of what is really going on in that statement - what is cout, what is
<<, what is overloading and how is it achieved - can be left until much
later without imparing their ability to write programs. Exactly the
same argumant applies to dynamic arrays vs vectos, char* vs strings
etc.
As for recognising << as a bit shift operator, that might be the case
for someone with previous experience of C. In that case, issues like
char* vs string is not so much to do with what is easiest to learn (a C
programmer will already know how to use char arrays). The issue is what
helps you write correct code more easily that will potentially be
maintained by C++ programmers who do not have a C background.
A complete novice will not recognise << as a bit shift operator. To
them it will be the stream insertion operator, which is no bad thing,
since that is often its main use. Later they will encounter its use in
bit shifting. If operator overloading has come up already this can be
shown as an example. It's not particularly important which is the core
language meaning and which is the library overload at this stage.
When I first saw the statement, I hadn't a bull's notion what "cout" was. Is
it an object? Is it a class? Is it Superman?
I'd re-assure learners that the whole "cout <<" thing should *indeed* appear
very strange to them, but that they should just accept that that's how it is
until they learn operator overloading, at which point everything will become
abundantly clear.
Operator overloading should not be mentioned unless you are teaching C
programmers who already know << to mean something else.
The real problem is one of primacy. Unless you put in concious effort
to avoid it, you will innately fall back on what you learn first.
When writing a C++ program, for clarity, correctness and robustness,
vector should always be the default preference over a dynamic array and
string should always be the default preference over char* (which is
certainly hackish). If you teach them the wrong way round, there is the
risk that the standard library components will be regarded as clever
and advanced variations. The student's natural way of thinking about
programming with strings will be char*, because that's what they learnt
first. It's certainly possible to break this backwards thinking, but
why introduce the need when it can be avoided so easily?
WIth C programmers learning C++, the primacy battle is already lost and
the only option is to break the entrenched backwards thinking. All the
more reason to hammer home the point early and hard and often.
Gavin Deane