C++ Tutorial

C++ Get Started

Install a compiler locally, or use the StudyGrid C++ editor. Either way you need a .cpp file and a main function.

Two ways to run C++

You can compile in the browser on StudyGrid, or install a compiler on your computer. The language is the same. The editor is faster for lessons. A local compiler is what you use for real projects.

The StudyGrid compiler is /cpp/try. Python stays at /try. HTML stays at/html/try. C++ examples never open those two.

Use the StudyGrid editor

  1. Open a chapter and copy an example, or go straight to Try C++.
  2. Press Compile & run (or Ctrl + Enter).
  3. Read the build log. If g++ is unhappy, the program does not run.
  4. Read program output underneath. That is what cout printed.

If the program uses cin, type values in the stdin box on the left, then compile again.

Install a compiler (optional)

SystemTypical setup
WindowsMSYS2, MinGW-w64, or Visual Studio Build Tools. You want g++ or cl on PATH.
macOSXcode Command Line Tools: xcode-select --install. Apple Clang compiles C++.
LinuxInstall g++ from the package manager, for example sudo apt install g++.

Check that it works:

Example

g++ --version

Compile a file locally

Save this as main.cpp:

Example

#include <iostream>
using namespace std;

int main() {
  cout << "Compiled locally" << endl;
  return 0;
}

Then in a terminal in that folder:

Example

g++ -std=c++17 main.cpp -o main
./main

On Windows with MinGW the run command is often main.exe. -std=c++17 asks for C++17, which is what the StudyGrid editor uses.

What you need in every file

  • A .cpp name, not .txt.
  • Headers you actually use, usually <iostream> for this tutorial.
  • Exactly one main function in the program you run.
  • A semicolon at the end of each statement.

If g++ prints a wall of errors, start at the first error. Later messages are often fallout from that first missing semicolon or typo.

Download from a lesson

Every example has Download .cpp. Save the file, compile it with g++, and compare the result with the browser editor. Next: the syntax rules the compiler enforces.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.

Chemistry

Label before you weigh

Copper(II) sulfate pentahydrate is CuSO4·5H2O. The dots in the formula are water of crystallisation, not a product. If two bottles sit on the bench, the label is the only thing that stops you using the anhydrous salt by mistake.

A C++ file starts in main. The first useful line is often that label, printed so the rest of the session has a name.

Example

#include <iostream>
using namespace std;

int main() {
  cout << "Sample: CuSO4.5H2O" << endl;
  cout << "Ready to weigh." << endl;
  return 0;
}

Engineering

A board saying it booted

Embedded work is often still C++, even when the chip is small. After reset, the first human-readable line on a serial port is how you tell a dead board from a live one.

Nothing is measured yet. The point of the listing is the compile-run loop: a .cpp file, a main, stdout.

Example

#include <iostream>
using namespace std;

int main() {
  cout << "logger v1: ok" << endl;
  return 0;
}

FAQ: C++ Get Started

Common questions about this page.

What is the StudyGrid C++ tutorial?

The StudyGrid C++ tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, classes, the STL, templates, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run c++ get started examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c++ get started in this C++ C++ lesson (C++ Get Started).

Is the C++ editor the same as Try Python or Try HTML?

No. Try C++ compiles with g++ at /cpp/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. C++ lessons never open those editors.

Do I need to install a compiler to learn C++?

No. Open a chapter, click Try it in C++, and compile in the browser. You can also download a .cpp file and compile locally with g++.

Where should I start the C++ tutorial?

Start at C++ Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open C++ Examples, then templates, map, and lambdas. Use Next at the bottom of each chapter.

Is the C++ tutorial free?

Yes. The C++ workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.