15.1 — The hidden “this” pointer and member function chaining

One of the questions about classes that new programmers often ask is, “When a member function is called, how does C++ keep track of which object it was called on?”. First, let’s define a simple class to work with. This class encapsulates an integer value, and provides some access functions …

19.3 — Destructors

A destructor is another special kind of class member function that is executed when an object of that class is destroyed. Whereas constructors are designed to initialize a class, destructors are designed to help clean up. When an object goes out of scope normally, or a dynamically allocated object is …

14.6 — Access functions

In previous lesson , we discussed the public and private access levels. As a reminder, classes typically make their data members private, and private members can not be directly accessed by the public. Consider the following Date class: #include <iostream> class Date { private: int m_year{ 2020 }; int m_month{ …

7.2 — User-defined namespaces and the scope resolution operator

In lesson , we introduced the concept of naming collisions and namespaces. As a reminder, a naming collision occurs when two identical identifiers are introduced into the same scope, and the compiler can’t disambiguate which one to use. When this happens, compiler or linker will produce an error because they …

20.3 — Recursion

A recursive function in C++ is a function that calls itself. Here is an example of a poorly-written recursive function: #include <iostream> void countDown(int count) { std::cout << “push ” << count << ‘\n’; countDown(count-1); // countDown() calls itself recursively } int main() { countDown(5); return 0; } When countDown(5) …

20.2 — The stack and the heap

The memory that a program uses is typically divided into a few different areas, called segments: The code segment (also called a text segment), where the compiled program sits in memory. The code segment is typically read-only. The bss segment (also called the uninitialized data segment), where zero-initialized global and …