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.

Frequently Asked Questions

What is the difference between a class and an object in C++?
A class is a blueprint that describes what data and functions a type has, while an object is an actual instance created from that blueprint. For example, Car is a class, and a specific red car you make with Car myCar; is an object.
What is a class in C++?
A class is a user-defined type that bundles data members (variables) and member functions (methods that act on that data) together. It is the foundation of object-oriented programming in C++, letting you model real-world things as self-contained types.
What are access specifiers in C++ classes?
Access specifiers control who can use a class member. public members are accessible from anywhere, private members only from inside the class, and protected members from the class and its derived classes. Class members are private by default.
What is encapsulation in C++?
Encapsulation is bundling data and the functions that operate on it inside one class, and hiding the internal data behind private access. Outside code interacts only through public functions, which keeps the data protected and lets you change the internals safely.
What is a member function in C++?
A member function is a function defined inside a class that can access and operate on the class's data members. It represents the behaviour of the object, and you call it on an object with the dot operator, such as obj.start();.

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 के ज़रिए उजागर करता है।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.