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):
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 semicolonCreating an object
int main() {
Car myCar; // an object
myCar.brand = "Toyota";
myCar.speed = 80;
myCar.drive(); // call its behaviour
return 0;
}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 :::
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:
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
privateby default in a class. - Accessing private members from outside the class.
- Forgetting
ClassName::when defining a member function outside the class.
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
privateby default;publicones 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++?
Car is a class, and a specific red car you make with Car myCar; is an object.What is a class in C++?
What are access specifiers in C++ classes?
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++?
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++?
obj.start();.