From Python to C++
August 21, 2018 6:59 AM   Subscribe

I'd like to learn C++, and I'm looking to do it with a relatively structured course or set of exercises rather than the "dream up a project and fake your way through it" approach. My programming background is in Python, which I'm really quite fluent in — so I don't want a "learn to program" or "learn what classes are" course, but pointers and memory management will be new. I would spend money on an online course if it was the best option available. Suggestions?

(For reasons having to do with the company I work at, it really does need to be C++ and not whatever other language you find preferable.)
posted by nebulawindphone to Computers & Internet (8 answers total) 13 users marked this as a favorite
 
If you have programming experience, then I'd just start with Stroustrup's A Tour of C++. It will introduce all the key concepts of modern C++ programming (disclaimer, I contributed errata!). Work through all the examples and you'll come out the other end with facility in C++.

Then, build some things! You can hone your knife with the Scott Meyer's "Effective Modern C++" books.
posted by dis_integration at 7:06 AM on August 21, 2018 [2 favorites]


Yeah, Stroustrup is very readable.
posted by johngoren at 7:34 AM on August 21, 2018


(This isn't directly an answer but hopefully it is useful as meta-direction)

The main way C++ is different from almost all other modern languages is it's a very wide language with weird gaps, the result of which is that there are many different styles people write in, often about pretty fundamental language features (exceptions vs return codes, how memory is managed, etc). Part of the reason for this is there was a big modernization effort with C++11 - best-practices C++ written before that point looks very different from code written after. (To a lesser extent there were similar updates with C++14 and 17, which is the latest prod release). Another reason is there are many very large libraries designed to fill gaps in the standard libraries - boost, folly, etc - which again have a big effect on the way you write code. So in general, I think it is much more important to learn your specific company style for C++ vs other languages - you may as well work through some general courses but you should really check with coworkers for specifics.

All that said, assuming you're writing post-C++11 code, you should watch https://youtu.be/xnqTKD8uD64 for a good conceptual discussion of memory management and similar.
posted by inkyz at 8:49 AM on August 21, 2018 [1 favorite]


I would second the idea that the various dialects of C++ are quite different and you should try to pick a direction. Python is great because it tries to have one way to do things but C++ is not at all that way. With braced initialization, lambda functions, for-all syntax, auto types, reference-counted pointers, C++11/14/17 is barely recognizable compared to older versions. And none of those involves classes. In fact, there has been a decided movement away from the traditional deep inheritance hierarchy to composable, functional (as in Lisp) style libraries. The biggest issue will be memory management and templates, memory is being done with RAII (Resource Allocation Is Initialization) and polymorphism is often handled via variadic templates now. I looked around a bit for online courses and didn't find much, which surprised me. C++ for C programmers is going to spend a lot of time on classes, which you don't need, while omitting crucial material on memory and type safety. Some of the stuff, like files, printing, and your basic container data types, are going to be useful no matter what, and honestly the language is so big that you probably couldn't do more than skim the whole thing. Do you have a local university or college that might have a course?
posted by wnissen at 11:07 AM on August 21, 2018


I found Stroustrop's Tour of C++ helpful when I needed to get a half-assed knowledge of C++ to hack on some scientific code.

Many C++ people are very annoyed with the "teach them to write C, and then let them take some convenient C++ features a la carte" approach that is often used. Which makes sense, especially since modern C++ is a radically nicer language with many different, superior ways of doing things.

But my own experience was that it really helped to be able to "get around" in C at a basic level. Getting a feel for how arrays and structs are laid out in memory, stack vs heap allocation, how C-style null-terminated ASCII strings work, and so on. I think this helped me actually properly understand how memory is really laid out*, and it also was useful because, for better or worse, you often end up having to either interact with C libraries or with less-than-modern C++ code that includes a lot of C idioms.

(the obvious recommendation for C is still K&R. it's a really good, very short book for learning the basics, which is all you need. sometimes people who in 2018 program in C for a living will say it teaches bad programming style, which may be true but doesn't really matter for this purpose IMO.)

Also I strongly endorse learning the basic use of Valgrind ASAP (it finds memory leaks among many other errors and yells at you about them).

*: i was surprised to learn that there are arguments that even C is really a high-level programming language that hides the real details of memory management from the programmer
posted by vogon_poet at 2:59 PM on August 21, 2018


I agree that learning C first is a boon. Otherwise C++ idioms are even more inexplicable and obscure.
posted by stoneandstar at 4:56 PM on August 21, 2018 [1 favorite]


exercism.io is a great way for a programmer to dig into another language. Working through some or all of the exercises provided will help solidify the concepts you learn from the books.
posted by mutagen at 9:54 PM on August 21, 2018


I picked up SFML Game Development a couple years ago when I wanted to learn a bit about game programming. I didn't know any C++ when I started, although I did have some experience with C and Java, which made me comfortable jumping in the deep end. I thought it was a fun way to learn a new language. The example code makes a point of using C++11 idioms, so you get a nice holistic introduction to modern usage.
posted by jomato at 6:13 PM on August 22, 2018


« Older Help me find this tweet   |   How do I give feedback on a new friends... Newer »
This thread is closed to new comments.