C++ में Class और Object Real-Life Example के साथ
Class एक blueprint है (घर के नक्शे जैसा), object उससे बनी असली चीज़ (असली घर). Members, methods और output के साथ पूरा C++ example.
The house-map analogy (this makes it click)
Key point people miss: the map itself is not a house. You cannot live in a map. A class occupies no memory for data by itself — memory is used only when an object is created.
Writing your first class — every line explained
#include <iostream>
using namespace std;
class Student { // 'Student' is the blueprint's name
public: // public: accessible from outside
string name; // data member 1 (every object gets its own)
int marks; // data member 2
void display() { // member function: an action a Student can do
cout << name << " scored " << marks << endl;
}
}; // note the semicolon after class - required!
int main() {
Student s1; // OBJECT 1 born - memory allocated NOW
s1.name = "Aman"; // fill s1's own name
s1.marks = 92;
Student s2; // OBJECT 2 - completely separate memory
s2.name = "Priya";
s2.marks = 88;
s1.display(); // s1 shows ITS data
s2.display(); // s2 shows ITS data
return 0;
}
Notice: we wrote display() once, but each object used it on its own data. That is the magic — one blueprint, many independent houses.
The dot operator: how you enter the house
s1.name // s1's name ("Aman")
s2.name // s2's name ("Priya") - totally different variable!
s1.display() // ask s1 to perform its action
Read the dot as "ka/of": s1.name = "s1 ka name". The dot connects a specific object to its own member.
Why private exists (the real-world reason)
class BankAccount {
private: // hidden: outside code CANNOT touch
double balance;
public:
void deposit(double amt) {
if (amt > 0) balance += amt; // rule enforced here
}
double getBalance() { return balance; }
};
BankAccount acc;
// acc.balance = 999999; // ERROR: 'balance' is private
acc.deposit(5000); // only legal door into the data
balance were public, any code anywhere could write acc.balance = -50000; and corrupt the account. Making data private and allowing changes only through checked functions is called encapsulation — the first pillar of OOP. Default in a class is private; that is intentional protection.Class vs Object — the exam table
| Point | Class | Object |
|---|---|---|
| What is it | Blueprint / design | Real instance built from it |
| Memory | No data memory by itself | Gets memory when created |
| How many | Defined once | As many as you need |
| Example | House map, Student class | Actual house, s1 / s2 |
| Created by | class Student { }; | Student s1; |
घर के नक्शे वाली analogy (इससे concept बैठ जाएगा)
जो बात लोग miss करते हैं: नक्शा खुद घर नहीं है. नक्शे में रहा नहीं जा सकता. Class खुद data के लिए कोई memory नहीं लेती — memory तभी लगती है जब object बनता है.
पहली class लिखना — हर line समझाई हुई
#include <iostream>
using namespace std;
class Student { // 'Student' नक्शे का नाम है
public: // public: बाहर से access हो सकता है
string name; // data member 1 (हर object की अपनी)
int marks; // data member 2
void display() { // member function: Student का एक action
cout << name << " scored " << marks << endl;
}
}; // class के बाद semicolon - ज़रूरी!
int main() {
Student s1; // OBJECT 1 पैदा हुआ - memory ABHI मिली
s1.name = "Aman"; // s1 का अपना name भरो
s1.marks = 92;
Student s2; // OBJECT 2 - बिल्कुल अलग memory
s2.name = "Priya";
s2.marks = 88;
s1.display(); // s1 APNA data दिखाता है
s2.display(); // s2 APNA data दिखाता है
return 0;
}
ध्यान दें: display() एक बार लिखा, लेकिन हर object ने उसे अपने खुद के data पर use किया. यही magic है — एक नक्शा, कई independent घर.
Dot operator: घर में घुसने का दरवाज़ा
s1.name // s1 का name ("Aman")
s2.name // s2 का name ("Priya") - बिल्कुल अलग variable!
s1.display() // s1 से उसका action करवाओ
Dot को "का" पढ़िए: s1.name = "s1 का name". Dot किसी specific object को उसके अपने member से जोड़ता है.
private क्यों होता है (असली दुनिया की वजह)
class BankAccount {
private: // छुपा हुआ: बाहर का code touch NAHI कर सकता
double balance;
public:
void deposit(double amt) {
if (amt > 0) balance += amt; // rule यहां enforce होता है
}
double getBalance() { return balance; }
};
BankAccount acc;
// acc.balance = 999999; // ERROR: 'balance' is private
acc.deposit(5000); // data तक जाने का इकलौता legal दरवाज़ा
balance public होता, तो कहीं का भी code acc.balance = -50000; लिखकर account बिगाड़ सकता था. Data को private रखकर changes सिर्फ checked functions से allow करना encapsulation कहलाता है — OOP का पहला pillar. Class में default private है; यह जानबूझकर की गई protection है.Class vs Object — exam table
| Point | Class | Object |
|---|---|---|
| क्या है | Blueprint / design | उससे बना real instance |
| Memory | खुद data memory नहीं लेती | बनते ही memory मिलती है |
| कितने | एक बार define | जितने चाहिए उतने |
| Example | घर का नक्शा, Student class | असली घर, s1 / s2 |
| बनता कैसे है | class Student { }; | Student s1; |
Frequently Asked Questions
Class और object आसान शब्दों में क्या हैं?
Class एक blueprint है जो बताता है किसी चीज़ में क्या data और actions होंगे; object उस blueprint से बना real instance है जिसके पास data की अपनी copy होती है — जैसे नक्शे से बना असली घर.
क्या class memory लेती है?
Class definition खुद data members के लिए memory नहीं लेती; memory objects बनने पर मिलती है, और हर object को data members की अपनी अलग copy मिलती है.
Class members default में private क्यों होते हैं?
Encapsulation enforce करने के लिए — data protected रहता है और सिर्फ validation rules वाले public member functions से बदल सकता है, जिससे बाहर का code उसे corrupt नहीं कर पाता.