Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟣 OOP  ·  Lesson 37

Object-Oriented Programming Concepts

Object-oriented programming क्या है?

Procedural programming (जैसे C) में program functions की सूची होता है जो अलग रखे data पर काम करते हैं। Program बड़ा होने पर यह जानना मुश्किल हो जाता है कि कौन-सा function कौन-सा data बदल रहा है। Object-oriented programming (OOP) data और उस पर काम करने वाले functions को objects में समूहित करती है, जो असली चीज़ों को दर्शाते हैं — student, bank account, library book।

OOP पूछती है: *मेरी problem में कौन-सी चीज़ें हैं, उन्हें क्या पता है, और वे क्या कर सकती हैं?*

C++ ने C के ऊपर OOP जोड़ी, इसीलिए इसे "C with classes" भी कहते हैं। यह lesson हर OOP concept को एक ही चलते उदाहरण — school bank account — से समझाता है, और हर concept के विस्तृत lesson का link देता है।

Class और object

Class एक blueprint है; object उससे बनी असली चीज़।

C++
class Account {          // blueprint
public:
    std::string holder;
    double balance;
};

Account a1;              // object 1
Account a2;              // object 2 — अलग data

holder और balance data members (attributes) हैं; class के अंदर के functions member functions (methods) कहलाते हैं। हर object के पास data members की अपनी copy होती है। देखें classes और objects।

1. Encapsulation

Encapsulation का मतलब है data को उसे इस्तेमाल करने वाले functions के साथ बाँधना *और* बाहर से सीधे access से छिपाना। Data को private रखें और बदलाव सिर्फ public functions से होने दें जो नियम check करें।

C++
class Account {
private:
    double balance = 0;               // छिपा हुआ
public:
    void deposit(double amt) {
        if (amt > 0) balance += amt;  // नियम एक ही जगह लागू
    }
    double getBalance() const { return balance; }
};

अब कोई acc.balance = -5000; नहीं लिख सकता। देखें access specifiers।

2. Abstraction

Abstraction का मतलब है object *क्या* करता है यह दिखाना और *कैसे* करता है यह छिपाना। acc.withdraw(500) call करते समय आपको फ़र्क नहीं पड़ता कि balance कैसे store, log या check होता है — आप एक आसान interface इस्तेमाल करते हैं। C++ में abstraction classes, access specifiers और abstract classes (pure virtual functions वाली classes) से मिलता है, जो बिना implementation के interface तय करती हैं। देखें abstract classes।

3. Inheritance

Inheritance से नई class (derived/child) मौजूदा class (base/parent) को फिर से इस्तेमाल और आगे बढ़ा सकती है। SavingsAccount एक Account *है* जिस पर ब्याज भी मिलता है।

C++
class SavingsAccount : public Account {
    double rate = 0.04;
public:
    void addInterest() { deposit(getBalance() * rate); }
};

SavingsAccount के पास अपने-आप deposit() और getBalance() हैं। प्रकार: single, multilevel, multiple, hierarchical और hybrid inheritance। देखें inheritance और multiple inheritance।

4. Polymorphism

Polymorphism ("कई रूप") का मतलब है एक नाम या interface object के हिसाब से अलग व्यवहार कर सकता है।

प्रकारकब तय होता हैC++ में कैसे
Compile-time (static)Compilation के दौरानFunction overloading, operator overloading, templates
Run-time (dynamic)Program चलते समयBase-class pointer या reference से call होने वाले virtual functions

पूरा उदाहरण: चारों एक साथ

C++
#include <iostream>
#include <string>
#include <vector>
#include <memory>

// Abstraction: हर account को देना होगा यह interface
class Account {
protected:
    std::string holder;
    double balance;                       // Encapsulation: public नहीं
public:
    Account(std::string h, double b) : holder(h), balance(b) {}
    virtual ~Account() = default;
    void deposit(double amt) { if (amt > 0) balance += amt; }
    virtual void monthEnd() = 0;          // pure virtual -> abstract class
    void show() const {
        std::cout << holder << ": Rs " << balance << "\n";
    }
};

// Inheritance
class Savings : public Account {
public:
    using Account::Account;
    void monthEnd() override { balance += balance * 0.01; }   // 1% ब्याज
};

class Current : public Account {
public:
    using Account::Account;
    void monthEnd() override { balance -= 50; }               // service fee
};

int main() {
    std::vector<std::unique_ptr<Account>> accounts;
    accounts.push_back(std::make_unique<Savings>("Aman", 10000));
    accounts.push_back(std::make_unique<Current>("Priya", 10000));

    for (auto& a : accounts) {
        a->monthEnd();     // Polymorphism: सही version run time पर चुना जाता है
        a->show();
    }
    return 0;
}
Aman: Rs 10100 Priya: Rs 9950

