Classes and Objects
Written and reviewed by Gagan Bhardwaj · 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):
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();.Classes और objects
Object-oriented programming आपको चीज़ों को स्व-निहित types के रूप में मॉडल करने देता है। Class blueprint है — यह बताता है कि किसी चीज़ में क्या data है और वह क्या कर सकती है। Object उस blueprint से बना एक वास्तविक चीज़ है। Car एक class है; आपकी विशिष्ट car एक object है।
Class परिभाषित करना
Class में data members (variables) और member functions (इसका व्यवहार) होते हैं:
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 deinObject बनाना
int main() {
Car myCar; // ek object
myCar.brand = "Toyota";
myCar.speed = 80;
myCar.drive(); // iska vyavhaar call karein
return 0;
}Toyota is driving at 80 km/h
आप object के members तक dot operator से पहुँचते हैं: myCar.brand, myCar.drive()।
Member functions
Member functions class के अंदर (ऊपर की तरह) परिभाषित हो सकते हैं या अंदर declare करके बाहर scope operator :: से परिभाषित:
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 होते हैं:
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 के ज़रिए उजागर करता है।