how to learn cpp

Save to:
Instapaper Pocket Readability

Learning C++

What is mentoring?

It’s the most important tool in learning a new technology.

Object-oriented and generic thinking is caught, not just taught. Get cozy with someone who really knows what they’re talking about, and try to get inside their head and watch them solve problems. Listen. Learn by emulating.

If you’re working for a company, get them to bring someone in who can act as a mentor and guide. We’ve seen gobs and gobs of money wasted by companies who “saved money” by simply buying their employees a book (“Here’s a book; read it over the weekend; on Monday you’ll be an OO/generic developer”).

Should I learn C before I learn C++?

Don’t bother.

The common subset of C and C++ is easier to learn than C. There will be less type errors to catch manually (the C++ type system is stricter and more expressive), fewer tricks to learn (C++ allows you to express more things without circumlocution), and better libraries available. The best initial subset of C++ to learn is not “all of C”.

The go-to book to learn C++ is Stroustrup’s A Tour of C++. Run, don’t walk, to read and recommend this core overview.

If you want additional options: See Stroustrup’s Learning Standard C++ as a New Language for a discussion of the choice of C++ constructs, techniques, and libraries for early learning. For an example of books that takes that approach systematically, see Stroustrup’s Programming: Principles and Practice using C++ and Koenig & Moo’ Accelerated C++ from Addison Wesley’s C++ In Depth series.

