Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · C++ · 04 Jul 2026 · Hindi + English

C++ में Class और Object Real-Life Example के साथ

Class एक blueprint है (घर के नक्शे जैसा), object उससे बनी असली चीज़ (असली घर). Members, methods और output के साथ पूरा C++ example.

The house-map analogy (this makes it click)

A class is a map/blueprint of a house. An object is the actual house built from that map. From one map you can build 10 houses — each house is separate, has its own rooms, its own paint colour, its own family living inside. Similarly, from one Student class you can create s1, s2, s3... each object with its own name and marks, completely independent of each other.

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;
}
Aman scored 92 Priya scored 88

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
If 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

PointClassObject
What is itBlueprint / designReal instance built from it
MemoryNo data memory by itselfGets memory when created
How manyDefined onceAs many as you need
ExampleHouse map, Student classActual house, s1 / s2
Created byclass Student { };Student s1;

घर के नक्शे वाली analogy (इससे concept बैठ जाएगा)

Class घर का नक्शा (blueprint) है. Object उस नक्शे से बना असली घर. एक नक्शे से 10 घर बन सकते हैं — हर घर अलग है, अपने कमरे, अपना paint colour, अंदर अपनी family. वैसे ही एक Student class से s1, s2, s3... बना सकते हैं — हर object का अपना name और marks, एक-दूसरे से पूरी तरह independent.

जो बात लोग 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;
}
Aman scored 92 Priya scored 88

ध्यान दें: 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

PointClassObject
क्या है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 नहीं कर पाता.