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 writecoutinstead ofstd::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
- You save a
.cppfile. - A compiler such as g++ checks types and builds an executable.
- 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
| Python | HTML | C++ | |
|---|---|---|---|
| Dashboard | /tutorial | /html | /cpp |
| Editor | /try — prints and plots | /html/try — live page | /cpp/try — g++ compile |
| Result | Output text and charts | A rendered document | Stdout 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;
}