Module 5 · Classes and Objects — topic 2 of 5
Constructors
Constructors initialise objects. Learn default, parameterised and delegating constructors, member initialiser lists, explicit, and the copy constructor.
Learning objectives
- Write default and parameterised constructors with initialiser lists
- Use explicit to prevent unwanted conversions
- Explain when the copy constructor runs
Definition
A constructor is a special member function with the class's name and no return type that runs automatically when an object is created, establishing its initial state.
Introduction
Without a constructor an object's members may hold garbage. A constructor guarantees every object starts valid: a Date is always a real date, a Fraction never has a zero denominator.
Why it matters
Constructors establish class invariants — the rules that must always be true for an object.
Initialiser lists are required for const members, references and members without default constructors.
Detailed explanation
Default constructor: takes no arguments. If you write no constructors at all, the compiler generates one (members of class type are default-constructed; built-ins keep their default member initialisers or are left uninitialised).
Member initialiser list: Point(int x, int y) : x_(x), y_(y) {} initialises members directly instead of assigning afterwards. Members are initialised in declaration order, not list order.
explicit stops a single-argument constructor from being used for implicit conversions (Fraction f = 3;).
Copy constructor T(const T&) creates an object as a copy; it runs when passing by value, returning by value (unless elided) or writing T b = a;.
Delegating constructor: Fraction() : Fraction(0, 1) {} reuses another constructor.
Syntax
- Constructors have no return type.
- Use
= defaultto request the compiler-generated version explicitly. - Throw an exception if arguments are invalid.
Code examples
Default and parameterised
Untitled (0 pages) Clean Code (464 pages)
Initialisation order follows declaration order
5 10
Compilers warn with -Wreorder when the list order differs from declaration order.
More examples
A validating Fraction class
-3/4 5/1 0/1 -3/4 copies: 1 error: denominator is zero
Step-by-step explanation
Fraction(6, -8)moves the sign to the numerator and reduces by the gcd 2: −3/4.Fraction(5)delegates to (5, 1);Fraction()to (0, 1).Fraction d = a;calls the user-written copy constructor, incrementingcopies.- A zero denominator throws, so an invalid Fraction can never exist.
Expected output
Running “A validating Fraction class” prints exactly:
Practical applications
std::string("text")andstd::vector<int>(10, 0)are constructor calls.- Database connection classes open the connection in the constructor.
- Value types like Date and Money validate in their constructors.
Best practices
- Initialise every member in the initialiser list or with a default member initialiser.
- Mark single-argument constructors
explicit. - Throw from a constructor when arguments are invalid.
Common mistakes
Assigning instead of initialising const members
Avoid
Prefer
const and reference members must be initialised in the initialiser list.
Most vexing parse
Avoid
Prefer
Empty parentheses declare a function returning Book; use no parentheses or braces.
Important notes
- If you define any constructor, the compiler no longer generates the default constructor; add
= defaultif you need it. - Copy elision means
T x = make();usually calls no copy constructor at all (guaranteed in C++17 for prvalues).
Key takeaways
- Constructors initialise objects and enforce invariants.
- Use initialiser lists; members initialise in declaration order.
explicitprevents implicit conversions; copy constructors copy.
Try it yourself
- 1Write a
Dateclass whose constructor rejects invalid dates such as 31/02/2026. - 2Add a constructor to
Rectanglethat creates a square from one side length, using delegation.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
9 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhen does a constructor run?
- BeginnerWhat is printed?
- BeginnerWhy is `Book b();` a problem?