1. OOP की अवधारणा और फायदे समझाइए
OOP की अवधारणा (Object Oriented Programming Concept)
OOP यानी Object Oriented Programming एक आधुनिक प्रोग्रामिंग पद्धति है जिसमें प्रोग्राम को Objects और Classes के रूप में डिज़ाइन किया जाता है। OOP में हम वास्तविक दुनिया (Real World) की वस्तुओं (Objects) को वैसा ही computer program में उपयोग कर सकते हैं।
इसमें डेटा और फंक्शन्स को एक साथ संगठित किया जाता है, जिससे कोड बेहतर, सुरक्षित और पुनः उपयोग योग्य बनता है।
OOP की मुख्य अवधारणाएँ:
1. Class (क्लास)
Class एक Template या Blueprint है जिसमें Data Members (variables) और Member Functions (methods) होते हैं।
2. Object (ऑब्जेक्ट)
Object क्लास का वास्तविक रूप होता है। हर object की अपनी identity, state और behavior होता है।
3. Encapsulation (एन्कैप्सुलेशन)
डेटा और functions को एक ही जगह बांध कर सुरक्षित करना।
उदाहरण: private members का उपयोग।
4. Abstraction (ऐब्स्ट्रैक्शन)
अनावश्यक चीजों को छिपाना और केवल आवश्यक चीजों को दिखाना।
जैसे—ATM मशीन user को केवल amount डालने देती है, अंदर का complex process नहीं दिखाती।
5. Inheritance (इनहेरिटेंस)
एक class दूसरी class के features को अपनाती है। इससे कोड reusability बढ़ती है।
6. Polymorphism (पॉलीमॉरफिज्म)
एक ही काम को कई तरीकों से करना।
जैसे—function overloading और virtual functions।
OOP के फायदे
-
Reusability – Inheritance द्वारा code बार-बार उपयोग हो सकता है।
-
Modularity – Program छोटे modules में बँट जाता है, जिससे maintenance सरल होती है।
-
Security – Encapsulation डेटा को सुरक्षित बनाता है।
-
Easy Maintenance – Object-based structure होने के कारण code update करना आसान है।
-
Large Projects के लिए उपयुक्त – Team work, version control और scaling आसान।
-
Real-world simulation – वास्तविक वस्तुओं को उसी तरह प्रोग्राम में दिखाना आसान।
2. कंस्ट्रक्टर के प्रकार और डेस्ट्रक्टर की भूमिका
कंस्ट्रक्टर (Constructor)
Constructor एक special member function है जो object बनते समय अपने-आप execute होता है। इसका उद्देश्य object को initialize करना है।
कंस्ट्रक्टर्स के प्रकार:
1. Default Constructor
-
बिना parameters
-
object creation पर automatic call
2. Parameterized Constructor
-
parameters के साथ
3. Copy Constructor
-
एक object से दूसरे object में values कॉपी करना
4. Dynamic Constructor
-
new operator से run-time पर memory allocate करता है।
5. Constructor Overloading
-
एक ही नाम के कई constructors लेकिन parameters अलग।
डेस्ट्रक्टर (Destructor)
-
Object destroy होते समय call होता है
-
Memory free करता है
-
Resource release करता है
Syntax:
डेस्ट्रक्टर की भूमिका:
-
Dynamic memory free करना।
-
Files बंद करना।
-
Database connections close करना।
-
Resources को सुरक्षित रीलीज़ करना।
3. ऐरे और पॉइंटर समझाइए
ऐरे (Array)
Array एक ऐसी data structure है जिसमें समान प्रकार के कई elements को एक ही नाम से संग्रहीत किया जाता है।
-
Memory में contiguous (लगातार) स्थान पर store होते हैं।
-
Indexing 0 से शुरू होती है।
उदाहरण:
पॉइंटर (Pointer)
Pointer एक variable है जो किसी अन्य variable का address store करता है।
-
Address प्राप्त करने के लिए
& -
Address की value प्राप्त करने के लिए
*
उदाहरण:
Array और Pointer का संबंध
-
array का नाम अपने पहले element का address है।
-
Pointer का उपयोग array traversal में किया जाता है।
उदाहरण:
4. फंक्शन और ऑपरेटर ओवरलोडिंग
Function Overloading
Function overloading का अर्थ है एक ही नाम के कई functions लेकिन parameters अलग-अलग।
इससे readability बढ़ती है।
उदाहरण:
Operator Overloading
C++ में operators को objects के लिए नए तरीके से उपयोग करने की सुविधा।
उदाहरण:
Operator overloading की आवश्यकता:
-
user-defined data types पर operations करना
-
code को natural बनाना
5. इनहेरिटेंस और उसके प्रकार; Single व Multiple Inheritance का अंतर
Inheritance (इनहेरिटेंस)
एक class दूसरी class के properties और methods को अपनाती है।
Derived class = Base class + New features
इनहेरिटेंस के प्रकार:
-
Single Inheritance
-
एक base class, एक derived class
-
-
Multiple Inheritance
-
एक derived class के लिए कई base classes
-
-
Multilevel Inheritance
-
Parent → Child → Grandchild
-
-
Hierarchical Inheritance
-
एक base class → कई derived classes
-
-
Hybrid Inheritance
-
कई inheritance types का combination
-
Single और Multiple Inheritance का अंतर
| आधार | Single | Multiple |
|---|---|---|
| Base classes | एक | दो या अधिक |
| Complexity | कम | अधिक |
| Ambiguity | नहीं | Diamond problem संभव |
| Example | B → A | C → A, B |
6. वर्चुअल और प्योअर वर्चुअल फंक्शन क्या हैं?
Virtual Function
-
Base class में declared
-
Derived class override कर सकती है
-
Runtime polymorphism प्रदान करता है
उदाहरण:
Pure Virtual Function
-
Base class में केवल declare
-
= 0लगाया जाता है -
Base class “Abstract Class” बन जाती है
उदाहरण:
7. फाइल हैंडलिंग समझाइए
File handling का अर्थ है प्रोग्राम द्वारा file पढ़ना, लिखना, modify करना, और update करना।
File Streams:
-
ifstream → file पढ़ने के लिए
-
ofstream → file लिखने के लिए
-
fstream → read + write
उदाहरण:
File handling के फायदे:
-
Data permanent storage
-
Large data management
-
Program automation
8. C++ I/O सिस्टम और मैनिपुलेटर्स
C++ I/O System
C++ में input-output operations streams द्वारा किए जाते हैं।
मुख्य streams:
-
cin– input -
cout– output -
cerr– errors -
clog– logging
Manipulators
Manipulators formatting के लिए उपयोग होते हैं।
कुछ प्रमुख manipulators:
-
setw()– output width -
setprecision()– decimal precision -
endl– next line -
fixed– fixed decimal format -
showpoint– decimal point दिखाना
उदाहरण:
9. पॉलीमॉरफिज्म — Compile Time और Runtime
Polymorphism (बहुरूपता)
एक कार्य को कई रूपों में करना।
Compile Time Polymorphism
-
Function overloading
-
Operator overloading
-
Compile-time पर निर्णय होता है
कारण: faster execution
Runtime Polymorphism
-
Virtual functions द्वारा
-
Real function रनटाइम पर select होता है
-
Inheritance आधारित
यह dynamic binding का उपयोग करता है।
10. new/delete द्वारा मेमोरी मैनेजमेंट
new operator
-
Heap memory allocate करता है
-
Initialization कर सकता है
उदाहरण:
delete operator
-
Memory free करता है
Array delete:
मेमोरी मैनेजमेंट के फायदे:
-
Memory leak रोकता है
-
System resources बचाता है
-
Large dynamic data handle करना आसान
Hi, Please do not spam in Comment