C++ Primer 5th Edition, Part 5: Core Language Versus Library?
One of the most important problems to consider in planning a C++ curriculum is how to teach the relationship between the core language and the standard library. In theory, the two are independent: It is possible to write C++ programs that do not use the standard library at all — particularly if an alternative input-output library is available. Of course, the core language is always available, and this constant availability implies that programmers who learn to use it can always count on that knowledge being useful.
Because every C++ programmer can count on the core language, it is tempting to bias C++ teaching in its favor. This bias shows up most strongly in how a C++ book treats two common data structures: strings and arrays. If you're willing to teach the library from the beginning, you can use examples such as:
string hello = "Hello"; string world = "world"; string helloworld = hello + ", " + world;
If you insist on sticking to the core language, you have to use examples such as:
const char* hello = "Hello"; const char* world = "world"; char* helloworld = new char[strlen(hello) + strlen(world) + 2]; strcpy(helloworld, hello); strcat(helloworld, ", "); strcat(helloworld, world);
Aside from its verbosity, this second example has four problems. First, it doesn't really avoid the standard library, because it uses
strlen. Of course we might argue that these three functions are so simple that programmers can implement those functions by themselves if they want to avoid using the standard ones. However, this strategy leads to the second problem: Both the implementation and the use of these functions requires a great deal of extra knowledge. Time that students spend acquiring that extra knowledge is time that they're not spending on learning how to solve the problems that really matter to them.
The third problem is that the second example is incomplete: It allocates memory for
helloworld but never frees it. Therefore, an intellectually honest explanation of the second example should really include an extra statement
delete  helloworld;
at a suitable place near the end of the program, along with an explanation of why the brackets are necessary, even though some implementations will work just fine without them.
The fourth problem is that code like the second example is hard to get right. To prove it, I've made a typical beginner's mistake in this example. Sharp-eyed readers are invited to find it before I explain it next week.
Obviously, I think it's a good idea to rely on the standard library from the beginning of the curriculum. As a result, it will probably not surprise you to learn that C++ Primer, 5th Edition starts its detailed coverage of strings in Chapter 3, on page 81 of a nearly 900-page book. For contrast, I looked at the table of contents of another well-known C++ book, which I'd rather not name. What I will say is that it's 1,300 pages long and, as far as I can tell, does not discuss strings in detail until Chapter 16 (page 951).
Maybe I have some kind of a conceptual blind spot, but I don't understand what advantages come from deferring the standard library for that long. Even if the goal is to teach students how to program at a low level, I think that they are much less likely to become frustrated, and therefore likely to learn more in less time, if they start with high-level abstractions and then, once they have learned how to use them, take the covers off and look inside to see how they work.
I invite comments, especially from readers who think that it is more effective to teach low-level concepts first.