Registered: Aug, 2005
Re: The miseducation of C++ programmers
Posted: Aug 18, 2005 10:37 PM
For a long time I don't like the usual curriculums taught on C++: classes, inheritance, protection, friends, virtual functions and then maybe templates. I think that students should be introduced to the OO part of C++ at the very end of their course. Good OO design is something you cannot learn in the course - it takes time and to see many good and bad examples.
When I was learning C++, most of my time I have (uselessly!) spent on deciding about protection levels, class hierarchies that never quite come out properly, etc.
What I was trying to accomplish with inheritance and virtual functions was actually genericity. And templates are much more suitable for this than OO.
I have been exposed to several functional languages, Ocaml (http://caml.inria.fr) and Scheme among others, and my current C++ programming style reflects my 'functional' thinking. In C++, templates make it relatively easy to program in functional style, although the syntax is much uglier than in e.g. Ocaml.
Now, when designing something new, I rarely mess with the OO part of C++ - I'm mostly using templates to achieve genericity and static polymorphism. And "function objects" can serve as a poor man's closures.
If I were to teach C++ to students who have already been exposed to C or other C-like language, this would be the order of topics:
1. memory allocation with new/delete/delete; references; function and operator overloading.
2. brief introduction to enhanced structs (briefly mention methods, constructors, destructors, that the struct name is a new type without the typedef). i would not mention classes and protection levels.
3. introduce struct and function templates, why to use them
4. introduce STL and many problem-solving exercises using STL
5. introduce boost and boost::bind and boost::lambda at least
6. show how to define your own generic algorithms and data-structures, in effect extending the STL. the point would be to show how to "abstract-away" relevant implementation details into policies. I find it much easier to abstract-away what is generic (e.g. comparison function in std::sort) than designing a proper class hierarchy.
7. iostreams (IMHO, they're really useful when you are doing more advanced stuff; for ordinary I/O C-style is IMHO much easier and controllable).
8. classes, protections, inheritance and virtual functions.
9. a bit on design patterns
I would spend perhaps 75% of the course time on topics 3-6.
Well.. this was a bit long.. just my 2 cents :)