C++ logo

C++ Beginner

Fast, strongly typed, and system-level — the backbone of high-performance software.

1 Structure & Output

C++ is a compiled, strongly-typed language. Every execution begins inside the main() function. Statements end with a semicolon, and output uses std::cout.

cpp · main.cpp
#include <iostream>

// This is a single-line comment

/* This is a 
   multi-line comment */

int main() {
    std::cout << "Hello, World!" << std::endl;
    
    return 0;
}
Compile your source file via terminal using a compiler like GCC: g++ main.cpp -o main, then run the executable with ./main.

2 Variables & Data Types

C++ is statically typed: you must explicitly declare the data type of a variable before using it. This type cannot change at runtime.

int Integers 42, -7, 0
double Floating-point 3.1415, -0.5
char Single character 'A', 'z', '9'
bool True or False true, false
void Valueless type Used in functions
auto Type deduction auto x = 5;
cpp · variables.cpp
int    age    = 25;
double height = 1.68;
char   grade  = 'A';       // single quotes for char
bool   active = true;
const  int DAYS_IN_WEEK = 7; // read-only constant

// C++11 Type deduction
auto score = 98.5;         // deduced as double

3 Operators

C++ uses standard symbols for mathematical logic, assignments, and side-by-side value comparisons.

cpp · operators.cpp
// Arithmetic
int sum = 10 + 3;        // 13
int mod = 10 % 3;        // 1  — modulo

// Integer division pitfall
double result = 10 / 3;    // → evaluates to 3.0 (int truncation)
double fixed  = 10.0 / 3;  // → evaluates to 3.33333...

// Comparisons
bool isEqual  = (5 == 5);  // true
bool isUnique = (5 != 3);  // true

// Logical
bool check = (true && false); // false — AND
bool match = (true || false); // true  — OR

4 Strings & Input

To use text strings safely instead of old C-style char arrays, include the <string> header. Reading input from the user can be done via std::cin.

cpp · strings.cpp
#include <string>
#include <iostream>

std::string greeting = "Hello, C++!";
std::cout << greeting.length() << std::endl; // 10

std::string name;
std::cout << "Enter your first name: ";
std::cin >> name; // reads until the first space

// To read a full line of text including spaces:
std::string fullName;
std::getline(std::cin, fullName);

5 Conditionals

C++ uses standard scoping patterns via blocks nested inside if, else if, and else frameworks alongside structural switch setups.

cpp · conditionals.cpp
int score = 72;

if (score >= 90) {
    std::cout << "Grade: A";
} else if (score >= 75) {
    std::cout << "Grade: B";
} else if (score >= 60) {
    std::cout << "Grade: C";   // ← this runs
} else {
    std::cout << "Grade: F";
}

// Inline Ternary Operator
std::string status = (score >= 60) ? "Pass" : "Fail";

6 Loops

Perform repetitive computational logic blocks safely using standard for, while, and range-based for loops.

cpp · loops.cpp
// Classic incremental for loop
for (int i = 0; i < 5; i++) {
    std::cout << i << " "; // 0 1 2 3 4
}

// Standard conditional while loop
int count = 0;
while (count < 3) {
    std::cout << count << "\n";
    count++;
}

7 Arrays & Vectors

Fixed-size built-in arrays require bounds defined at compile-time. Dynamic resizing lists rely on std::vector from the Standard Template Library (STL).

cpp · collections.cpp
#include <vector>

// Fixed Array
int nums[4] = {10, 20, 30, 40};

// Dynamic STL Vector
std::vector<int> v = {1, 2, 3};
v.push_back(4); // appends 4 to the end
v.pop_back();  // pops out the last element

// Range-based for loop (C++11)
for (int element : v) {
    std::cout << element << " ";
}

8 Functions & References

Functions require signature declarations detailing returned data types. To pass parameters without expensive memory copying, use references (&).

cpp · functions.cpp
#include <iostream>

// Value passing configuration (copies data)
int square(int x) {
    return x * x;
}

// Reference passing configuration (modifies original variable)
void increment(int &num) {
    num++;
}

int main() {
    int val = 5;
    increment(val); 
    std::cout << val; // outputs 6
    return 0;
}
Now that you have learned the foundations of procedural syntax, explore object-oriented architecture patterns like Classes, Pointers, and manual Memory Management.