Modern C++ और C Alternatives

ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग (OOP)

C++ classes और virtual functions देता है; C structs और function pointers से समान संबंध बनाता है।

C++ में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग क्या है?

OOP state, invariants और behavior वाले objects के आसपास program बनाता है। C++ classes, access control, inheritance और virtual dispatch देता है; C इन्हें स्पष्ट रूप से बनाता है।

OOP के चार मुख्य सिद्धांत

  • Encapsulation internal state नियंत्रित करके invariants बचाता है।
  • Abstraction छोटा public interface देता है।
  • Inheritance valid base contract को specialize करता है।
  • Polymorphism virtual call से concrete behavior चुनता है।

C और C++ में design

C

C struct और function pointers जोड़ता है; conventions और API invariants तथा ownership संभालते हैं।

C++

C++ private state, inheritance और override उपयोग करता है जिन्हें compiler जाँचता है।

डिज़ाइन सुझाव: Polymorphic base में virtual destructor रखें, owning raw pointers से बचें और composition को प्राथमिकता दें।

C और C++ code examples

C
कोड चलाएँ →
main.c
#include <stdio.h>

typedef struct Account Account;
typedef void (*MonthEnd)(Account *account);

struct Account {
    const char *owner;
    double balance;
    double interest_rate;
    MonthEnd month_end;
};

void standard_month_end(Account *account) {
    (void)account;
}

void savings_month_end(Account *account) {
    account->balance *= 1.0 + account->interest_rate;
}

void print_account(Account *account) {
    account->month_end(account);
    printf("%s: %.2f\n", account->owner, account->balance);
}

int main(void) {
    Account accounts[] = {
        {"Ada", 150.0, 0.0, standard_month_end},
        {"Lin", 200.0, 0.05, savings_month_end}
    };

    for (int i = 0; i < 2; ++i) {
        print_account(&accounts[i]);
    }
    return 0;
}
अपेक्षित output
Ada: 150.00
Lin: 210.00
C++
कोड चलाएँ →
main.cpp
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

class Account {
    std::string owner_;
    double balance_;

protected:
    void add_balance(double amount) { balance_ += amount; }

public:
    Account(std::string owner, double balance)
        : owner_(std::move(owner)), balance_(balance) {}

    virtual ~Account() = default;
    virtual void month_end() {}

    const std::string& owner() const { return owner_; }
    double balance() const { return balance_; }
};

class SavingsAccount final : public Account {
    double interest_rate_;

public:
    SavingsAccount(std::string owner, double balance, double rate)
        : Account(std::move(owner), balance), interest_rate_(rate) {}

    void month_end() override {
        add_balance(balance() * interest_rate_);
    }
};

int main() {
    Account standard("Ada", 150.0);
    SavingsAccount savings("Lin", 200.0, 0.05);
    std::vector<Account *> accounts{&standard, &savings};

    std::cout << std::fixed << std::setprecision(2);
    for (Account *account : accounts) {
        account->month_end();
        std::cout << account->owner() << ": "
                  << account->balance() << '\n';
    }
}
अपेक्षित output
Ada: 150.00
Lin: 210.00

C और C++ की तुलना

C version struct में state और function pointer रखता है। C++ balance को private रखता, common interface inherit करता और virtual method override करता है। client को concrete type जाँचने की जरूरत नहीं।

C

C prefixes, callbacks, macros और context structs उपयोग करता है।

C++

Language scoped और type-safe abstractions देता है।

अभ्यास

दोनों versions चलाकर बदलें और language guarantees की तुलना करें।

  • पहले C mechanism लिखें।
  • Manual cleanup को RAII से बदलें।
  • Allocation और virtual dispatch जाँचें।