Learn C++ from scratch
Hello! Welcome
Today we are going to learn C++ from scratch
What is C++?
C++ is a high-level programming language that allows you to control many details of your program.
It’s ideal for those who want to understand how programs work from the inside
It’s widely used in high-performance applications, game development, and embedded systems. The possibilities are immense!
Setting up the environment
Before you start programming in C++, you will need to download and install a development environment.
You can
- Download an IDE like Visual Studio
- Simply use VS Code with a compiler like g++.
How does C++ work?
Now you write your code in C++. To run it, you need to compile it. For example like this:
g++ program.cpp -o program
The compiler translates your code into a language that the computer understands—your first program!
You’re doing great!
Now let’s talk about C++ syntax
Variables
Variables in C++ allow you to store data (like numbers, text, or boolean values).
int age = 25;
int total = age + 25;
C++ lets you perform mathematical operations. For example, adding, subtracting, multiplying, dividing—and much more!
Conditionals
With conditionals in C++, you can make the program make decisions. We use if, else if, and else to execute code only if a condition is true.
if (age > 18) {
cout << "You are an adult";
}
Loops
Loops in C++ allow you to repeat actions. Use for, while, or do-while to repeat tasks several times until a condition is met.
for (int i = 0; i < 10; i++) {
cout << i;
}
Functions
Functions are blocks of code that perform specific tasks.
int sum(int a, int b) {
return a + b;
}
They allow you to organize and reuse your code easily.
Collections
Collections are structures that allow you to store multiple values. The simplest one is an array.
int numbers[5] = {1, 2, 3, 4, 5};
vector<int> vec = {1, 2, 3, 4, 5};
In C++, we also have vectors, which are more flexible because they can change size dynamically.
You’re almost there!
We just need to see how to program your Arduino
Classes and objects
C++ is an object-oriented language. This means you can create classes
class Person {
public:
string name;
void greet() { cout << "Hello, " << name; }
};
These represent real-world objects, with properties and behaviors.
Pointers and references
In C++, pointers allow you to work directly with memory.
A pointer is a variable that stores the memory address of another variable. References are aliases for variables, and both concepts are essential in memory management.
int* ptr = &age;
cout << *ptr; // Prints the value of 'age'
Use them with care!
Libraries
A library is a set of predefined functions and classes that you can use in your programs. You can include them in your project like this:
#include <vector>
using namespace std;
For example, the standard library (std) contains essential functions and classes for the language (like data structures, mathematical functions, and string handling)
There are a lot of libraries and functionalities!
Well done!
Now you know the basics of C++! Keep practicing and learning.