C++ fundamentals
You learn C++, a fast and widely used language built on C, from scratch, compiling with g++ and writing small programs with the standard library.
Level 101 · 19 lessons · 18 practice labs
Lessons
Getting started
- A first program with g++: You compile a C++ file with g++, run it and print to the screen with iostream.
- Reading compiler error messages
- Variables, types and auto: You use the basic types, constants and type deduction with auto, and learn how initialisation styles differ.
- Operators and precedence: You use arithmetic, comparison and logical operators and know traps such as integer division.
- Input and output with cin and cout: You read data from the user, print it formatted and catch bad input through the state of cin.
- Text handling with std::string: You concatenate, search and slice with the self-managing string class and escape the overrun risk of C strings.
- Short task: a command-line calculator
Control flow and functions
- Conditions and loops: You control flow with if, switch, for, while and the range-based for.
- Functions, overloading and default arguments: You write functions with the same name and different parameters and shorten calls with default arguments.
- References and const: You pass large objects to functions by const reference without copying them.
- Arrays and std::vector: You use the growable vector and learn the difference between bounds-checked at() and unchecked [] access.
- Namespaces and header files: You split code into header and source files and prevent name clashes with namespaces.
- Short task: a menu-driven notebook
Data, errors and files
- Grouping data with struct and enum class: You gather related fields in a struct and define safe sets of choices with enum class.
- Error handling: try, catch and throw: You throw and catch errors and use the standard exception classes and your own error type.
- File streams: ifstream and ofstream: You read and write files with streams, check for files that fail to open and let the stream close itself.
- Parsing lines with stringstream
- Short task: add up the amounts in a CSV file
Capstone
- Capstone: a log-file summary program: You build a program that reads a log, counts events with vector and map, catches errors and prints a summary.
Practice labs
- Calculating fuel cost: Write a program that reads distance, consumption and price per litre with cin and prints the trip cost to two decimals.
- Fixing an average broken by integer division: Find the integer division and the wrong auto-deduced type in a program that always rounds the mark average down, fix them and pass the tests.
- Repairing a menu that hangs on bad input: In a menu that loops forever when a letter is typed instead of a number, clear the stream error state, discard the rest of the input and ask again.
- Extracting initials from a full name: Write a function that produces upper-case initials from a full name with std::string functions and handles multiple spaces correctly.
- Parsing a key=value line: Split settings lines into key and value with stringstream, trim leading and trailing spaces and skip comment lines.
- Repairing a replace-all function: Fix a replace function that loops forever when the new text contains the search text and pass the tests with random strings.
- Finding the days of a month with switch: Write a function with switch that returns the number of days for a year and month, applying the leap-year rule correctly.
- Writing area calculations with overloading: Write overloaded area functions of the same name for square, rectangle and circle and fix the usage that gives an ambiguous-call error.
- Turning needless copies into const references: Convert functions that take large vectors by value to const references, separate those that must modify and pass the copy-counter test.
- Finding the smallest and largest measurement: Write a function that returns the minimum, maximum and their difference for a vector of temperature readings and handles an empty vector.
- Solving a multiple-definition error with headers: Split helpers from a single file into a header and a source file, add an include guard and clear the linker's multiple-definition error.
- Repairing an erase-in-loop bug: Fix a stock clean-up loop that erases while iterating by index and skips some records, passing the tests with consecutive removals.
- Keeping order status in an enum class: Convert an order status kept as a string to an enum class and write the function that turns the status into text.
- Summing CSV amounts while skipping bad rows: Sum the amount column of Turnalı Perakende's sales file, counting rows that are not numbers and reporting them with line numbers.
- Preventing a half-written file on an exception: Rework an exporter that leaves corrupt output when an exception occurs mid-write so it writes to a temporary file and renames at the end, passing the failure test.
- Inserting into a sorted vector in place: Write a function that inserts each new value at the right position in a vector kept sorted, reserving capacity up front and skipping duplicates.
- Checking the open before writing a file: Write a list of measurements line by line to a file with ofstream, giving a clear message and exiting with a non-zero code if the file cannot be opened.
- Resolving a circular header dependency: Fix a project that fails to compile because two headers include each other by using include guards and forward declarations, so each header compiles on its own.
About this course
You learn C++, a fast and widely used language built on C, from scratch, compiling with g++ and writing small programs with the standard library.
What you will learn
- Getting started
- Control flow and functions
- Data, errors and files
- Capstone
Prerequisites
No prior knowledge is needed; the course starts from zero.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at beginner level; every term is explained where it first appears.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.