Type Here to Get Search Results !

PGDCA Object-Oriented Programming With C++

0

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 के फायदे

  1. Reusability – Inheritance द्वारा code बार-बार उपयोग हो सकता है।

  2. Modularity – Program छोटे modules में बँट जाता है, जिससे maintenance सरल होती है।

  3. Security – Encapsulation डेटा को सुरक्षित बनाता है।

  4. Easy Maintenance – Object-based structure होने के कारण code update करना आसान है।

  5. Large Projects के लिए उपयुक्त – Team work, version control और scaling आसान।

  6. Real-world simulation – वास्तविक वस्तुओं को उसी तरह प्रोग्राम में दिखाना आसान।


2. कंस्ट्रक्टर के प्रकार और डेस्ट्रक्टर की भूमिका

कंस्ट्रक्टर (Constructor)

Constructor एक special member function है जो object बनते समय अपने-आप execute होता है। इसका उद्देश्य object को initialize करना है।

कंस्ट्रक्टर्स के प्रकार:

1. Default Constructor

  • बिना parameters

  • object creation पर automatic call

Class() { }

2. Parameterized Constructor

  • parameters के साथ

Class(int x, int y) { }

3. Copy Constructor

  • एक object से दूसरे object में values कॉपी करना

Class(Class &obj) { }

4. Dynamic Constructor

  • new operator से run-time पर memory allocate करता है।

5. Constructor Overloading

  • एक ही नाम के कई constructors लेकिन parameters अलग।


डेस्ट्रक्टर (Destructor)

  • Object destroy होते समय call होता है

  • Memory free करता है

  • Resource release करता है
    Syntax:

~Class() { }

डेस्ट्रक्टर की भूमिका:

  1. Dynamic memory free करना।

  2. Files बंद करना।

  3. Database connections close करना।

  4. Resources को सुरक्षित रीलीज़ करना।


3. ऐरे और पॉइंटर समझाइए

ऐरे (Array)

Array एक ऐसी data structure है जिसमें समान प्रकार के कई elements को एक ही नाम से संग्रहीत किया जाता है।

  • Memory में contiguous (लगातार) स्थान पर store होते हैं।

  • Indexing 0 से शुरू होती है।

उदाहरण:

int a[5] = {1,2,3,4,5};

पॉइंटर (Pointer)

Pointer एक variable है जो किसी अन्य variable का address store करता है।

  • Address प्राप्त करने के लिए &

  • Address की value प्राप्त करने के लिए *

उदाहरण:

int x = 10; int *p = &x;

Array और Pointer का संबंध

  • array का नाम अपने पहले element का address है।

  • Pointer का उपयोग array traversal में किया जाता है।

उदाहरण:

int arr[3] = {10, 20, 30}; int *p = arr;

4. फंक्शन और ऑपरेटर ओवरलोडिंग

Function Overloading

Function overloading का अर्थ है एक ही नाम के कई functions लेकिन parameters अलग-अलग।
इससे readability बढ़ती है।

उदाहरण:

void add(int a, int b); void add(double a, double b);

Operator Overloading

C++ में operators को objects के लिए नए तरीके से उपयोग करने की सुविधा।
उदाहरण:

Complex operator + (Complex);

Operator overloading की आवश्यकता:

  • user-defined data types पर operations करना

  • code को natural बनाना


5. इनहेरिटेंस और उसके प्रकार; Single व Multiple Inheritance का अंतर

Inheritance (इनहेरिटेंस)

एक class दूसरी class के properties और methods को अपनाती है।
Derived class = Base class + New features


इनहेरिटेंस के प्रकार:

  1. Single Inheritance

    • एक base class, एक derived class

  2. Multiple Inheritance

    • एक derived class के लिए कई base classes

  3. Multilevel Inheritance

    • Parent → Child → Grandchild

  4. Hierarchical Inheritance

    • एक base class → कई derived classes

  5. Hybrid Inheritance

    • कई inheritance types का combination


Single और Multiple Inheritance का अंतर

आधारSingleMultiple
Base classesएकदो या अधिक
Complexityकमअधिक
AmbiguityनहींDiamond problem संभव
ExampleB → AC → A, B

6. वर्चुअल और प्योअर वर्चुअल फंक्शन क्या हैं?

Virtual Function

  • Base class में declared

  • Derived class override कर सकती है

  • Runtime polymorphism प्रदान करता है

उदाहरण:

virtual void show();

Pure Virtual Function

  • Base class में केवल declare

  • = 0 लगाया जाता है

  • Base class “Abstract Class” बन जाती है

उदाहरण:

virtual void display() = 0;

7. फाइल हैंडलिंग समझाइए

File handling का अर्थ है प्रोग्राम द्वारा file पढ़ना, लिखना, modify करना, और update करना।

File Streams:

  • ifstream → file पढ़ने के लिए

  • ofstream → file लिखने के लिए

  • fstream → read + write

उदाहरण:

ofstream file("data.txt"); file << "Hello World"; file.close();

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 दिखाना

उदाहरण:

cout << setw(10) << setprecision(2) << 3.14159;

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 कर सकता है

उदाहरण:

int *p = new int(10);

delete operator

  • Memory free करता है

delete p;

Array delete:

int *arr = new int[5]; delete[] arr;

मेमोरी मैनेजमेंट के फायदे:

  • Memory leak रोकता है

  • System resources बचाता है

  • Large dynamic data handle करना आसान

Post a Comment

0 Comments

Top Post Ad

Bottom Post Ad

Show ad in Posts/Pages

WhatsApp