Monday, May 7, 2018

Navigating the C++ forest

I have recently been working my way through 'C++ Primer', 5th edition by Lippman, Lajoie and Moo.  The front cover says the book has been a bestseller since 1986 and also that the most recent edition has been completely rewritten for the new C++11 standard.  The authors have worked in leading companies and laboratories such the Bell Laboratories, Pixar, Microsoft, IBM and AT&T.  And they have worked closely with the creator of C++, Bjarne Stroustrup.  The contents of the book live up to the billing on the cover!

I learnt some C++ a long time ago with the help of a book called 'C++: A Beginner's Guide', 2nd edition by Schildt and a university course based on the book 'Guide to Scientific Computing in C++' by Pitt-Francis and Whiteley.  These books are both fairly introductory.  I would recommend them, not least because they are both much shorter than C++ Primer. 

The advantages of C++ Primer, in my opinion, are that it gives a more comprehensive overview of the language and has a strong emphasis on Modern C++, e.g., features that were introduced in the C++11 standard.  Somehow C++ Primer achieves this without being dry!

Another popular C++ book is Bjarne Stroustroup's 'The C++ Programming Language.'  I borrowed this from my university library before I bought C++ Primer and didn't really like it.  I think it covers advanced material in more depth than C++ Primer, but it reads much more like a reference book than a tutorial.  It is longer than C++ Primer and I suspect it contains a lot of material that is not relevant to what I'm doing.

The main thing that I've learnt over the last few months is that C(++) is not really one programming language.

Although you can compile and run programs written in C, old-style C++, and Modern C++ using the same compiler, they look and feel very different from each other.  When I only knew C and old-style C++ I found it very difficult to understand programs written in Modern C++.

The C language first appeared in 1972.  If you write in C then you have to learn about pointers and use arrays if you want to do calculations with vectors and matrices.  Dynamic memory allocation is particularly awkward to implement.  There are not that many concepts to learn, and I think it is not a bad language to learn if you are prepared to learn some basic low-level programming and want to write code that runs quickly.  Because C is so simple it tends to be easier to interface with other languages, such as Fortran and Matlab.

C++ introduces features that are not available in C such as function overloading, inheritance, and the idea of data abstraction.  It is designed to facilitate Object-Oriented Programming (OOP).  Code written in OOP separates interface from implementation.  It is designed to give full control to library developers and stop users from doing anything stupid.  I generally find code written in OOP difficult to read.  I like to work in an environment where I can implement algorithms myself, but also have access to a library of algorithms that I can call easily.  Old-style C++ does not really seem to be set up for this.

In contrast, it is relatively easy to learn Modern C++, at least to the level where you can use it for simple programming tasks.  There doesn't seem to be a precise definition of Modern C++, but this page has a fairly good summary - https://docs.microsoft.com/en-gb/cpp/cpp/welcome-back-to-cpp-modern-cpp.  I think it is fair to say that I knew almost nothing about Modern C++ from the initial learning I did (which was around 10 years ago).

Although I am not generally a fan of new standards for programming languages, I have noticed that a lot of answers on Stack Overflow for C++ questions use C++11.  Often solutions for different standards are presented and the C++11 standard looks nicer / more elegant.

If you want to write programs that run fast without having to learn much / anything about low-level programming, Modern C++ is probably the way to go.  The vector and string classes in the Standard Library implement dynamic memory allocation without the user having to know anything about how this is implemented.  So it is possible to write clean looking programs that are actually doing quite sophisticated operations.

Really understanding Modern C++, to the point where you can write high-quality libraries requires a lot of effort.  To give you some idea, C++ Primer starts with a section called The Basics, which runs to about 300 pages.  The reason it is long is because there is a lot to cover, not because the writing is verbose!  Object-Oriented Programming (including concepts such as inheritance) is not discussed until p600.  And the section on Advanced Topics starts on p715.

In summary, the world of C++ is difficult to navigate, at least in my experience.  C++ Primer is the best guide that I have found.  Chapter 1 of C++ Primer on Getting Started  (only 30 pages) is particularly impressive for the range of ideas that it introduces.  I would recommend C++ Primer to people who are literally just getting started with programming as well as to people who want to write high-quality libraries.


 

No comments:

Post a Comment