5.2.6C++ Programming

Constructors — default, parameterized, copy, delegating

2,120 words10 min readdifficulty · medium

WHAT is a constructor?


1. Default constructor


2. Parameterized constructor


3. Copy constructor


4. Delegating constructor (C++11)


Figure — Constructors — default, parameterized, copy, delegating

Order of operations (the HOW)


Flashcards

What three properties define a constructor?
Same name as class, no return type, called implicitly at object creation.
When does the compiler stop generating a default constructor for you?
As soon as you declare any other constructor; restore it with = default.
Why must the copy constructor take its parameter by reference?
By value would itself invoke the copy constructor → infinite recursion.
Why const in ClassName(const ClassName&)?
You only read the source, and it lets you copy from const objects and temporaries.
Shallow vs deep copy problem?
Default copy copies pointers, so two objects share memory → double free in destructors; deep copy allocates a separate buffer.
State the Rule of Three.
If you define a destructor, copy constructor, or copy assignment, you probably need all three.
What is a delegating constructor and its one restriction?
A constructor that calls another of the same class in its init list; that call must be the ONLY thing in the list (no extra member inits).
In what order are members initialized?
In the order they are DECLARED in the class, not the order in the initializer list.
What does explicit on a single-arg constructor prevent?
Implicit conversions like Meter m = 5;.
Three situations that fire the copy constructor?
Copy-initialization (T b=a), pass-by-value, return-by-value.

Recall Feynman: explain to a 12-year-old

Imagine a toy factory. Every time a new toy comes off the line, a worker must set it up so it's ready to play with. That worker is the constructor.

  • Default: builds a plain toy with standard parts when you don't say anything special.
  • Parameterized: you hand the worker a colour and size, and they build that toy.
  • Copy: you point at a finished toy and say "make me one exactly like that." If the toy has its own toy-dog, a lazy copy gives both toys the same dog (they fight over it!). A deep copy gives the new toy its own dog.
  • Delegating: instead of teaching every worker the full setup, one master worker knows it, and the others just say "hey master, you do it."

Connections

Concept Map

flavour

flavour

flavour

flavour

born from nothing

synthesized only if

declaring any ctor

restore with

single-arg risk

blocked by

copies from

reuses logic of

prefer

prefer

required for

Constructor: same name, no return type, implicit

Default ctor

Parameterized ctor

Copy ctor

Delegating ctor

Enables Point p and arrays

No other ctor declared

Point&(&) = default

Implicit conversion

explicit keyword

Existing object

Member initializer list

const and reference members

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Constructor ek special function hai jo object ke "janam" ke time automatically chalta hai. Iska kaam hai object ko ek valid, ready-to-use state mein le aana taaki koi bhi half-built object ko use na kar paaye. Iska naam class ke naam jaisa hota hai aur iska koi return type nahi hota.

Char flavours yaad rakho. Default — bina argument ke (Point p;), zero/standard values set karta hai. Parameterized — tum values do (Point p(3,4)), wahi set hoti hain. Copy — kisi existing object se naya clone banata hai, signature Point(const Point&). Yahan & zaroori hai warna by-value lene se copy constructor khud ko hi call karta rahega — infinite recursion! Aur const isliye taaki source ko sirf padho. Delegating (C++11) — ek constructor doosre constructor ko init-list mein call karta hai, code repeat na karna pade.

Sabse important trap: jaise hi tum koi constructor likhte ho, compiler free wala default constructor dena band kar deta hai — wapas chahiye to Point() = default; likho. Doosra bada trap: agar class koi raw pointer/heap memory own karti hai, to default (shallow) copy do objects ko same memory dega aur dono destructors usko delete karenge — double free crash. Solution: apna copy constructor likho jo deep copy kare (naya buffer allocate karke). Yahi hai Rule of Three — destructor likha to copy constructor aur copy assignment bhi chahiye.

Last cheez: members hamesha class mein declare kiye gaye order mein initialize hote hain, init-list ke order mein nahi. Isliye list ka order declaration ke order se match rakho, warna uninitialized member use ho sakta hai. Ye chhoti chhoti baatein hi exam aur real bugs dono mein 80% kaam karti hain.

Go deeper — visual, from zero

Test yourself — C++ Programming

Connections