Classes — member functions, access specifiers (public, private, protected)
5.2.5· Coding › C++ Programming
1. Class kya hai? (WHAT)
class BankAccount {
private: // walls go up here
double balance; // member variable (state)
public:
void deposit(double amount); // member function (behaviour)
double getBalance() const;
};WHY class ko object se alag karte hain? Ek blueprint (class) se laakhon objects stamp out ho sakte hain, jinmein har ek ke paas member variables ki apni copy hoti hai lekin sab same member-function code share karte hain.
2. Access specifiers — teen diwaare (WHAT + WHY)
| Specifier | Same class | Derived class | Outside (main) |
|---|---|---|---|
| public | ✅ | ✅ | ✅ |
| protected | ✅ | ✅ | ❌ |
| private | ✅ | ❌ | ❌ |

3. Member functions (HOW)
Class ke andar vs bahar define karna
class BankAccount {
private:
double balance = 0;
public:
// (a) INSIDE define kiya → implicitly inline
double getBalance() const { return balance; }
// (b) andar declare, OUTSIDE define kiya scope resolution :: ke saath
void deposit(double amount);
};
void BankAccount::deposit(double amount) { // :: ties it to the class
balance += amount; // 'balance' really means this->balance
}4. Pura worked example (Derivation-from-scratch)
#include <iostream>
using namespace std;
class Rectangle {
private: // STEP 1: hide raw data
double width, height;
public:
// STEP 2: controlled way to set state (validates input!)
void setDimensions(double w, double h) {
if (w < 0 || h < 0) { width = height = 0; return; }
width = w; height = h;
}
// STEP 3: read-only computed property
double area() const { return width * height; }
};
int main() {
Rectangle r;
r.setDimensions(3, 4); // ✅ public gateway
cout << r.area(); // prints 12
// r.width = -5; // ❌ blocked: cannot create invalid state
}STEP 1 kyun? Agar width/height public hote, toh koi bhi negative width set kar sakta tha → ek impossible rectangle.
STEP 2 validate kyun karta hai? Data mein jaane ka ek hi darwaza ek guard ka bhi kaam karta hai — invalid states unrepresentable ho jaate hain.
area() const kyun hai? Area calculate karna rectangle ko change nahi karta; ise const mark karna yeh document aur enforce karta hai.
5. Common mistakes (Steel-man + fix)
6. The 80/20 core
Recall Feynman: ek 12-saal ke bachche ko samjhao
Ek vending machine socho. Andar paise aur snacks hain (woh private data) — tum andar haath daalkaar nahi le sakte. Bahar buttons hain (woh public functions): "coin daalo", "B4 dabaao". Tum sirf buttons ke through interact karte ho, aur machine ensure karti hai ki tum chura ya tod nahi sakte. Ek class ek vending machine ka design hai; hallway mein har actual machine ek object hai. Private = box ke andar band cheezein. Public = buttons. Protected = ek secret repair panel jo sirf machine ki repair-machines ki family khol sakti hai, lekin customers nahi.
Connections
- Encapsulation & Data Hiding
- Constructors and Destructors
- Inheritance in C++ — jahaan
protectedessential ban jaata hai - const correctness
- friend functions and classes
- this pointer
- struct vs class
Flashcards
C++ mein teen access specifiers kya hain?
class mein default access level kya hai?
struct mein default access level kya hai?
private member ko kahaan access kiya ja sakta hai?
protected kya extra deta hai private ke upar?
C++ mein access per-class hai ya per-object?
Hidden this pointer kis taraf point karta hai?
Member function signature ke baad const kya guarantee karta hai?
Bahar define ki gayi function ko uski class se kaun sa operator jodhta hai?
::