Theory
वह program जो ख़ुद से बड़ा हो गया
BCA104 में आपने C में marks program बनाया था: कुछ functions, कुछ global variables, बस।
अब कल्पना कीजिए college canteen आपसे till software माँगता है: menu items, prices, member discounts, daily totals। आप इसे C-style में लिखते हैं: एक global total, global arrays, 14 functions जो सब उन्हें छूते हैं।
फिर एक function ग़लत moment पर total update कर देता है। शाम 3 बजे के बाद हर bill ग़लत है, और आप बता नहीं सकते 14 में से किस function ने यह किया।
Theory
Open kitchen बनाम stations
एक procedural program एक open kitchen है: हर cook हर shelf तक पहुँच सकता है, तो एक लापरवाह हाथ सबके लिए दाल बिगाड़ देता है।
एक OOP program counters वाले stations की तरह चलता है: tea station अपनी kettle रखता है, billing station अपना cash। आप उनकी चीज़ें नहीं पकड़ते, आप counter पर एक request रखते हैं। हर station अपने ingredients खुद protect करता है।
Theory
दोनों styles, formally
Procedural programming एक program को procedures (functions) की एक sequence के रूप में structure करती है जो data पर operate करते हैं, और data आमतौर पर shared या global होता है। C इसी तरह काम करता है।
Object-Oriented Programming (OOP) एक program को objects के एक collection के रूप में structure करती है: वे units जो data (member variables) को उन functions के साथ bundle करते हैं जिन्हें उस data को छूने की permission है। C++ इसी तरह काम करता है।
Gहरा difference: procedural thinking पूछता है "कौन से steps होते हैं?", OOP thinking पूछता है "कौन सी चीज़ें exist करती हैं, और हर एक क्या कर सकती है?"
At a glance
Procedural बनाम OOP, exam table
| Aspect | Procedural (C) | OOP (C++) |
|---|---|---|
| Focus | Functions (steps) | Objects (data + behaviour) |
| Data | Shared या global, सबके लिए खुला | Objects में bundled, hidden |
| Design | Top-down | Bottom-up |
| Reuse | Functions फिर से call कीजिए | Inheritance classes extend करता है |
| Safety | कोई भी function data corrupt कर सकता है | Access control data guard करता है |
| Examples | C, Pascal | C++, Java, Python |
Quiz
C-style canteen till में, `total` एक global variable है। इसमें एक ग़लत amount दिखता है। इस bug को ढूँढना मुश्किल क्यों है?
- 14 में से हर एक function total में लिख सकता था, तो सब suspects हैं
- Global variables decimal values store नहीं कर सकते
- C programs debug नहीं किए जा सकते
- Bug ज़रूर main() में है, globals सिर्फ़ वहीं बदले जाते हैं
Show the answer
14 में से हर एक function total में लिख सकता था, तो सब suspects हैं
Global data का कोई owner नहीं होता: कोई भी function इसे modify कर सकता है, तो search space पूरा program है। OOP बिल्कुल यही pain हटाता है, data को एक object देकर और सिर्फ़ उसके member functions के through changes allow करके। बाक़ी options ग़लत beliefs हैं: globals किसी भी type को hold करते हैं, C debuggable है, और कोई भी function (सिर्फ़ main नहीं) एक global लिख सकता है।
Theory
चार pillars जिन्हें आप पढ़ने वाले हैं
यह पूरा subject चार OOP ideas खोलता है:
- Encapsulation: data + functions एक unit में sealed (station)।
- Abstraction: counter दिखाइए, kitchen छुपाइए।
- Inheritance: एक मौजूदा class से एक नई class बनाइए (Person card से Student card)।
- Polymorphism: एक call, कई behaviours (एक
bill()जो students और staff के लिए अलग discount देती है)।
आगे का हर topic इन्हीं pillars में से किसी एक का detail है।
Think first
इसे classify कीजिए
एक program में functions addItem(), printBill(), applyDiscount() हैं, और तीनों एक global array menu[50] पढ़ते-लिखते हैं। tap करने से पहले: यह procedural है या object-oriented, और कौन सा एक word आपको बताता है?
Show the answer
Procedural। Giveaway word है global: data किसी भी object के बाहर रहता है और हर function सीधे इस तक पहुँच सकता है। Functions होना test नहीं है (OOP के पास भी member functions हैं)। Test है data का owner कौन है। OOP में, menu एक class के अंदर रहता और सिर्फ़ उसके member functions ही इसे छू सकते।
Watch out
Trap: ".cpp" का मतलब OOP नहीं है
अपना BCA104-style code एक .cpp में ख़त्म होने वाली file में लिखना इसे object-oriented नहीं बनाता। C++ दोनों styles allow करता है, और global data से भरा एक main() अभी भी procedural है।
OOP एक design choice है, file extension नहीं। Exams को यह सवाल पसंद है "क्या C++ purely object-oriented है?" Safe answer: नहीं, C++ partially OOP है क्योंकि यह procedural code भी support करता है (और main() खुद एक free function है)।
Theory
यह marks से आगे क्यों matter करता है
हर serious system जो आप मिलेंगे (Java/Kotlin में Android apps, Python में Django, C++ में game engines) object-oriented है, क्योंकि 50 लोगों की teams एक open kitchen share नहीं कर सकतीं। जब कोई interviewer पूछता है "OOP क्यों?", winning answer scale पर maintainability के बारे में है: bugs पूरे program में लीक होने के बजाय एक class के अंदर रहते हैं।
Summary
Key takeaways
- Procedural programming = shared या global data पर काम करते functions, top-down design (C)।
- OOP = objects जो data को उन member functions के साथ bundle करते हैं जिन्हें उसे छूने की permission है, bottom-up design (C++)।
- Global data का कोई owner नहीं, तो corrupt होने पर हर function suspect है; OOP data को owner देता है।
- आगे चार pillars: encapsulation, abstraction, inheritance, polymorphism।
- C++ purely OOP नहीं है: यह procedural code भी ख़ुशी से चलाता है, OOP एक design choice है।
- Memory hook: open kitchen बनाम counters वाले stations।