C++
The power of C with OOP and the Standard Library
The language of universities and programming contests: variables, loops and functions, pointers and references, classes and inheritance, then the STL: vector, map and algorithms.
- 42 lessons
- 7 chapters
- Arabic & English
Your progress
What's inside
Lessons go from the basics to advanced topics. Read them in order, or jump to what you need.
Chapter 1
Getting Started
- 1C++ IntroductionMeet C++: what it is, where it came from, where it is used today, and why it is one of the best languages for computer science and engineering students.
- 2C++ Get StartedSet up your environment: install the g++ compiler, pick an IDE, write your first program in main.cpp, then compile and run it from the terminal.
- 3C++ SyntaxLearn the structure behind every C++ program: the #include line, the std namespace, the main() function, statements, and semicolons.
- 4C++ Output (Print Text)Print text and numbers with cout and the << operator, start new lines with \n or endl, and learn the difference between them and the escape sequences.
- 5C++ CommentsUse comments to explain your code and make it easier to read, or to switch part of it off while testing. Learn // and /* */ and when to use each.
Chapter 2
Basics
- 6C++ VariablesStore data in variables: declare them, assign and change their values, and print them. Learn the naming rules, const constants, and the auto keyword.
- 7C++ Data TypesLearn C++'s basic data types, their sizes and ranges, the sizeof operator, characters and their codes, type conversion, and the integer-division trap.
- 8C++ User InputRead numbers and words from the keyboard with cin and the >> operator, read whole lines with getline(), and avoid the classic trap when mixing the two.
- 9C++ OperatorsPerform operations on variables and values with arithmetic, assignment, comparison, and logical operators, and learn increment/decrement and operator precedence.
- 10C++ StringsWork with text using the string type: join strings, get their length, read and change characters, use handy methods like substr(), find(), and replace(), and convert between strings and numbers.
- 11C++ MathUse max() and min() and the <cmath> functions such as sqrt(), pow(), round(), ceil(), and floor(), and format decimals with <iomanip>.
- 12C++ BooleansLearn the bool type and its two values, true and false, print them as words with boolalpha, and use boolean expressions to make decisions.
Chapter 3
Control Flow
- 13C++ If...ElseMake your program take decisions with if, else, and else if, and learn nested conditions, the ternary operator ?:, and how to check user input.
- 14C++ SwitchPick one of many code blocks with switch, case, break, and default, understand fall-through, and learn when to prefer switch over if.
- 15C++ While LoopRepeat code while a condition is true with while, run it at least once with do...while, and read and validate an unknown number of inputs.
- 16C++ For LoopWhen you know how many times to repeat, use the for loop. Learn its three parts, nested loops, and the range-based for loop that visits every element directly.
- 17C++ Break and ContinueExit a loop early with break, skip one round with continue, and learn how they behave in while loops and nested loops.
Chapter 4
Arrays & Memory
- 18C++ ArraysStore multiple values of the same type in one variable: create arrays, read and change elements by index, loop through them, and find how many elements they hold.
- 19C++ Multi-Dimensional ArraysStore data as a table of rows and columns with two-dimensional arrays: declare them, read and change elements, and loop through them with nested loops.
- 20C++ ReferencesA reference is a second name for an existing variable. Learn to declare references with &, how they differ from copies, how to use them in loops, and const references.
- 21C++ PointersA pointer is a variable that stores the memory address of another variable. Learn to declare pointers, reach values through them with *, use nullptr, and work with arrays through pointers.
- 22C++ Dynamic MemoryAllocate memory while the program runs with new and release it with delete and delete[]. Learn the stack and the heap, memory leaks and dangling pointers, and why modern C++ prefers smart pointers and vector.
Chapter 5
Functions
- 23C++ FunctionsCreate and call your own functions, return values with return, and understand function declarations vs. definitions and the scope of local and global variables.
- 24C++ Function ParametersPass data to functions through parameters. Understand the key difference between passing by value, by reference, and by const reference, and how to pass arrays to functions.
- 25C++ Default ArgumentsGive function parameters default values that are used when the caller doesn't pass one, and learn the rules: they start from the right and are written once, in the declaration.
- 26C++ Function OverloadingDefine several functions with the same name as long as their parameters differ in number or type. Learn how the compiler picks the right one, what ambiguity means, and why the return type alone isn't enough.
- 27C++ RecursionMake a function call itself to solve a big problem by breaking it into smaller problems of the same kind. Learn the base case, classic examples (factorial, Fibonacci, strings), and when loops are the better choice.
Chapter 6
Object-Oriented Programming
- 28C++ Classes and ObjectsDiscover object-oriented programming: create classes with attributes and methods, make objects from them, use . to access members, :: to define methods outside the class, and -> with pointers.
- 29C++ ConstructorsInitialize objects automatically when they're created with constructors. Use member initializer lists and overloaded constructors, and meet destructors, the object lifetime, and the RAII idea.
- 30C++ Access SpecifiersControl access to class members with public, private, and protected. Apply encapsulation by hiding data behind getters and setters that validate values, and use const member functions.
- 31C++ InheritanceCreate new classes that inherit the attributes and methods of an existing class with :. Use protected, call the parent's constructor, and learn multilevel inheritance and redefining methods.
- 32C++ PolymorphismMake the same call behave differently depending on the real object type, using virtual functions and override. Create abstract classes with pure virtual functions, and learn why virtual destructors matter.
- 33C++ Operator OverloadingGive operators like +, ==, and << a meaning for your own classes, so you can naturally write p1 + p2 and cout << p, and learn the rules and best practices.
Chapter 7
STL & More
- 34C++ VectorsUse vector, the smart array that resizes itself: create one, add and remove elements, read and change them, loop through them, pass vectors to functions, and build 2D vectors.
- 35C++ MapsStore data as key/value pairs with the ordered map and the fast unordered_map: add, read, and change entries, check whether a key exists, erase, and loop through entries.
- 36C++ SetsStore unique, automatically sorted values with set: insert, search, and erase; sort in descending order; remove duplicates from vectors; and meet unordered_set and multiset.
- 37C++ Iterators and AlgorithmsMove through containers with iterators, use ready-made STL algorithms such as sort(), find(), count(), and accumulate(), and write your own criteria with lambda expressions.
- 38C++ TemplatesWrite generic code that works with any type using templates: function templates, class templates, how the compiler deduces types, and why the whole STL is built on templates.
- 39C++ ExceptionsHandle errors at run time with try, throw, and catch; use the standard exception classes such as runtime_error, invalid_argument, and out_of_range; and catch them by const reference.
- 40C++ FilesWrite data to files and read it back with the <fstream> library: the ofstream, ifstream, and fstream classes, open modes such as append (ios::app), and reading line by line or value by value.
- 41C++ Smart PointersLet C++ manage dynamic memory for you: use unique_ptr for exclusive ownership and shared_ptr for shared ownership, create them with make_unique and make_shared, transfer ownership with move, and meet weak_ptr.
- 42C++ Next StepsReview what you've learned in a mini project that combines structs, vectors, maps, and algorithms — then discover the road ahead: data structures and algorithms, competitive programming, modern C++, and professional tools.