Theory
यह क्यों ज़रूरी है
एक mark को float में और एक नाम को character array में रखना आपको आता है। लेकिन student सिर्फ एक value नहीं होता। Student का मतलब है एक roll number, एक नाम, तीन विषयों के marks, और अपना result खुद print करने की क्षमता। इन सबको अलग-अलग variables में रखना 10 students तक चलता है और 10,000 पर बिखर जाता है। Class इसी का C++ वाला जवाब है, और अगले दो units का लगभग हर topic class से जुड़ा कोई न कोई नियम ही है।
Theory
एक template, हज़ारों ID cards
आपके college ने ID card का एक layout design किया है: photo का box, नाम की line, roll number की line, एक barcode। वह design खुद कोई ID card नहीं है। आप उसे library gate पर swipe नहीं कर सकते। Office उसी design से हज़ारों असली cards print करता है, हर card पर अलग photo और अलग roll number, लेकिन fields वही और उसी जगह पर।
वह design class है। हर छपा हुआ card एक object है।
Theory
दो definitions
Class एक user-defined data type है जो data (member variables) और उस data पर काम करने वाले functions (member functions) को एक ही unit में बांध देता है। यह blueprint है, कोई चीज़ नहीं।
Object class का एक instance है: उसी blueprint से बनी एक असली entity, अपनी values के साथ।
Exam में इसी तरह लिखिए: Student class है, s1 और s2 उस class के objects हैं।
Theory
वह line जो examiner सुनना चाहता है
Class लिखने से कोई memory allocate नहीं होती। class Student { int roll; float marks; }; कुछ भी reserve नहीं करती। यह सिर्फ compiler को बताती है कि Student दिखेगा कैसा।
Memory तब बनती है जब आप object बनाते हैं। Student s1; लिखते ही s1 को अपना roll और अपना marks मिल जाता है। दो objects का मतलब है data की दो अलग copies, जबकि member functions एक ही बार store होते हैं और सारे objects उन्हें मिलकर इस्तेमाल करते हैं।
At a glance
Class बनाम Object, exam वाला जवाब
| Aspect | Class | Object |
|---|---|---|
| यह है क्या | एक blueprint या template | उसी से बना एक instance |
| Memory | Declare करने पर कुछ नहीं | हर object के लिए allocate होती है |
| कितने | एक बार लिखा जाता है | जितने आप बनाएं |
| कैसे बनता है | class Student { ... }; | Student s1, s2; |
| रखता क्या है | Data का design | असली values |
Practical
एक class, दो objects
#include <iostream>
using namespace std;
class Student { // the blueprint: nothing exists yet
public:
int roll; // member variable
float marks; // member variable
void show() { // member function
cout << roll << " scored " << marks << endl;
}
};
int main() {
Student s1, s2; // two objects, NOW memory is allocated
s1.roll = 101; s1.marks = 78.5;
s2.roll = 102; s2.marks = 64.0;
s1.show(); // 101 scored 78.5
s2.show(); // 102 scored 64
return 0;
}
Think first
मन में trace कीजिए
s1 और s2 एक ही class Student के दो objects हैं। आप s1.roll = 101 set करते हैं, और बाद में s2.roll = 202 चलाते हैं। पाँच second लीजिए: अब s1.roll क्या है, और क्यों?
Show the answer
s1.roll अब भी 101 ही है। हर object को हर member variable की अपनी copy मिलती है, इसलिए s2 के ज़रिए लिखने से s1 पर कोई असर नहीं पड़ता।
सिर्फ member functions साझा होते हैं: show() की एक ही copy memory में रहती है और दोनों objects उसी से काम चलाते हैं। अगर आपने 202 सोचा था, तो आप class को एक साझा डिब्बा मान रहे थे, और इस topic की सबसे आम beginner गलती यही है।
Watch out
जाल: class के नाम को object की तरह इस्तेमाल करना
Student.roll = 101; compile नहीं होगी। Student एक type है, कोई चीज़ नहीं, ठीक वैसे ही जैसे x = 5; की जगह int = 5; लिखना।
पहले object बनाइए, फिर उसी object पर dot operator लगाइए: Student s1; और उसके बाद s1.roll = 101;। Viva में यही पकड़ना सबसे तेज़ तरीका है यह दिखाने का कि दोनों शब्दों का फ़र्क़ आपको साफ़ है।
Quiz
`class Student { int roll; float marks; };` अभी-अभी compile हुई है। इस समय `roll` और `marks` के लिए कितनी memory allocate हुई है?
- कुछ नहीं, क्योंकि अभी कोई object बना ही नहीं
- `roll` के लिए 4 bytes और `marks` के लिए 4 bytes
- एक Student जितनी, compiler ने पहले ही reserve कर ली
- यह इस पर निर्भर है कि आप आगे कितने objects बनाते हैं
Show the answer
कुछ नहीं, क्योंकि अभी कोई object बना ही नहीं
Class declaration सिर्फ एक type का वर्णन करती है, इसलिए allocate कुछ नहीं होता। "हर एक के लिए 4 bytes" और "एक Student जितनी", दोनों जवाब यह मान लेते हैं कि class खुद values रखती है, और इस topic की सबसे आम गलती यही है। आख़िरी option भावना में करीब है पर जगह गलत बताता है: class बढ़ती नहीं, हर Student s1; उसी वक़्त उस object की अपनी copy allocate करती है।
Formula
यह आगे कहाँ जाता है
आगे का सारा syllabus इन्हीं दो शब्दों के नियम हैं। Unit 2 तय करता है कि object के data को कौन छू सकता है (public, private, protected), फिर constructors और destructors के ज़रिए हर object को जन्म और अंत देता है। Unit 3 में एक ही call पर अलग-अलग classes के objects अलग जवाब देते हैं।
Exam में "define class and object with an example" 3 से 4 marks का standard सवाल है। दोनों definitions, एक छोटा code skeleton, और memory वाली line, तीनों लिखिए।
Summary
Key takeaways
- Class data members और member functions को एक user-defined type में बांधता है। यह blueprint है।
- Object class का instance है, और असल में memory वही लेता है।
- Class declare करने से कुछ allocate नहीं होता;
Student s1;सेs1के लिए होता है। - हर object अपने data members की अपनी copy रखता है, जबकि member functions एक ही बार store होकर साझा होते हैं।
- Members तक object के dot operator से पहुँचिए, class के नाम से कभी नहीं।
- याद रखने का तरीका: एक template, हज़ारों ID cards।