5.2.6 · D1C++ Programming

Foundations — Constructors — default, parameterized, copy, delegating

1,644 words7 min readBack to topic

Before you can read a single line of the parent note, you need a vocabulary. Below is every symbol, keyword, and picture the topic quietly assumes. We build them in order — each one earns its place before the next arrives.


0. What even is an "object"?

Picture a cookie-cutter and cookies. The cutter is the class; each cookie is an object. The cutter has no dough in it — it just describes the shape. Every cookie is a separate lump of dough shaped by it.

Figure — Constructors — default, parameterized, copy, delegating

1. Members: the data inside x, y

In the picture above, the little boxes drawn inside each cookie are its members. Two different Point objects each have their own x and their own y — changing one cookie's box never touches another's.


2. int, int*, and the arrow to the heap

Figure — Constructors — default, parameterized, copy, delegating

Look at the two panels. On the left, an int is the value 5. On the right, an int* is an arrow: the box only stores where the numbers are, and the numbers themselves sit in a separate region called the heap (memory you ask for at runtime with new).


3. & — the reference

Imagine one house with two name-plates on the door: "12 Oak St" and "Grandma's place." Both names open the same door. A reference is the second name-plate — no new house is built.


4. const — read-only

Think of a museum exhibit behind glass: you can look (read) but not touch (write).


5. The member-initializer list : x(0), y(0)

Figure — Constructors — default, parameterized, copy, delegating

Follow the arrow of time in the figure. First the members are born straight into their given values (the green step). Then the body { } runs (the blue step). Writing x = 0; inside the body would instead let x be born as garbage first and then overwrite it — two steps where one would do. For deeper study see Member Initializer Lists.


6. Overloading — same name, many recipes

Point(), Point(int), and Point(int,int) are three different recipes that all answer to "Point." Handing over zero, one, or two numbers selects which runs.


7. = default and explicit — small keywords, big effects


The prerequisite map

class vs object

member data inside object

int value box

int pointer to heap

new and delete

reference the nickname

const read only

initializer list

overloading same name

default and explicit keywords

Constructors topic

Read the arrows as "is needed before." Everything funnels into the Constructors node — which is the parent topic. Once these foundations click, the parent note's deep-copy and delegation ideas, plus Rule of Three Five Zero and Move Semantics — move constructor, become natural next steps.


Equipment checklist

Self-test: can you answer each before revealing?

A class vs an object — which is the blueprint and which is the built thing?
The class is the blueprint; the object (instance) is one actual thing built from it.
What is a data member?
A variable that lives inside every object, with its own copy per object.
Difference between int x and int* data?
int x holds the value directly; int* data holds an address pointing to values stored elsewhere (the heap).
What must match every new int[n]?
Exactly one delete[] to return that heap memory.
What is a reference ClassName&?
A second name for the same object — no copy is made.
Why does the copy constructor take its argument by reference?
By value would call the copy constructor to make that value → infinite recursion; a reference avoids the copy.
What does const promise in const ClassName&?
That the source is only read, not modified — so you may copy from const and temporary objects too.
What does the member-initializer list do that the body {} does not?
It initializes members directly at birth, before the body runs — no wasteful default-then-assign.
In what order are members actually initialized?
In the order they are declared in the class, not the order written in the list.
What is overloading, and how do constructor flavours use it?
Same name, different argument lists; default/parameterized/copy are all overloads of the constructor.
What does = default do?
Asks the compiler to generate the standard version (e.g. re-enable the default constructor).
What does explicit prevent?
Silent implicit conversions like Meter m = 5;.