14.14 — Converting constructors, explicit, and delete

By default, C++ will treat any constructor as an implicit conversion operator. Consider the following case: #include <cassert> #include <iostream> class Fraction { private: int m_numerator; int m_denominator; public: // Default constructor Fraction(int numerator = 0, int denominator = 1) : m_numerator(numerator), m_denominator(denominator) { assert(denominator != 0); } // Copy …

14.13 — Copy initialization

Consider the following line of code: int x = 5; This statement uses copy initialization to initialize newly created integer variable x to the value of 5. However, classes are a little more complicated, since they use constructors for initialization. This lesson will examine topics related to copy initialization for …

2.12 — Header guards

In lesson , we noted that a variable or function identifier can only have one definition (the one definition rule). Thus, a program that defines a variable identifier more than once will cause a compile error: int main() { int x; // this is a definition for variable x int …