5.2.5 · HinglishC++ Programming

Classes — member functions, access specifiers (public, private, protected)

1,646 words7 min readRead in English

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
Figure — Classes — member functions, access specifiers (public, private, protected)

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


Flashcards

C++ mein teen access specifiers kya hain?
public, private, protected
class mein default access level kya hai?
private
struct mein default access level kya hai?
public
private member ko kahaan access kiya ja sakta hai?
Sirf class ke apne member functions ke andar (aur friends)
protected kya extra deta hai private ke upar?
Derived (child) classes se bhi access milta hai
C++ mein access per-class hai ya per-object?
Per-class — ek object ka method doosre same-class object ke private members padh sakta hai
Hidden this pointer kis taraf point karta hai?
Us object ki taraf jis par member function call hua
Member function signature ke baad const kya guarantee karta hai?
Function object ke members ko modify nahi karega
Bahar define ki gayi function ko uski class se kaun sa operator jodhta hai?
Scope resolution operator ::
Data ko public functions ke peeche kyun chupaate hain?
Invariants/validation enforce karne ke liye aur invalid states ko unrepresentable banane ke liye (encapsulation)
Kya tum ek const object par non-const member function call kar sakte ho?
Nahi — sirf const member functions hi

Concept Map

instantiates

bundles

bundles

plus behaviour equals

plus data equals

enforced by

open to all

class only

class plus children

default for

carries hidden

points to

safe API like deposit

Class blueprint

Object instance

Member variables

Member functions

Encapsulation

Access specifiers

public

private

protected

this pointer