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।
C++ ने C के ऊपर OOP जोड़ी, इसीलिए इसे "C with classes" भी कहते हैं। यह lesson हर OOP concept को एक ही चलते उदाहरण — school bank account — से समझाता है, और हर concept के विस्तृत lesson का link देता है।
Class और object
Class एक blueprint है; object उससे बनी असली चीज़।
class Account { // blueprint
public:
std::string holder;
double balance;
};
Account a1; // object 1
Account a2; // object 2 — अलग dataholder और 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 करें।
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 *है* जिस पर ब्याज भी मिलता है।
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 |
पूरा उदाहरण: चारों एक साथ
#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;
}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 किससे बनता है | Functions | Objects (data + functions) |
| Data access | अक्सर global, सबके लिए खुला | Private, member functions से |
| Reuse | Functions copy या call करना | Inheritance और composition |
| तरीका | Top-down | Bottom-up |
| उदाहरण language | C | C++, 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 कौन-से हैं?
Class और object में क्या अंतर है?
Student। Object उस blueprint से बना असली instance है, जैसे Student s1;, जिसकी memory में अपनी values होती हैं। एक class से कई objects बन सकते हैं।Encapsulation और abstraction में क्या अंतर है?
private रखें और access सिर्फ public functions से दें। Abstraction इस बारे में है कि user क्या देखता है: withdraw(amount) जैसा आसान interface दिखाएँ और उसके पीछे का जटिल implementation छिपाएँ। Encapsulation abstraction पाने का एक साधन है।क्या C++ pure object-oriented language है?
main() खुद किसी class का member नहीं है। इसलिए C++ को multi-paradigm (या partially object-oriented) language कहते हैं।