Loop को नहीं पता कि उसमें कौन-सा account है; हर object अपना monthEnd() चलाता है।

OOP के फ़ायदे

  • Modularity: हर class एक स्वतंत्र इकाई है जिसे समझना और test करना आसान है।
  • Reusability: inheritance और composition से tested code फिर इस्तेमाल होता है।
  • Data security: encapsulation data में गलत बदलाव रोकता है।
  • Maintainability: class के अंदर बदलाव उसके public interface को इस्तेमाल करने वाले code को नहीं तोड़ते।
  • Real-world modelling: classes problem की चीज़ों से स्वाभाविक रूप से मेल खाती हैं।

Procedural vs object-oriented

बिंदुProcedural (C)Object-oriented (C++)
Program किससे बनता हैFunctionsObjects (data + functions)
Data accessअक्सर global, सबके लिए खुलाPrivate, member functions से
ReuseFunctions copy या call करनाInheritance और composition
तरीकाTop-downBottom-up
उदाहरण languageCC++, Java, Python

और अंतर के लिए देखें C++ vs C।

और OOP terms जो आगे मिलेंगे

  • Constructor / destructor: object बनने / नष्ट होने पर चलने वाले खास functions। देखें constructors।
  • Message passing: objects एक-दूसरे के member functions call करके बात करते हैं।
  • Dynamic binding: कौन-सा function चलेगा, यह run time पर तय होता है (virtual functions)।
  • Composition: एक class के *पास* दूसरी class के objects होते हैं (एक School के पास Students)।

आम गलतियाँ

  • हर data member को public बना देना, जिससे encapsulation ख़त्म हो जाता है।
  • जहाँ "has-a" (composition) सही हो वहाँ inheritance लेना — Car के पास Engine है, वह Engine नहीं है।
  • Base function पर virtual भूलना, जिससे derived की जगह base version चलता है।
  • Base class में virtual destructor भूलना, जब object base pointer से delete हो।
  • Abstraction (जटिलता छिपाना) और encapsulation (data छिपाना) को एक समझना।
🏋️ अभ्यास

Library model करें: abstract class Item जिसमें pure virtual lateFee(int days) हो, और derived classes Book (Rs 2/दिन) और Magazine (Rs 1/दिन)। दोनों को vector<unique_ptr<Item>> में रखें और 5 दिन की देरी पर हर item की fee print करें।

सारांश

  • OOP programs को data और व्यवहार वाले objects के इर्द-गिर्द बनाती है।
  • Class = blueprint, object = instance।
  • Encapsulation data छिपाता है; abstraction जटिलता छिपाता है।
  • Inheritance classes को reuse और extend करता है; polymorphism एक interface को कई रूप देता है।
  • C++ OOP support करती है पर pure OOP language नहीं है।

अगला: classes और objects → constructors → inheritance → polymorphism।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में OOP के चार pillars कौन-से हैं?
चार pillars हैं: encapsulation (data को उसे इस्तेमाल करने वाले functions के साथ बाँधना और details छिपाना), abstraction (object क्या करता है यह दिखाना, कैसे करता है यह नहीं), inheritance (मौजूदा class से नई class बनाना) और polymorphism (एक interface, कई रूप — जैसे एक ही function call अलग classes के लिए अलग व्यवहार करे)।
Class और object में क्या अंतर है?
Class एक blueprint है जो data members और member functions परिभाषित करती है, जैसे Student। Object उस blueprint से बना असली instance है, जैसे Student s1;, जिसकी memory में अपनी values होती हैं। एक class से कई objects बन सकते हैं।
Encapsulation और abstraction में क्या अंतर है?
Encapsulation data की सुरक्षा के बारे में है: data members private रखें और access सिर्फ public functions से दें। Abstraction इस बारे में है कि user क्या देखता है: withdraw(amount) जैसा आसान interface दिखाएँ और उसके पीछे का जटिल implementation छिपाएँ। Encapsulation abstraction पाने का एक साधन है।
क्या C++ pure object-oriented language है?
नहीं। C++ OOP support करती है पर procedural code भी चलने देती है — आप किसी class के बाहर functions और variables लिख सकते हैं, और main() खुद किसी class का member नहीं है। इसलिए C++ को multi-paradigm (या partially object-oriented) language कहते हैं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicClasses and Objects आपकी प्रगति: 37 / 81 (46%) · Lesson 38 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें