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.

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 (below), 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 you 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 steps depend on your operating system:

  • For Windows, I suggest using Cygwin. These instructions are a bit old, but the steps should still work in the same general way. The default options in the installer will also install a command line terminal and place an icon for it on your desktop.
  • For Mac OS, you can install a compiler by opening a terminal and running g++. It will either already be installed or prompt you to install the needed software.

To write and edit your code, use any programming text editor you like. I recommend BBEdit (Mac), Notepad++ (Windows), Atom (cross-platform), or Visual Studio Code (cross-platform). All are free and geared toward programming.

To compile and run a program, you’ll need to be familiar with working with a Unix command line.

To compile a program, run:

g++ prog.cpp -o prog

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++ prog.cpp other1.cpp other2.cpp -o prog

For more complex programs, it is best to use a makefile to organize and simplify the build process, but the single commands above suffice for simple programs.

Integrated Development Environment (IDE)

If you want to use an IDE, there are many options. 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.

Of these, Eclipse is very commonly used in education, and it is installed in the CNS labs. If you use Eclipse on Windows for developing in C++, you need to install a separate C++ compiler; the instructions linked above for installing GCC under Cygwin are one way to accomplish this.

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.

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 — Echos 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.

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.