Learn C++ programming with examples, variables, loops, functions and vectors.
Weeks 1–2 C++ foundations
Concept & theory
Compiler, main(), variables, data types, input/output, operators and conditions. A compiler translates C++ source into an executable. int stores whole numbers; cin reads input; cout prints output.
Example code
#include <iostream>
using namespace std;
int main(){
int age; cin >> age;
if(age >= 18) cout << "Eligible";
else cout << "Not eligible";
return 0;
}
Code explained
Line 1 imports input/output. main is the entry point. age is an integer. cin reads it; if checks a Boolean condition; cout displays the matching message.
Try it yourself
Read two marks and print pass if both are at least 40. Test 39/90 and 40/40.
Weeks 3–4 Loops, functions and vectors
Concept & theory
for and while repeat operations; functions group reusable logic; vector stores a dynamically sized sequence. A loop must have a termination condition.
Example code
#include <iostream>
#include <vector>
using namespace std;
int sum(vector<int> a){int total=0; for(int x:a) total+=x; return total;}
int main(){cout << sum({2,4,6});}
Code explained
sum receives a vector by value, initializes total, iterates through elements, accumulates, and returns 12. Passing by const reference avoids copying large vectors.
Try it yourself
Write a function returning the largest number; test an empty vector safely.