5.2.1 · HinglishC++ Programming

C++ as superset of C — key additions

1,792 words8 min readRead in English

5.2.1 · Coding › C++ Programming


C++ exist karta hi kyun hai?

C mein add kyun karein? C hardware se baat karne ke liye brilliant hai lekin bade programs ko organise karne mein koi help nahi deta. Jab codebases bade hote gaye, programmers baar-baar same patterns haath se re-invent karte rahe:

  • data aur uspe kaam karne wale functions ko ek saath group karna → classes tak le gaya
  • generic containers jo kisi bhi type ke liye kaam karein → templates tak le gaya
  • return-code checking se zyaada safe error signalling → exceptions tak le gaya
  • runtime ki jagah compile time par bugs pakadna → stronger type checking tak le gaya

Toh C++ hai C + abstractions jo humans ko complexity manage karne deti hain, aise features ke liye (ideally) zero runtime cost ke saath jo tum use nahi karte — yahi famous "you don't pay for what you don't use" principle hai.


KYA add hua — bade buckets

Figure — C++ as superset of C — key additions

1. Classes & Object-Oriented Programming (OOP)

KYA: class/struct data + member functions ko bundle karta hai, public/private access control, constructors, destructors, inheritance, aur virtual polymorphism ke saath. KYUN: C mein tum ek struct Account* ko free functions jaise deposit(acc, x) mein pass karte ho. Koi bhi acc->balance ko directly corrupt karne se nahi ruk sakta. Ek class balance ko private banati hai, toh single legal path methods ke through hai → invariants protected rehte hain.

2. Templates (generic programming)

KYA: template<typename T> ek hi definition ko har type ke liye kaam karne deta hai. KYUN: C mein, ek "max" ya toh macro hona chahiye (#define MAX(a,b)..., unsafe) ya har type ke liye dobara likhna padta hai. Templates compile time par resolve hone wali type-safe, zero-overhead genericity dete hain.

3. References, new/delete, bool, default args, overloading

  • References (int& r = x;): ek alias — jaise ek pointer jo hamesha valid ho aur auto-dereference ho jata ho. KYUN: int* se zyaada clean pass-by-reference.
  • new/delete: type-aware allocation jo constructors/destructors call karta hai (malloc/free ke unlike).
  • Function overloading: same naam, alag parameter lists. KYUN: print_int, print_double ki jagah print(int) aur print(double).
  • Default arguments, bool ek real type ke roop mein, inline functions.

4. Namespaces

KYA: namespace net { ... } names ko clash avoid karne ke liye group karta hai. KYUN: C mein ek global namespace hai, toh do libraries jo dono init() define karti hain, collide karti hain. C++ net::init() vs gfx::init() deta hai.

5. STL + Exceptions + stronger typing

  • STL: ready-made vector, map, string, sort, iterators.
  • Exceptions: throw/try/catch error path ko happy path se alag karta hai.
  • Stronger type rules: jaise C++ implicit void*T* conversion forbid karta hai jo C allow karta hai.

KAISE har addition ek C pain-point se aata hai


Common mistakes (Steel-manned)


Active recall

Recall Answers cover karo — kya tum inhe reproduce kar sakte ho?
  • Wo paanch "buckets" name karo jo C++ C mein add karta hai. → OOP/classes, templates, references+new/delete+overloading, namespaces, STL+exceptions+stronger typing.
  • C++ near-superset kyun hai, true superset kyun nahi? → C++ stricter hai (no implicit void* cast, reserved keywords, etc.).
  • newmalloc ka ek concrete reason? → new constructors/destructors run karta hai.
  • Namespaces kya problem solve karte hain? → libraries mein name collisions.
Recall Feynman: ek 12-saal ke bachche ko explain karo

Socho C ek basic LEGO set hai: tum kuch bhi bana sakte ho, lekin har brick plain hai aur tumhe sab apne aap track karna padta hai. C++ wahi LEGO box hai plus kuch magic helpers: ek labelled box jo related bricks ek saath rakhta hai aur todhne nahi deta (classes), ek photocopier jo same shape ko kisi bhi colour mein bana deta hai (templates), aur name tags taaki do bacchon ke pieces mix na hon (namespaces). Tum phir bhi plain bricks bilkul pehle jaisi use kar sakte ho — helpers extra hain, replacements nahi.


Connections

  • Classes and Objects in C++
  • Templates and Generic Programming
  • Pointers vs References
  • new and delete vs malloc and free
  • Namespaces and the std namespace
  • The STL — vector, map, string
  • Exception Handling try-catch-throw
  • Compilation Model — C vs C++
C++ originally went by what name, reflecting its first major addition?
"C with Classes"
Define a near-superset and say why C++ is only a near-superset of C.
Agar A ke zyaadaatar valid programs B mein bhi compile hon; C++ stricter hai (jaise no implicit void*→T* conversion, zyaada reserved keywords) isliye kuch C programs fail ho jaate hain.
List the five buckets of C++ additions over C.
Classes/OOP, Templates, References+new/delete+overloading, Namespaces, STL+exceptions+stronger typing.
What is the key behavioural difference between new/delete and malloc/free?
new/delete constructors/destructors call karte hain; malloc/free nahi karte.
Why are templates safer than C macros for generic code like MAX?
Templates real functions hain: arguments ek baar evaluate aur type-check hote hain; macros double-evaluate kar sakte hain aur unme type safety nahi hoti.
What problem do namespaces solve that C cannot?
Name collisions — C mein ek single global namespace hai, isliye do libraries jo init() define karti hain wo clash karti hain; namespaces net::init() vs gfx::init() dete hain.
State the C++ design principle about feature cost.
"You don't pay for what you don't use" — jo features use nahi karte unka zero overhead.
What does a reference give you over a raw pointer?
Ek hamesha-valid, auto-dereferenced alias jo null ya reseat nahi ho sakta, pointer bugs ki ek class hata deta hai.
Give one C snippet that compiles in C but errors in C++.
int* p = malloc(4); — C++ mein void* se explicit cast chahiye.

Concept Map

is base of

weak at

motivates

adds

adds

adds

adds

enforces

gives

guided by

not 100% compatible

C language

C++ near-superset

Managing large programs

Classes and OOP

Templates

References new/delete namespaces

STL exceptions stronger types

Protected invariants private data

Type-safe zero-overhead generics

Pay only for what you use

Some C code breaks