Suggestions for Learning C++¶
The following contains suggestions for how to most effectively learn a new programming language. There are a few common steps you can take to learn most any new programming language, and I’ll describe those here along with suggestions specific to C++.
Before I say anything else, though, my primary, above-all-else suggestion is to remember you’re not learning a language on your own. When you have a question or run into something difficult, and you will, post a question in the Q&A forum and/or come to office hours. Someone else in the class has probably run into the same thing and can help, and I definitely can. It’s a waste of your time to try to figure everything out yourself when getting some help would be much more efficient. Don’t waste your time like that. And help each other out.
1. Getting Started¶
The very first thing to work out in any new language is “Hello, World!” The hello world program is basically the simplest program you can write in most languages, and your first task is to get that working. By getting hello world to work, you will have learned the basics of the language’s syntax, but more importantly, you’ll have learned how to write, store, and execute a program in the language. Does the language use an interpreter? Is it compiled? Do you run the code with a virtual machine? Etc.
Text Editor¶
To write and edit your code, use any programming text editor you like. Visual Studio Code (aka VSCode) is popular, free, and geared toward programming. VSCodium is a more free/open version that is not tied so tightly to Microsoft, but without some of the features Microsoft exclusively allows in VSCode.
Beyond text editors, Integrated Development Environments (IDEs) are powerful applications for developing complex software. If you want to use an IDE, there are many options. VSCode is sort of a “lite” IDE, especially when you install various extensions to extend its functionality. But a “full” IDE comes with everything built-in, including far more features than you are likely to use early in your education. Some of the most well-known and commonly-used:
- Eclipse [free, open-source]
- Netbeans [free, open-source]
- Visual Studio [commercial product, free “community” version available]
- CLion [commercial product, free for students]
These are all professional tools, so the features and options and interfaces can be daunting at first. Good tutorials exist for all of them, and you can always just ignore the vast majority of the features that you aren’t using at first.
Command Line¶
It’s worth knowing how to compile and run your code from the command line, even if you end up using an IDE, so make sure to at least become familiar with the process. That is how instructors and automated checkers will run and test your code, usually, so it is wise for you to test it that way as well.
There are a few different free command line compilers for C/C++; GCC and LLVM/Clang are the most commonly used. Installation and usage depends on your operating system:
- For Windows, I suggest using WSL, which provides a full Linux environment inside Windows. (Linux is a separate operating system from Windows and MacOS. Lots of development work is done in Linux, and many development tools are written primarily for Linux.) The VSCode documentation provides a guide for developing in WSL using VSCode, and then you can use their guide to C/C++ support on Linux to set up the language and start writing C++ code.
- For Mac OS, you can install a compiler by opening a terminal and running
clang++. It will either already be installed or prompt you to install the needed software. The VSCode guide to C/C++ support on MacOS explains more.
To compile and run a program on the command line, you’ll need to be familiar with working with a Unix command line.
To compile a program, you can run:
g++ -std=c++20 -Wall -Wextra prog.cpp -o prog
The arguments used here are:
-
-std=c++20: Tells the compiler to use the modern C++20 standard. -
-Wall -Wextra: Enables helpful compiler warnings to catch errors early. -
prog.cppis the name of the source file you are compiling. -
-o prog: Outputs the compiled program as a file namedprog.
The program will be created with the name prog, and you should be able to run it as ./prog.
Or, if you are compiling multiple source files into one program:
g++ -std=c++20 -Wall -Wextra prog.cpp other1.cpp other2.cpp -o prog
For more complex programs, it is best to use a makefile or the CMake tool to organize and simplify the build process, but the single commands above suffice for simple programs.
2. Learn the Syntax¶
Once you know how to write and execute a basic program, it’s time to figure out how to write other programs using the programming concepts you already know. That’s primarily a matter of learning the syntax for loops, functions, variables, conditionals, etc. in the new language.
Books & Tutorials¶
To start, you should take a methodical approach, working through something structured to… well, give you some structure.
There are several good C++ textbooks freely available online. Many are out of date, however, as the C++ language has been updated with several major improvements (often referred to as “Modern C++”), and it is best to use materials that reflect and teach with those improvements.
- Fundamentals of C++ Programming — Available as a PDF. Includes some discussion of Modern C++ features.
- How to Think Like a Computer Scientist: C++ Version — Available as a PDF. Doesn’t include Modern C++ features, but is otherwise high quality.
If you prefer a physical book, the five listed at the bottom of this page are some of the most frequently recommended.
You can find a large number of C++ tutorials online as well. These few are the best I’ve found:
- LearnCpp.com — High quality, clearly presented, and regularly updated.
- Programiz: Learn C++ Programming — Echoes much of what is covered on this page (background, how to compile/run, etc.); includes links to tutorials on specific topics at the bottom.
- cplusplus.com Tutorial — Somewhat limited relative to the others, but covers the basics well enough.
I suggest evaluating a few different resources to find one you think might work best for you. As you’re learning, you can always look into the others for alternate explanations and perspectives on particular topics, and you may find you want to switch to a different one later.
References / Cheat Sheets¶
At some point, you can start looking up specific topics in a general reference. You’ll have the general idea in mind, but you’ll run into something from time to time that you want to look up. The following references assume you don’t need to learn the basics of programming.
- C++ Reference Card — A compact, 2 page PDF.
- C++ Syntax Cheat Sheet — More detailed, with some decent explanations throughout.
- C++ Patterns — Simple, explained examples of Modern C++ features.
- Elements of Modern C++ Style — Good for familiarizing oneself with Modern C++ features and how/when to use them.
- cppreference.com — Comprehensive, cleanly-presented reference for C++ language features and standard libraries. Best used via search.
Examples¶
Additionally, example code is always very helpful. It’s one thing to read about a topic and see the bare syntax patterns, but it can be much more useful to see a complete, working example:
3. Practice¶
You might be tempted to just start into an assignment right away, and I can’t stop you, but I’d recommend you take just a bit of time to practice with the language first. By practice, I mean work on simple problems that give you immediate feedback about whether you’ve solved it correctly or not. These two sites, for example, let you submit code to solve problems online, and they give you instant feedback. Run through some exercises. Focus on specific topics you want to learn.
4. Write Lots of Code¶
There’s no way around it: you have to write a lot of code to become proficient with any language. The more you write, the more you’ll learn and the easier it will get.
5. And Don’t Forget!…¶
Ask for help when you need it. You will need it, and you should ask. I understand the urge to figure something out yourself, but 1) it’s a waste of time to bang your head against something for hours when you could get a quick answer to a quick question and move on, and 2) you get no extra points, figurative or otherwise, for learning something yourself. You get points, literal and otherwise, for learning something, no matter how you learn it, so make sure you’re doing so in the most effective way possible for yourself.