5.2.6 · HinglishC++ Programming

Constructors — default, parameterized, copy, delegating

1,917 words9 min readRead in English

5.2.6 · Coding › C++ Programming


WHAT hai ek constructor?


1. Default constructor


2. Parameterized constructor


3. Copy constructor


4. Delegating constructor (C++11)


Figure — Constructors — default, parameterized, copy, delegating

Operations ka order (HOW)


Flashcards

Constructor ko define karne wali teen properties kya hain?
Class ke same naam ka, koi return type nahi, object creation par implicitly call hota hai.
Compiler kab aapke liye default constructor banana band kar deta hai?
Jaise hi aap koi bhi doosra constructor declare karo; use = default se wapas laao.
Copy constructor apna parameter reference se kyun leta hai?
By value lena khud copy constructor ko invoke karta → infinite recursion.
ClassName(const ClassName&) mein const kyun?
Aap sirf source read karte ho, aur yeh const objects aur temporaries se copy karne deta hai.
Shallow vs deep copy problem?
Default copy pointers copy karta hai, toh do objects memory share karte hain → destructors mein double free; deep copy alag buffer allocate karta hai.
Rule of Three batao.
Agar aap destructor, copy constructor, ya copy assignment define karo, toh probably teeno chahiye.
Delegating constructor kya hai aur uski ek restriction?
Ek constructor jo init list mein same class ke doosre constructor ko call kare; woh call list mein SIRF wahi cheez ho sakti hai (koi extra member init nahi).
Members kis order mein initialize hote hain?
Jis order mein class mein DECLARE kiye gaye hain, initializer list ke order mein nahi.
explicit single-arg constructor par kya rokta hai?
Implicit conversions jaise Meter m = 5;.
Teen situations jo copy constructor fire karti hain?
Copy-initialization (T b=a), pass-by-value, return-by-value.

Recall Feynman: 12-saal ke bachche ko samjhao

Ek toy factory imagine karo. Jab bhi koi naya toy line se nikalata hai, ek worker use set up karta hai taaki woh khelne ke liye ready ho. Woh worker hi constructor hai.

  • Default: seedha sada toy banata hai standard parts se jab aap kuch special nahi bolte.
  • Parameterized: aap worker ko colour aur size dete ho, aur woh wahi toy banata hai.
  • Copy: aap kisi bane hue toy ki taraf point karte ho aur bolte ho "mujhe exactly aisa ek aur banao." Agar toy ka apna toy-dog hai, toh ek lazy copy dono toys ko ek hi dog deta hai (woh uss par ladte hain!). Deep copy naye toy ko uska apna dog deta hai.
  • Delegating: har worker ko poori setup sikhane ke bajaye, ek master worker jaanta hai, aur baaki bolte hain "hey master, tum karo."

Connections

  • Destructors and RAII — constructors ke saath pair karta hai resource lifetime manage karne ke liye.
  • Copy Assignment Operator — copy constructor ka = wala sibling.
  • Move Semantics — move constructor — C++11 ka "copy ki jagah steal karo" wala cousin.
  • Member Initializer Lists — efficient construction ka engine.
  • Rule of Three Five Zero — special members kab likhen/chhodein.
  • explicit keyword and implicit conversions

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