If your ultimate goal is to learn C++ and you don’t already know C, reading books or taking courses in C will not only waste your time, but it will teach you a bunch of things that you’ll explicitly have to un-learn when you finally get back on track and learn C++ (e.g., malloc(), printf(), unnecessary use of switch statements, error-code exception handling, unnecessary use of #define macros, etc.).

If you want to learn C++, learn C++. Taking time out to learn C will waste your time and confuse you.

Should I learn Objective-C or another OO language before I learn C++?

Don’t bother.

Learning something new is almost always a good idea. However, each language is different and has its own styles and quirks. Code written in some supposedly “pure” OO style modeled on some other language (quirks and all) is often sub-optimal and frustrating when too literally transcribed into C++. Also, “writing just pure Object-oriented code” is not one of C++’s ideals; see Stroustrup’s OOPSLA keynote Why C++ isn’t just an Object-Oriented Programming Language. If you want to become a good C++ programmer and don’t have a few months to spare, concentrate on C++ and the concepts it embodies.

If your ultimate goal is to learn C++ and you don’t already know Objective-C, reading books or taking courses in Objective-C will not only waste your time, but it will teach you a bunch of things that you’ll explicitly have to un-learn when you finally get back on track and learn C++ (e.g., dynamic typing, non-subtyping inheritance, error-code exception handling, etc.).

Knowing a “pure” OO language doesn’t make the transition to OO/C++ any easier. This is not a theory; we have trained and mentored literally thousands of software professionals in OO. In fact, Objective-C experience can make it harder for some people: they need to unlearn some rather deep notions about typing and inheritance in addition to needing to learn new syntax and idioms. This unlearning process is especially painful and slow for those who cling to Objective-C with religious zeal (“C++ is not like Objective-C, therefore C++ is evil”).

If you want to learn C++, learn C++. Taking time out to learn Objective-C will waste your time and confuse you.

How do I start learning C++?

Quick answer: Read Stroustrup’s A Tour of C++.

Naturally, that strongly depends on what you already know and your reasons for learning C++. If you are a novice at programming, we strongly recommend that you find an experienced programmer to help you. Otherwise, the inevitable mistakes about language concepts and practical problems with the implementation you use can magnify into serious frustrations.

You’ll need a textbook for learning C++. This is the case even when your implementation comes with ample on-line documentation. The reason is that language and library documentation together with sample code are not good teachers of concepts. Typically such sources are silent about why things are the way they are and what benefits you can expect (and which you shouldn’t expect) from a technique. Focus on concepts and techniques rather than language-technical details.

When choosing a book, look for one that presents Standard C++ and uses the standard library facilities in an integrated manner from the start. For example, reading a string from input should look something like

string s;   // Standard C++ style
cin >> s;

and not like this

char s[MAX];    /* Standard C style */
scanf("%s",s);

Look for book recommendations from programmers with solid C++ experience.

For a gentle introduction suitable even for people who have never programmed before, a great book is Programming: Principles and Practice using C++, but remember that no one book is the best for everyone. Have a look at the book reviews on the ACCU (The Association of C and C++ Users) site.

Aim to write idiomatic C++: Avoid simply writing code in the style of your previous language using C++ syntax. There is little to be gained from simply changing syntax.

What is the best book to learn C++ from?

See also why to buy several books.

If you are new to programming (have never programmed before), consider Programming: Principles and Practice using C++. This is the book Bjarne Stroustrup wrote for a freshman (1st year university students) programming class and it has benefited from three years of classroom use.

Otherwise, the go-to book to learn C++ is Stroustrup’s A Tour of C++. Run, don’t walk, to read and recommend this core overview.

If you want additional options: See Stroustrup’s Learning Standard C++ as a New Language for a discussion of the choice of C++ constructs, techniques, and libraries for early learning. For an example of books that takes that approach systematically, see Stroustrup’s Programming: Principles and Practice using C++ and Koenig & Moo’ Accelerated C++ from Addison Wesley’s C++ In Depth series.

When looking for a second or third book, it depends what you’re looking for and on your learning style. There are quite a few excellent books on C++. Have a look at the ACCU (The Association of C and C++ Users) site. This is one of the best sites for book recommendations by experienced programmers who are not afraid to speak their mind (booksellers tend to give rosy reviews, and reviews of the form “This book is perfect, I love it, I have read almost three chapters, and can’t wait to read more” are worse than useless – why anyone would take advice on how to learn C++ from someone who completely lacks C++ experience beats me). The ACCU rates books for level of experience required and overall quality.

For people who are programmers and willing to learn new concepts and techniques from a classical textbook, we recommend The C++ Programming Language (4th edition). See a note about the structure, contents, and aims of “The C++ Programming Language (3rd edition)”: The book is aimed at programmers with some experience and a wish to master C++. It is not aimed at non-programmers trying to learn their first programming language or casual programmers trying to gain a superficial understanding of C++ as fast as possible. Consequently, this book focuses on concepts and techniques and goes to some pain to be complete and precise. It describes “pure C++,” that is, the language independently of any particular software development environment or foundation library (except the standard library, of course).

If you want to know why C++ is the way it is, have a look at The Design and Evolution of C++ (D&E). Understanding the design criteria and constraints helps writing better programs.

Should I buy one book, or several?

At least three.

There are three categories of insight and knowledge in OO programming using C++. You should get a great book from each category, not an okay book that tries to do an okay job at everything. The three OO/C++ programming categories are:

Legality guides describe all language features with roughly the same level of emphasis; morality guides focus on those language features that you will use most often in typical programming tasks. Legality guides tell you how to get a given feature past the compiler; morality guides tell you whether or not to use that feature in the first place.

Meta comments:

  • Don’t trade off these categories against each other. You shouldn’t argue in favor of one category over the other. They dove-tail.
  • The “legality” and “morality” categories are both required. You must have a good grasp of both what can be done and what should be done.

In addition to these (emphasis on “addition”), you should consider at least one book in each of two other categories: at least one book on OO Design plus at least one book on coding standards. Design books give you ideas and guidelines for thinking at a higher level with objects, and coding standard books establish best practices across your organization, plus help make sure everybody can read each others’ code (e.g., so you can move people around if one of the teams falls behind).

What are some best-of-breed C++ morality guides?

Here’s a subjective and selective short-list of must-read C++ morality guides, alphabetically by author:

  • Cline, Lomow, and Girou, C++ FAQs, Second Edition, 587 pgs, Addison-Wesley, 1999, ISBN 0-201-30983-1. Covers around 500 topics in a FAQ-like Q&A format.
  • Meyers, Effective C++, Third Edition, 320 pgs, Addison-Wesley, 2005, ISBN 0321334876. Covers 55 topics in a short essay format. Also C++98.
  • Meyers, Effective Modern C++, 336 pgs, O’Reilly Media, 2014, ISBN 1491903996. Covers 42 topics in a short essay format. C++14.
  • Meyers, More Effective C++, 336 pgs, Addison-Wesley, 1996, ISBN 0-201-63371-X. Covers 35 topics in a short essay format. Also C++98.

Similarities: All three books are extensively illustrated with code examples. All three are excellent, insightful, useful, gold plated books. All three have excellent sales records. Of these, only Effective Modern C++ covers C++11 and C++14. The others cover C++98.

Differences: Cline/Lomow/Girou’s examples are complete, working programs rather than code fragments or standalone classes. Meyers contains numerous line-drawings that illustrate the points.

What are some best-of-breed C++ legality guides?

As before, the go-to book to learn C++ is Stroustrup’s A Tour of C++. This takes you “around modern C++ in 180 pages.”

Beyond that, here is a subjective and selective short-list of must-read C++ legality guides, alphabetically by author:

  • Lippman, Lajoie and Moo, C++ Primer, Fifth Edition, 976 pgs, Addison-Wesley, 2012, ISBN 978-0321714114. Very readable/approachable, and covers C++11.
  • Stroustrup, The C++ Programming Language, Fourth Edition, 1368 pgs, Addison-Wesley, 2013, ISBN 978-0321563842. Covers a lot of ground and the definitive reference for C++11.

Both books are excellent overviews of almost every language feature. They’re both top notch, gold plated, excellent books. Both have excellent sales records.

What are some best-of-breed C++ programming-by-example guides?

Here’s a subjective and selective short-list of must-read C++ programming-by-example guides:

  • Koenig and Moo, Accelerated C++, 336 pgs, Addison-Wesley, 2000, ISBN 0-201-70353-X. Lots of examples using the standard C++ library. Truly a programming-by-example book.
  • Musser and Saini, STL Tutorial and Reference Guide, Second Edition, Addison-Wesley, 2001, ISBN 0-201-037923-6. Lots of examples showing how to use the STL portion of the standard C++ library, plus lots of nitty gritty detail.

Are there other books that are relevant to C++?

Yes! Tons!

The morality, legality, and by-example categories listed above were for programming. The areas of analysis and design are also relevant, and have their own best-of-breed books.

There are tons and tons of good books in these other areas. The seminal book on design patterns is (in my personal, subjective and selective, opinion) a must-read book: Gamma et al., Design Patterns, 395 pgs, Addison-Wesley, 1995, ISBN 0-201-63361-2. Describes “patterns” that commonly show up in good designs. You must read this book if you intend to do OO and generic design work.

Will someone here help me with my homework?

No. Sorry. We don’t do (other people’s) homework. We get too many requests for help with homework and help with finding bugs in student programs to be able to find the time. Anyway, having a distant expert fix your programs is not the best way to learn. Try finding a local person with C++ experience that you can ask for guidance. A good mentor is the best help a student can have; maybe that’s why they are not easy to find.

Also, no, we will not suggest “a good project for a student to work on”. Our experience is that learning enough about a student and his/her course to know what level of difficulty is required and what kind of project is of interest takes time. To think of a good project is then non-trivial, and to explain exactly what the project is and how to approach it can take several messages and several hours. We just don’t have that kind of time. Remember, these request come at least weekly. Finally, some students seem to have the idea that if we suggest a project, we are morally obliged to provide quite detailed help in its completion.

Ideas: Look at the exercises in TC++PL4 or other good textbooks. Many of those exercises are designed to keep a student busy for several days, and reading those exercises can inspire an enterprising student to so something similar. Or look at the non-computer-science part of your world: Maybe a biology project could use support for a new measurement device or a friend studying history could use an improved database interface. Many of the best projects and the best uses of computers are outside traditional computer science!

See also Stroustrup’s C++ style and techniques FAQ. Real novices facing their first “read some data, do something to it, and produce some output” exercise might be interested in a very simple program or a program reading a string from input.

Where can I get a free C++ compiler?

From lots of places; see isocpp.org’s Get Started page.