how to learn cpp

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艹.

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 250 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.

These next two are C艹98-era books so they don’t cover newer features in C艹11 and C艹20, but the code still works (C艹 has great backward compatibility) and the techniques are useful and even nifty.

  • 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 C艹.com’s Get Started page.