Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
🟣 OOP  ·  Lesson 38

Classes and Objects

Written and reviewed by · Senior IT Faculty · 15+ years’ experience

Classes and objects

Object-oriented programming lets you model things as self-contained types. A class is the blueprint — it says what data a thing has and what it can do. An object is one actual thing built from that blueprint. Car is a class; your specific car is an object.

Defining a class

A class holds data members (variables) and member functions (its behaviour):

C++
class Car {
public:
    string brand;      // data member
    int speed;         // data member

    void drive() {     // member function
        cout << brand << " is driving at " << speed << " km/h\n";
    }
};   // note the semicolon

Creating an object

C++
int main() {
    Car myCar;                 // an object
    myCar.brand = "Toyota";
    myCar.speed = 80;
    myCar.drive();             // call its behaviour
    return 0;
}
Output:
Toyota is driving at 80 km/h

You reach an object's members with the dot operator: myCar.brand, myCar.drive().

Member functions

Member functions can be defined inside the class (as above) or declared inside and defined outside using the scope operator :::

C++
class Car {
public:
    string brand;
    void drive();          // declaration only
};
void Car::drive() {        // definition outside
    cout << brand << " is driving\n";
}

Access: public and private

Access specifiers decide who can touch a member. By default, class members are private:

C++
class Account {
private:
    double balance = 0;        // hidden from outside
public:
    void deposit(double amt) { balance += amt; }
    double getBalance() { return balance; }
};

Outside code cannot touch balance directly — only through the public functions. See access specifiers for more.

Encapsulation

Keeping data private and exposing it only through public functions is called encapsulation. It protects the data (no one can set a negative balance directly) and lets you change the internals later without breaking outside code.

Where to go next

From here the OOP journey continues: constructors to initialise objects, inheritance to build new classes from old, and polymorphism for flexible behaviour.

Common mistakes

  • Forgetting the semicolon after the class's closing brace.
  • Expecting members to be public — they are private by default in a class.
  • Accessing private members from outside the class.
  • Forgetting ClassName:: when defining a member function outside the class.
🏋️ Practice

Write a Rectangle class with private width and height, public functions to set them, and an area() function. Create an object, set its size, and print the area.

Summary

  • A class is a blueprint; an object is an instance of it.
  • Classes hold data members and member functions.
  • Access members with the dot operator on an object.
  • Members are private by default; public ones form the interface.
  • Encapsulation hides data and exposes it through public functions.

Classes और objects

Object-oriented programming आपको चीज़ों को स्व-निहित types के रूप में मॉडल करने देता है। Class blueprint है — यह बताता है कि किसी चीज़ में क्या data है और वह क्या कर सकती है। Object उस blueprint से बना एक वास्तविक चीज़ है। Car एक class है; आपकी विशिष्ट car एक object है।

Class परिभाषित करना

Class में data members (variables) और member functions (इसका व्यवहार) होते हैं:

C++
class Car {
public:
    string brand;      // data member
    int speed;         // data member

    void drive() {     // member function
        cout << brand << " is driving at " << speed << " km/h\n";
    }
};   // semicolon dhyan dein

Object बनाना

C++
int main() {
    Car myCar;                 // ek object
    myCar.brand = "Toyota";
    myCar.speed = 80;
    myCar.drive();             // iska vyavhaar call karein
    return 0;
}
Output:
Toyota is driving at 80 km/h

आप object के members तक dot operator से पहुँचते हैं: myCar.brand, myCar.drive()

Member functions

Member functions class के अंदर (ऊपर की तरह) परिभाषित हो सकते हैं या अंदर declare करके बाहर scope operator :: से परिभाषित:

C++
class Car {
public:
    string brand;
    void drive();          // sirf declaration
};
void Car::drive() {        // bahar definition
    cout << brand << " is driving\n";
}

Access: public और private

Access specifiers तय करते हैं कि किसी member को कौन छू सकता है। Default रूप से, class members private होते हैं:

C++
class Account {
private:
    double balance = 0;        // bahar se chhupa hua
public:
    void deposit(double amt) { balance += amt; }
    double getBalance() { return balance; }
};

बाहरी code balance को सीधे नहीं छू सकता — केवल public functions के ज़रिए। अधिक के लिए access specifiers देखें।

Encapsulation

Data को private रखना और उसे केवल public functions के ज़रिए उजागर करना encapsulation कहलाता है। यह data की रक्षा करता है (कोई सीधे ऋणात्मक balance सेट नहीं कर सकता) और आपको बाद में बाहरी code तोड़े बिना internals बदलने देता है।

आगे कहाँ जाएँ

यहाँ से OOP यात्रा जारी रहती है: objects initialise करने को constructors, पुरानी से नई classes बनाने को inheritance, और लचीले व्यवहार के लिए polymorphism

आम गलतियाँ

  • Class के closing brace के बाद semicolon भूलना।
  • Members को public मानना — class में वे default रूप से private हैं।
  • Private members को class के बाहर से access करना।
  • Class के बाहर member function परिभाषित करते समय ClassName:: भूलना।
🏋️ अभ्यास

एक Rectangle class लिखें जिसमें private width और height, उन्हें सेट करने को public functions, और एक area() function हो। एक object बनाएँ, उसका size सेट करें, और area print करें।

सारांश

  • Class एक blueprint है; object उसका एक instance है।
  • Classes में data members और member functions होते हैं।
  • Object पर dot operator से members access करें।
  • Members default रूप से private हैं; public वाले interface बनाते हैं।
  • Encapsulation data छिपाता है और उसे public functions के ज़रिए उजागर करता है।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में class और object में क्या अंतर है?
Class एक blueprint है जो बताता है कि किसी type में क्या data और functions हैं, जबकि object उस blueprint से बनाया गया वास्तविक instance है। जैसे, Car एक class है, और Car myCar; से आपका बनाया एक विशिष्ट लाल car एक object है।
C++ में class क्या है?
Class एक user-defined type है जो data members (variables) और member functions (methods जो उस data पर काम करते हैं) को साथ बाँधता है। यह C++ में object-oriented programming की नींव है, आपको असली दुनिया की चीज़ों को स्व-निहित types के रूप में मॉडल करने देते हुए।
C++ classes में access specifiers क्या हैं?
Access specifiers नियंत्रित करते हैं कि class के किसी member को कौन इस्तेमाल कर सकता है। public members कहीं से भी पहुँच योग्य हैं, private members केवल class के अंदर से, और protected members class तथा इसकी derived classes से। Class members default रूप से private होते हैं।
C++ में encapsulation क्या है?
Encapsulation data और उस पर काम करने वाले functions को एक class के अंदर बाँधना, और आंतरिक data को private access के पीछे छिपाना है। बाहरी code केवल public functions के ज़रिए बातचीत करता है, जो data को सुरक्षित रखता है और आपको internals सुरक्षित रूप से बदलने देता है।
C++ में member function क्या है?
Member function एक class के अंदर परिभाषित function है जो class के data members तक पहुँच और उन पर काम कर सकता है। यह object के व्यवहार को दर्शाता है, और आप इसे object पर dot operator से call करते हैं, जैसे obj.start();
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.