I think the idea of using arrays for strings (one that comes entirely
from C) was a quick, effective and intelligent solution for creating
strings using other data type elements in the language. For that
matter "char"-s and "string"-s are types derived from "int"-s and
arrays. I feel, the way we construct char arrays with terminating
zeros is so that we can write functions that process such arrays as
strings.
If there was no C, only C++, I would presume we would never speak
about "char*"-s ... only "std::string"-s ... or in a multi-lingual
world ... of std::basic_string, leaving "char*"-s as an implementation
detail perhaps.
Having said that ... I wonder how judgmental can we get regarding the
appropriateness of teaching "char*"-s before "string"-s. If it's a
quick C++ course where the students are expected to get up to speed in
using strings, I guess it's a good idea to jump to std::string. If
however, the language elements are to be understood thoroughly, then
the students need to see how strings can be constructed using arrays
.... how well they serve specific needs and where they fall short ...
whereby the need for better alternatives arise and what those
alternatives are. The reason I say this is because students need to
have a motivation for learning something. And the motivation can come
from the understanding the need to use one method over the other.
std::string is not a small isolated topic ... it is merely a typedef
for a generic stl class. May be they should be taught with this fact
in context ... which would require them to have the more basic
language elements in control.
--
Cheers,
Andy
Andrew Koenig said:
I should think using char [ ] for strings must still be taught. Firstly, it
is an intuitive way to "see" arrays and learn to use them. Secondly, there
is lots of code extant that uses them and a beginner better learn to
understand it. Thirdly, it still seems easier to me to teach it first and
then switch to standard string class. The relief or not having to deal with
the problems of char [ ] shows the benefit of the standard libraries
clearly. But making the switch also involves discussion of namespaces which
I tend to defer until the little coders are reasonably confident using the
scope resolution operator to access globals, statics, and classes.
I agree with you that every professional C++ programmer must eventually
learn how to use arrays. However, I don't think that teaching them before
teaching strings is the most useful way to go. The trouble is that
beginners have enough information to absorb that there's a very good chance
that they will be unable to write programs that work correctly for quite a
while--long enough for them to become frustrated.
I have taught C++ both ways, and my experience is that the students learn a
lot faster when they start with strings, then move to arrays of characters.
One reason is that they spend much less time debugging.
Without knowing what book, I'd say it isn't the best advice to abandon it
just yet. I'm aware you prefer to teach standard C++ from the outset and
fill in the retrograde topics later. Besides, I can't get the school to use
your books -- damned committees!
Interesting. I'd like to know what the committees' objections are. Perhaps
we can discuss this offline?