C++ Tutorial

C++ Introduction

C++ is a compiled language. You write source, a compiler builds a program, then the program runs.

What is C++?

C++ is a general-purpose programming language used for systems, games, finance, and tools that need to be fast. It started as C with classes. Today it is still close to the hardware: you choose types, you see when memory is allocated, and a compiler turns your file into a program before anything runs.

That compile step is the main difference from Python on StudyGrid. Python reads a file and executes it. C++ must build first. If the types do not match, you get an error instead of a running program.

A first program

This is a complete C++ program. Copy it into the C++ editor and change the message.

Example

#include <iostream>
using namespace std;

int main() {
  cout << "Hello, C++" << endl;
  return 0;
}

Click Try it in C++ under the example. That opens /cpp/try — a compile-and-run editor.

A second example: numbers

cout can print integers and results of expressions. This program adds two values and prints the sum. Change a or b and compile again.

Example

#include <iostream>
using namespace std;

int main() {
  int a = 7;
  int b = 5;
  cout << a << " + " << b << " = " << (a + b) << endl;
  return 0;
}

What each line does

  • #include <iostream> brings in input and output (cout, cin).
  • using namespace std; lets you write cout instead of std::cout.
  • int main() is where the program starts. Every program needs a main.
  • cout << ... writes text to the console.
  • return 0; tells the operating system the program finished normally.

Compiled, not interpreted

  1. You save a .cpp file.
  2. A compiler such as g++ checks types and builds an executable.
  3. You run that program. It prints output or reads input.

On StudyGrid the C++ editor does steps 2 and 3 for you. Locally you would typeg++ -std=c++17 main.cpp -o main and then run ./main.

C++ vs Python vs HTML on StudyGrid

PythonHTMLC++
Dashboard/tutorial/html/cpp
Editor/try — prints and plots/html/try — live page/cpp/try — g++ compile
ResultOutput text and chartsA rendered documentStdout and compiler messages
File.py.html.cpp

What you will cover

  • Syntax, output, variables, and types
  • Conditions, loops, arrays, and pointers
  • Functions, overloading, and recursion
  • Classes, inheritance, and polymorphism
  • Files, exceptions, vectors, and memory
  • Examples, templates, STL, map, set, algorithms, and lambdas

Next: how to compile a file locally, then the rules of C++ syntax.

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.

Physics

Current from an ammeter

Current is charge per second. The SI unit is the ampere: one coulomb of charge passing a point in one second. A first program does not have to compute that. It only has to print a reading a person already took.

Lab notebooks start the same way. Write the quantity, the number, and the unit on one line. If the unit is missing, the 0.45 is useless.

I = Q / t

Example

#include <iostream>
using namespace std;

int main() {
  cout << "Current I = 0.45 A" << endl;
  return 0;
}

Maths

A 3-4-5 triangle

A right triangle whose legs are 3 and 4 has hypotenuse 5. That is Pythagoras, not a coincidence: 9 + 16 = 25. Printers and carpenters still use the triple to square a corner.

Printing the three numbers is a Hello World with a reason to exist. Later chapters compute the 5 from the 3 and the 4. Here you only name them.

a² + b² = c²

Example

#include <iostream>
using namespace std;

int main() {
  cout << "Right triangle sides: 3, 4, 5" << endl;
  return 0;
}

FAQ: C++ Introduction

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++ introduction 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++ introduction in this C++ C++ lesson (C++ Introduction).

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.