What is Object-Oriented Programming?
The key ideas that make OOP different from writing one long procedural program.
What is Object-Oriented Programming?
Object-Oriented Programming (OOP) एक programming approach (प्रोग्रामिंग पद्धति) है जिसमें program को objects के रूप में organize किया जाता है। Object किसी real-world entity (वास्तविक दुनिया की वस्तु) जैसे Student, Employee, Car या Bank Account को represent कर सकता है।
OOP में data और उस data पर काम करने वाले functions को एक single unit में combine किया जाता है। इस single unit को object कहा जाता है। C++ एक object-oriented programming language है जो OOP concepts को support करती है।
Need for Object-Oriented Programming
Traditional programming में बड़े programs को manage करना कठिन हो सकता है क्योंकि data और functions को अलग-अलग handle करना पड़ता है। OOP में related data और functions को एक साथ organize किया जाता है, जिससे large programs को develop और maintain करना आसान हो जाता है।
OOP real-world entities को program में represent करने की सुविधा देता है। उदाहरण के लिए, school management system में Student, Teacher और Course को अलग-अलग objects के रूप में represent किया जा सकता है।
Basic Concepts of OOP
Object-Oriented Programming के प्रमुख concepts निम्नलिखित हैं:
- Class
- Object
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
Class
Class एक blueprint (खाका) या template है जिसका उपयोग objects बनाने के लिए किया जाता है। Class में data members और member functions define किए जा सकते हैं।
उदाहरण के लिए, यदि Student एक class है, तो इसमें roll number, name और marks जैसे data members तथा display() जैसे member functions हो सकते हैं।
#include <iostream>
using namespace std;
class Student
{
public:
int rollNo;
float marks;
void display()
{
cout << "Roll No: " << rollNo << endl;
cout << "Marks: " << marks;
}
};
int main()
{
Student s1;
s1.rollNo = 101;
s1.marks = 88.5;
s1.display();
return 0;
}
Roll No: 101
Marks: 88.5
यहाँ Student एक class है और s1 उसका object है।
Object
Object किसी class का instance (उदाहरण) होता है। Class केवल structure या blueprint define करती है, जबकि object उस class के आधार पर वास्तविक data को represent करता है।
उदाहरण:
Student s1;
Student s2;
यहाँ s1 और s2 Student class के दो अलग-अलग objects हैं। प्रत्येक object के पास अपने data members की अलग values हो सकती हैं।
Encapsulation
Encapsulation का अर्थ data और उससे संबंधित functions को एक single unit में bind करना है। C++ में class encapsulation को implement करने का प्रमुख माध्यम है।
Encapsulation data को direct unwanted access से protect करने में भी सहायता करता है। इसके लिए private, public और protected access specifiers का उपयोग किया जाता है।
#include <iostream>
using namespace std;
class BankAccount
{
private:
float balance;
public:
void setBalance(float b)
{
balance = b;
}
void showBalance()
{
cout << "Balance: " << balance;
}
};
int main()
{
BankAccount account;
account.setBalance(5000);
account.showBalance();
return 0;
}
Balance: 5000
यहाँ balance को private रखा गया है और उसे directly access करने के बजाय member functions के माध्यम से access किया गया है।
Abstraction
Abstraction का अर्थ unnecessary implementation details को छिपाकर केवल आवश्यक information या functionality को user के सामने प्रस्तुत करना है।
उदाहरण के लिए, जब हम car चलाते हैं तो हमें engine के अंदर fuel injection या ignition system की पूरी working जानने की आवश्यकता नहीं होती। हम केवल steering, brake और accelerator जैसे controls का उपयोग करते हैं।
Inheritance
Inheritance वह OOP feature है जिसमें एक class किसी दूसरी class के properties और functions को inherit (प्राप्त) कर सकती है। इससे existing code को reuse किया जा सकता है।
जिस class से properties inherit की जाती हैं उसे base class और जो class उन्हें inherit करती है उसे derived class कहा जाता है।
#include <iostream>
using namespace std;
class Animal
{
public:
void eat()
{
cout << "Animal eats";
}
};
class Dog : public Animal
{
};
int main()
{
Dog d;
d.eat();
return 0;
}
Animal eats
यहाँ Dog class ने Animal class से eat() function inherit किया है।
Polymorphism
Polymorphism का अर्थ है "many forms" अर्थात एक ही interface या function का अलग-अलग situations में अलग behavior होना। C++ में polymorphism को function overloading, operator overloading और virtual functions जैसी techniques द्वारा implement किया जा सकता है।
उदाहरण के लिए, एक ही नाम के functions अलग-अलग प्रकार के arguments के साथ अलग कार्य कर सकते हैं।
#include <iostream>
using namespace std;
class Calculator
{
public:
int add(int a, int b)
{
return a + b;
}
float add(float a, float b)
{
return a + b;
}
};
int main()
{
Calculator c;
cout << c.add(10, 20) << endl;
cout << c.add(2.5f, 3.5f);
return 0;
}
30
6
यहाँ add() function का नाम समान है लेकिन अलग-अलग parameter types के साथ अलग versions मौजूद हैं। इसे function overloading कहा जाता है और यह compile-time polymorphism का उदाहरण है।
Data Hiding
Data Hiding का अर्थ object के internal data को direct external access से सुरक्षित रखना है। C++ में private access specifier का उपयोग करके data को hide किया जा सकता है।
class Student
{
private:
int marks;
public:
void setMarks(int m)
{
marks = m;
}
};
यहाँ marks private है, इसलिए class के बाहर से इसे directly access नहीं किया जा सकता।
Message Passing
OOP में objects एक-दूसरे से communicate करने के लिए functions या methods को call कर सकते हैं। इस concept को message passing कहा जाता है।
उदाहरण के लिए:
student.display();
यहाँ student object को display() function execute करने के लिए message भेजा जा रहा है।
Real-World Example of OOP
मान लीजिए हमें एक school management system बनाना है। इसमें कई real-world entities हो सकती हैं:
| Real-World Entity | Possible Class | Possible Data |
|---|---|---|
| Student | Student | Roll No, Name, Marks |
| Teacher | Teacher | Name, Subject, Salary |
| Course | Course | Course Name, Duration |
| Book | Book | Title, Author, Price |
इन entities को classes के रूप में define करके उनके multiple objects बनाए जा सकते हैं।
Advantages of Object-Oriented Programming
- OOP real-world entities को program में represent करने में मदद करता है।
- Large programs को छोटे और manageable components में divide किया जा सकता है।
- Code reusability बढ़ती है।
- Encapsulation और data hiding की सहायता से data को protect किया जा सकता है।
- Inheritance existing code को reuse करने में सहायता करता है।
- Polymorphism program को flexible बनाने में मदद करता है।
- Programs को maintain और modify करना आसान होता है।
- OOP complex software systems को organized तरीके से develop करने में उपयोगी है।
Characteristics of Object-Oriented Programming
| Concept | Meaning |
|---|---|
| Class | Objects बनाने का blueprint |
| Object | Class का instance |
| Encapsulation | Data और functions को एक unit में bind करना |
| Abstraction | Unnecessary details को hide करना |
| Inheritance | Existing class से properties और functions प्राप्त करना |
| Polymorphism | एक interface के multiple forms या behaviors |
| Data Hiding | Data को unwanted direct access से protect करना |
Procedure-Oriented Programming vs Object-Oriented Programming
Programming को समझने के लिए Procedure-Oriented Programming (POP) और Object-Oriented Programming (OOP) के बीच अंतर जानना उपयोगी है।
| Procedure-Oriented Programming | Object-Oriented Programming |
|---|---|
| Focus functions या procedures पर होता है। | Focus objects और classes पर होता है। |
| Data और functions सामान्यतः अलग-अलग होते हैं। | Data और related functions को एक unit में combine किया जा सकता है। |
| Top-down approach अधिक सामान्य है। | Bottom-up approach अधिक सामान्य है। |
| Data security comparatively कम हो सकती है। | Encapsulation और data hiding उपलब्ध हैं। |
| Code reuse सीमित हो सकता है। | Inheritance द्वारा code reuse संभव है। |
Why C++ is an Object-Oriented Language
C++ को object-oriented language कहा जाता है क्योंकि यह class, object, encapsulation, inheritance और polymorphism जैसे OOP concepts को support करती है। Programmer इन concepts का उपयोग करके complex applications को structured और reusable तरीके से develop कर सकता है।
Important Points
- OOP का पूरा नाम Object-Oriented Programming है।
- C++ एक object-oriented programming language है।
- Class objects बनाने का blueprint है।
- Object किसी class का instance होता है।
- Encapsulation data और functions को एक unit में bind करता है।
- Abstraction unnecessary implementation details को hide करता है।
- Inheritance existing class की properties और functions को reuse करने की सुविधा देता है।
- Polymorphism का अर्थ many forms है।
- Data hiding unwanted direct access से data को protect करता है।
- OOP large software systems को organize और maintain करने में उपयोगी है।
Board Focus
OOP → Object-Oriented Programming
Class → Objects का blueprint
Object → Class का instance
Encapsulation → Data और functions को एक unit में bind करना
Abstraction → Unnecessary details को hide करना
Inheritance → Existing class से properties/functions प्राप्त करना
Polymorphism → Many forms
Data Hiding → Data को unwanted access से protect करना
Board Important Questions
Very Short Answer Questions
Q1. OOP का पूरा नाम क्या है?
Answer: Object-Oriented Programming.
Q2. C++ किस प्रकार की programming language है?
Answer: C++ एक object-oriented programming language है।
Q3. Class क्या है?
Answer: Class objects बनाने का blueprint या template है।
Q4. Object क्या है?
Answer: Object किसी class का instance होता है।
Q5. Encapsulation क्या है?
Answer: Data और related functions को एक single unit में bind करना encapsulation कहलाता है।
Q6. Polymorphism का क्या अर्थ है?
Answer: Polymorphism का अर्थ many forms अर्थात एक interface या function के multiple forms या behaviors होना है।
Q7. Inheritance क्या है?
Answer: एक class द्वारा दूसरी class की properties और functions को प्राप्त करने की प्रक्रिया inheritance कहलाती है।
Q8. Abstraction क्या है?
Answer: आवश्यक features को दिखाना और unnecessary implementation details को hide करना abstraction कहलाता है।
Short Answer Questions
Q9. Object-Oriented Programming क्या है?
Answer: Object-Oriented Programming एक programming approach है जिसमें program को objects और classes के आधार पर organize किया जाता है। इसमें data और उससे संबंधित functions को एक unit में combine किया जा सकता है।
Q10. Class और Object में अंतर बताइए।
| Class | Object |
|---|---|
| Class एक blueprint या template है। | Object class का instance है। |
| Class objects की structure और behavior define करती है। | Object class के आधार पर वास्तविक data को represent करता है। |
| एक class से कई objects बनाए जा सकते हैं। | हर object का अपना अलग data हो सकता है। |
Q11. Encapsulation और data hiding में क्या संबंध है?
Answer: Encapsulation data और related functions को एक unit में combine करता है। Data hiding में internal data को direct external access से सुरक्षित रखा जाता है। C++ में private members का उपयोग करके data hiding implement की जा सकती है।
Q12. Inheritance के क्या advantages हैं?
Answer: Inheritance existing class के properties और functions को derived class में reuse करने की सुविधा देता है। इससे code duplication कम होता है और code reusability बढ़ती है।
Q13. Polymorphism को उदाहरण सहित समझाइए।
Answer: Polymorphism का अर्थ many forms है। इसमें एक ही function या interface अलग-अलग situations में अलग behavior कर सकता है। Function overloading इसका एक उदाहरण है, जिसमें same function name के अलग versions अलग parameters के साथ define किए जाते हैं।
Long Answer Questions
Q14. Object-Oriented Programming के प्रमुख concepts समझाइए।
Answer: Object-Oriented Programming के प्रमुख concepts Class, Object, Encapsulation, Abstraction, Inheritance और Polymorphism हैं। Class objects का blueprint है और Object class का instance होता है। Encapsulation data और functions को एक unit में bind करता है। Abstraction unnecessary details को hide करता है। Inheritance existing class से properties और functions को reuse करने की सुविधा देता है। Polymorphism एक interface या function के multiple forms या behaviors को support करता है।
Q15. OOP के advantages समझाइए।
Answer: OOP real-world entities को program में represent करने, code को reusable बनाने और large programs को manageable components में divide करने में सहायता करता है। Encapsulation और data hiding data security को बेहतर बनाते हैं। Inheritance code reusability बढ़ाता है और polymorphism program को flexible बनाता है। इस कारण large और complex software applications को develop और maintain करना आसान हो जाता है।
Q16. C++ में class और object का उपयोग करके एक सरल program लिखिए।
Answer:
#include <iostream>
using namespace std;
class Rectangle
{
public:
int length;
int breadth;
int area()
{
return length * breadth;
}
};
int main()
{
Rectangle r;
r.length = 10;
r.breadth = 5;
cout << "Area: " << r.area();
return 0;
}
Area: 50
यहाँ Rectangle एक class और r उसका object है। Object के द्वारा data members को values दी गई हैं और area() member function को call करके area calculate किया गया है।
Quick Revision
- OOP: Object-Oriented Programming
- Class: Object बनाने का blueprint
- Object: Class का instance
- Encapsulation: Data और functions को एक unit में bind करना
- Abstraction: Unnecessary details को hide करना
- Inheritance: Existing class से features प्राप्त करना
- Polymorphism: Many forms
- Data Hiding: Internal data को direct access से protect करना
Practice Questions
- Object-Oriented Programming क्या है?
- OOP का पूरा नाम लिखिए।
- Class और Object को परिभाषित कीजिए।
- Class और Object में अंतर बताइए।
- Encapsulation क्या है?
- Abstraction क्या है?
- Inheritance क्या है?
- Polymorphism से आप क्या समझते हैं?
- Data hiding क्या है?
- OOP के प्रमुख concepts लिखिए।
- OOP के advantages लिखिए।
- Procedure-Oriented और Object-Oriented Programming में अंतर बताइए।
- C++ में class और object का उपयोग करके एक program लिखिए।
- Inheritance का उदाहरण देते हुए C++ program लिखिए।
- Function overloading द्वारा polymorphism को demonstrate करने का C++ program लिखिए।