Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟣 OOP  ·  Lesson 38

Classes and Objects in C++

In this lesson: Create classes and objects in C++ — data members, member functions, access — examples with output. In Hindi & English.

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();.
← 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.
Next topicConstructors in C++ Your progress: 38 / 81 (47%) · Lesson 39 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free