Thuta Learning
C++
AdvancedProgrammingအခြေခံ

Access Specifiers

C++သင်ခန်းစာ 2125 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • public၊ private၊ protected တစ်ခုစီကို ဘယ်သူ access လုပ်နိုင်လဲ ပြောနိုင်ဖို့
  • Invariant ဆိုတာ ဘာလဲ ရှင်းပြပြီး private data နဲ့ public member function တွေက အဲဒါကို ဘယ်လို ကာကွယ်လဲ ပြနိုင်ဖို့
  • Access control က object တစ်ခုချင်းအလိုက် မဟုတ်ဘဲ class အလိုက် စစ်တာကို နားလည်ဖို့
  • Encapsulation ကို နာမည်အတွက်ပဲ ဖြစ်စေတဲ့ setter နဲ့ reference ပြန်ပေးတာလို ဒီဇိုင်း အမှားတွေကို ရှောင်နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

ဘဏ်တစ်ခုမှာ ငွေတိုက်ထဲ ဝင်ပြီး ကိုယ်တိုင် ငွေယူခွင့် ဘယ်သူမှ မရှိပါဘူး။ ကောင်တာက ဝန်ထမ်းကို တောင်းဆိုရပြီး ဝန်ထမ်းက အကောင့်ထဲ ငွေလုံလောက်လား အရင် စစ်ပါတယ်။ ငွေတိုက်က private data ဖြစ်ပြီး ကောင်တာက public member function ပါ။

C++ မှာ access specifier သုံးခု ရှိပါတယ်။ public: နောက်က member တွေကို ဘယ်သူမဆို သုံးလို့ ရပြီး private: နောက်က member တွေကို class ကိုယ်တိုင်ရဲ့ member function တွေကပဲ ထိလို့ ရပါတယ်။ protected: ကတော့ ကြားထဲမှာ ရှိပြီး class ကိုယ်တိုင်နဲ့ အဲဒီ class ကနေ ဆင်းသက်လာတဲ့ derived class တွေကို ခွင့်ပြုပါတယ်။ စည်းမျဉ်း ချိုးဖောက်တဲ့ code ကို compiler က compile မလုပ်ပေးပါဘူး။

Problem → Why → Solution

ပြဿနာ: ဘယ် code ကမဆို data ကို ပျက်စီးစေနိုင်တာ

Account balance ကို public field အဖြစ် ထားလိုက်ရင် program ထဲက function တိုင်းက စစ်ဆေးမှု မပါဘဲ ပြောင်းလို့ ရပါတယ်။ အောက်က payRent() က balance လုံလောက်လား မစစ်ဘဲ နုတ်လိုက်လို့ အကောင့်က အနုတ် ဖြစ်သွားပါတယ်။ Program ကြီးလာရင် ဒီလို line က file ရာချီထဲက တစ်နေရာရာမှာ ပုန်းနေနိုင်ပါတယ်။

public_balance.cpp
#include <iostream>

struct BankAccount {
    double balance = 0;          // public: any code may change it
};

void payRent(BankAccount& acc) {
    acc.balance -= 500;          // no check: the account goes negative
}

int main() {
    BankAccount acc;
    acc.balance = 100;
    payRent(acc);
    std::cout << acc.balance << '\n';   // -400
}

ဘာကြောင့် ဒါက ပြဿနာ ကြီးတာလဲ

"balance က အနုတ် မဖြစ်ရ" ဆိုတဲ့ စည်းမျဉ်းကို စောင့်ဖို့ တာဝန်က balance ကို ထိတဲ့ line တိုင်းမှာ ပြန့်ကျဲနေပါတယ်။ တစ်နေရာမှာ မေ့တာနဲ့ data ပျက်ပြီး bug ကို ရှာတဲ့အခါ ဘယ် line က ပြောင်းလိုက်လဲဆိုတာ program တစ်ခုလုံးမှာ လိုက်ရှာရပါတယ်။ balance ကို private လုပ်ပြီး withdraw() ကနေပဲ ပြောင်းခွင့်ပေးရင် စစ်ဆေးမှုက နေရာ တစ်ခုတည်းမှာ ရှိပြီး ကျော်ဖြတ်လို့ မရတော့ပါဘူး။

Core concept: invariant ကို class ကိုယ်တိုင် စောင့်ရှောက်တယ်

Access specifier တွေက class ထဲမှာ ရေးတဲ့ label တွေပါ။ Label တစ်ခုက နောက် label တစ်ခု ရောက်တဲ့အထိ ရှိတဲ့ member အားလုံးကို သက်ရောက်ပါတယ်။ စည်းမျဉ်းကို compile time မှာ စစ်တာမို့ main() ထဲက account.balance = 1e9; လို line က "'double BankAccount::balance' is private within this context" လို error နဲ့ build ကနေ ရပ်သွားပြီး program run ချိန်မှာ အပို ကုန်ကျစရိတ် လုံးဝ မရှိပါဘူး။

Specifierဘယ်သူ access လုပ်နိုင်လဲပုံမှန် သုံးပုံ
publicဘယ် code မဆိုClass ရဲ့ interface (constructor၊ operation)
protectedClass ကိုယ်တိုင်နဲ့ derived class များDerived class အတွက် helper function
privateClass ကိုယ်တိုင်ရဲ့ member နဲ့ friend များData member နဲ့ internal helper

Access control ကို ဘာကြောင့် သုံးလဲဆိုတော့ invariant တွေကြောင့်ပါ။ Invariant ဆိုတာ object တစ်ခု construct လုပ်ပြီးတဲ့ အချိန်ကနေ ဖျက်တဲ့ အချိန်အထိ အမြဲ မှန်နေရမယ့် စည်းမျဉ်းပါ — balance က အနုတ် မဖြစ်ရ၊ Fraction ရဲ့ denominator က 0 မဖြစ်ရ၊ sorted list က sorted ဖြစ်နေရ စတာတွေပါ။ Data ကို private ထားလိုက်ရင် အဲဒီ data ကို ပြောင်းနိုင်တဲ့ code က class ရဲ့ member function တွေ ဖြစ်တဲ့ နေရာ အနည်းငယ်မှာပဲ ရှိပါတော့တယ်၊ ဒါကြောင့် စည်းမျဉ်းကို အဲဒီ နေရာတွေမှာ စစ်ရုံနဲ့ object တိုင်းအတွက် အာမခံ ပေးနိုင်ပါတယ်။ ဒါကို encapsulation လို့ ခေါ်ပါတယ်။

Constructor ကလည်း invariant စတင်တဲ့ နေရာ ဖြစ်တာကို မမေ့ပါနဲ့။ deposit() က အနုတ် amount ကို ငြင်းပေမယ့် constructor က BankAccount account(-100); ကို လက်ခံရင် object က မွေးကတည်းက စည်းမျဉ်း ချိုးထားပြီးသား ဖြစ်နေပါတယ်။ class နဲ့ struct ရဲ့ ကွာခြားချက်ကိုလည်း သတိရပါ — class ထဲမှာ label မရေးရင် private ဖြစ်ပြီး struct ထဲမှာ public ဖြစ်ပါတယ်၊ ဒါကြောင့် invariant ရှိတဲ့ type တွေကို class နဲ့ ရေးလေ့ ရှိပါတယ်။

Access ကို object တစ်ခုချင်း မဟုတ်ဘဲ class အလိုက် စစ်ပါတယ်။ BankAccount ရဲ့ member function တစ်ခုက ကိုယ့် object ရဲ့ private member ကိုသာမက parameter အဖြစ် ရလာတဲ့ တခြား BankAccount ရဲ့ private member ကိုလည်း ထိလို့ ရပါတယ်၊ ဒါကြောင့် transfer လို operation တွေကို class ထဲမှာ ရေးလို့ ရပါတယ်။

မှတ်ထားရမယ့် ပုံစံ: တံခါးစောင့်ပါတဲ့ data
  1. Caller က public function ကို ခေါ်
  2. Function က invariant ကို စစ်
  3. စစ်ပြီးမှ private data ကို ပြောင်း
  4. တိုက်ရိုက် ထိရင် compile error

Private data ကို ပြောင်းနိုင်တဲ့ လမ်းကြောင်း အားလုံးက class ထဲမှာပဲ ရှိလို့ စစ်ဆေးမှုကို ကျော်လို့ မရပါဘူး။

အထွေထွေ ပုံစံ

cpp
class ClassName {
public:      // the interface: any code may call these
    bool change(Type amount);          // checks the invariant first
    Type current() const;              // read-only view of the state
protected:   // this class and classes derived from it
    void helperForDerived();
private:     // only this class's own member functions (and friends)
    Type state_ = initialValue;        // guarded by the invariant
    void internalHelper();
};

Public section မှာ class ကို အသုံးပြုသူတွေ တကယ် လိုအပ်တဲ့ operation တွေကိုပဲ ထားပြီး data တွေကို private section ထဲ ထည့်ပါ။ Data ကို ပြောင်းတဲ့ function တွေက စည်းမျဉ်း စစ်ပြီး ဖတ်ရုံ function တွေကို const လုပ်ပါ။ protected ကို derived class တွေအတွက် ရည်ရွယ်ချက်ရှိရှိ ဖွင့်ပေးတဲ့ helper တွေအတွက်ပဲ သုံးပါ။

Example: private balance ပါတဲ့ BankAccount

ဒီ example မှာ balance ကို private ထားပြီး deposit() နဲ့ getBalance() ကို public ထားပါတယ်။ main() က balance ကို တိုက်ရိုက် မထိဘဲ public function တွေကနေပဲ သုံးတာကို ကြည့်ပါ။ Breakdown ထဲမှာ ဒီ class ကို ဘယ်နေရာတွေမှာ ပိုခိုင်မာအောင် လုပ်နိုင်လဲ ဆိုတာလည်း ထောက်ပြထားပါတယ်။

cpp
#include <iostream>
using namespace std;

class BankAccount {
  private:
    double balance;

  public:
    BankAccount(double startBalance) {
        balance = startBalance;
    }

    void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    double getBalance() {
        return balance;
    }
};

int main() {
    BankAccount account(100);
    account.deposit(50);
    cout << account.getBalance();
    return 0;
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Lines 5-6double balance;

    private: label အောက်မှာ ရှိလို့ BankAccount ရဲ့ member function တွေကပဲ balance ကို နာမည်နဲ့ ခေါ်လို့ ရပါတယ်။ main() ထဲမှာ account.balance လို့ ရေးတာ compile error ဖြစ်ပါတယ်။

  2. Line 9BankAccount(double startBalance) {

    Constructor ကို public ထားမှ main() က object create လုပ်နိုင်ပါတယ်။ ဒါပေမယ့် startBalance ကို မစစ်လို့ BankAccount account(-100); ကို လက်ခံနေပါတယ် — invariant ကို တကယ် စောင့်ချင်ရင် ဒီမှာလည်း စစ်ရပါမယ်။

  3. Line 14if (amount > 0) {

    Deposit စည်းမျဉ်းက ဒီ line တစ်ကြောင်းတည်းမှာ ရှိပါတယ်။ balance ကို ပြောင်းနိုင်တဲ့ အပြင်လမ်း မရှိလို့ amount အနုတ် ဒါမှမဟုတ် 0 ကို ဘယ်တော့မှ မထည့်မိပါဘူး။

  4. Line 19double getBalance() {

    ဖတ်ခွင့်ပဲ ပေးပြီး ရေးခွင့် မပေးပါဘူး။ Object ကို မပြောင်းလို့ double getBalance() const လို့ ရေးသင့်ပါတယ်၊ ဒါမှ const BankAccount& ကနေလည်း ခေါ်လို့ ရပါမယ်။

  5. Line 27cout << account.getBalance();

    main() က balance ကို public function ကနေ ဖတ်ပါတယ်။ 100 နဲ့ စပြီး 50 ထည့်လို့ 150 ထွက်ပါတယ်။

You should see
150

Public interface ကို ဒီဇိုင်းဆွဲခြင်း

အောက်က version မှာ balance ကို double အစား integer cent (long long) နဲ့ သိမ်းပါတယ်၊ double က 0.1 လို value တွေကို အတိအကျ မသိမ်းနိုင်လို့ ငွေအတွက် မသင့်တော်ပါဘူး။ Balance က 0 ကနေ စပြီး မှားတဲ့ request တွေကို withdraw() နဲ့ deposit() က false ပြန်ပြီး ငြင်းပါတယ်။ transferTo() က တခြား account ရဲ့ private cents_ ကို တိုက်ရိုက် ထိနေတာကို သတိထားပါ — class တူလို့ ခွင့်ပြုတာပါ။

account.cpp
#include <format>
#include <iostream>
#include <string>

class BankAccount {
public:
    explicit BankAccount(std::string owner) : owner_(owner) {}

    bool deposit(long long cents) {
        if (cents <= 0) return false;
        cents_ += cents;
        return true;
    }

    bool withdraw(long long cents) {
        if (cents <= 0 || cents > cents_) return false;   // never below zero
        cents_ -= cents;
        return true;
    }

    bool transferTo(BankAccount& other, long long cents) {
        if (!withdraw(cents)) return false;
        other.cents_ += cents;     // allowed: access is checked per class
        return true;
    }

    long long balance() const { return cents_; }
    const std::string& owner() const { return owner_; }

private:
    std::string owner_;
    long long cents_ = 0;          // invariant: cents_ >= 0
};

void show(const BankAccount& a) {
    std::cout << std::format("{}: {}.{:02}\n", a.owner(), a.balance() / 100,
                             a.balance() % 100);
}

int main() {
    BankAccount aye("Aye");
    BankAccount bo("Bo");
    aye.deposit(15000);
    std::cout << std::boolalpha;
    std::cout << "withdraw 200.00: " << aye.withdraw(20000) << '\n';
    std::cout << "transfer 40.00: " << aye.transferTo(bo, 4000) << '\n';
    show(aye);
    show(bo);
    // aye.cents_ = -500;   error: 'cents_' is private within this context
}

Output က "withdraw 200.00: false"၊ "transfer 40.00: true"၊ "Aye: 110.00"၊ "Bo: 40.00" ဖြစ်ပါတယ်။ setBalance() လို function မပါတာကို သတိထားပါ — အပြင်ကနေ balance ကို ကြိုက်တဲ့ value ပြောင်းခွင့်ပေးလိုက်ရင် private ဖြစ်နေတာ အဓိပ္ပာယ် မရှိတော့ပါဘူး။ Public interface မှာ deposit၊ withdraw၊ transfer လို တကယ့် operation တွေပဲ ရှိပါတယ်။

protected၊ friend နဲ့ access control က ဘာ မဟုတ်လဲ

protected member တွေကို derived class တွေက သုံးလို့ ရပါတယ် (inheritance ကို နောက် lesson မှာ လေ့လာပါမယ်)။ ဒါပေမယ့် protected data က class ကနေ ဆင်းသက်လာတဲ့ ဘယ် class ကမဆို ပြောင်းနိုင်လို့ invariant ကာကွယ်မှု အားနည်းပါတယ်။ Data ကို base class မှာတောင် private ထားပြီး derived class တွေ လိုအပ်တာကို protected member function အဖြစ် ပေးတာက ပိုလုံခြုံပါတယ်။

friend ကတော့ သတ်မှတ်ထားတဲ့ function သို့မဟုတ် class တစ်ခုကို private member တွေ ထိခွင့်ပေးတာပါ။ operator<< လို class နဲ့ နီးနီးကပ်ကပ် အလုပ်လုပ်ရတဲ့ function တွေအတွက် အသုံးဝင်ပေမယ့် friend တိုင်းက invariant ကို ချိုးနိုင်တဲ့ နေရာ အသစ် ဖြစ်လို့ နည်းနည်းပဲ သုံးပါ။

private က security မဟုတ်ပါ

Access control က programmer အမှားတွေကို compile time မှာ ဖမ်းပေးတဲ့ tool ပါ။ Private member တွေကလည်း memory ထဲမှာ ရှိနေတာ အတူတူပဲ ဖြစ်ပြီး pointer နဲ့ ဖတ်တာ၊ debugger နဲ့ ကြည့်တာ၊ memory dump ထဲမှာ တွေ့တာ ဖြစ်နိုင်ပါတယ်။ Password နဲ့ key လို လျှို့ဝှက်ချက်တွေကို private ဖြစ်လို့ လုံခြုံတယ်လို့ မယူဆပါနဲ့။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

std::vector က encapsulation ရဲ့ ကောင်းတဲ့ ဥပမာပါ။ အထဲမှာ data pointer၊ size နဲ့ capacity ကို private ထားလို့ size ကို 1000 လို့ တိုက်ရိုက် ပြောင်းပြီး memory မရှိတဲ့ element တွေကို ယူသုံးတာမျိုး ဖြစ်မလာပါဘူး။ push_back()၊ resize() လို public function တွေက memory ကို လိုသလို ချဲ့ပြီး size နဲ့ capacity ကို အမြဲ ကိုက်ညီအောင် ထိန်းပါတယ်။ Thread-safe class တွေမှာလည်း data နဲ့ mutex ကို private ထားပြီး member function တိုင်းက lock လုပ်ပြီးမှ data ကို ထိလို့ "lock မလုပ်ဘဲ ထိမိတာ" မဖြစ်နိုင်တော့ပါဘူး။

Encapsulation က code ပြောင်းလဲမှုကိုလည်း လွယ်ကူစေပါတယ်။ Private member တွေကို ပြောင်းတာ (double ကနေ integer cent ပြောင်းတာလို) က class ကို သုံးတဲ့ code ကို ပြင်စရာ မလိုပါဘူး၊ public interface မပြောင်းသမျှ ပြန် compile လုပ်ရုံပါပဲ။ Library တွေက ပြန် compile လုပ်တာကိုပါ ရှောင်ချင်ရင် private member တွေကို .cpp file ထဲက သီးခြား struct ထဲ ရွှေ့တဲ့ pImpl (pointer to implementation) idiom ကို သုံးကြပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. ဘာမဆို လက်ခံတဲ့ setter ထည့်တာ

balance_ ကို private လုပ်ပြီး void setBalance(double b) { balance_ = b; } ကို public ထည့်လိုက်ရင် အပြင်က code က setBalance(-400) လို့ ခေါ်လို့ ရနေတုန်းပါ — encapsulation က နာမည်အတွက်ပဲ ကျန်ပါတော့တယ်။ Field တစ်ခုစီအတွက် getter/setter အတွဲ ထုတ်မပေးဘဲ withdraw() လို အဓိပ္ပာယ်ရှိတဲ့ operation တွေကို ပေးပါ။

2. Private data ကို non-const reference နဲ့ ပြန်ပေးတာ

std::vector<Item>& items() { return items_; } လို function က caller ကို items_ ကို clear() လုပ်တာ၊ စစ်ဆေးမှု မပါဘဲ ထည့်တာတွေ လုပ်ခွင့် ပေးလိုက်ပါတယ်။ ဖတ်ဖို့ပဲ လိုရင် const std::vector<Item>& items() const ကို ပြန်ပေးပါ၊ ပြောင်းဖို့ လိုရင် addItem() လို သီးခြား operation ကို ပေးပါ။

3. Constructor မှာ invariant မစစ်တာ

Member function တိုင်းက စည်းမျဉ်း စစ်နေပေမယ့် constructor က BankAccount(-100) ကို လက်ခံရင် object က ပထမဆုံး အခိုက်ကတည်းက invalid ဖြစ်နေပါတယ်။ Constructor ထဲမှာ စစ်ပါ (lesson 24 ရဲ့ exception တွေက ဒီအတွက် သင့်တော်ပါတယ်) ဒါမှမဟုတ် ဒီ lesson ရဲ့ deeper example လို 0 ကနေ စပြီး deposit() ကနေပဲ ငွေထည့်ခွင့် ပေးပါ။

4. Data အားလုံးကို protected လုပ်ထားတာ

"နောက်ပိုင်း derived class တွေ လိုမှာ" ဆိုပြီး data member အားလုံးကို protected လုပ်ထားရင် derived class တိုင်းက invariant ကို ကျော်ပြီး ပြောင်းနိုင်ပါတယ်။ Default အနေနဲ့ private ထားပြီး တကယ် လိုအပ်မှ protected member function ကို ဖွင့်ပေးပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Invariant ရှိတဲ့ type တိုင်းမှာ data member တွေကို private ထားပါ၊ invariant မရှိတဲ့ data အစုအဝေး သက်သက်ကိုတော့ public member ပါတဲ့ struct အဖြစ် ထားလို့ ရပါတယ်။
  • Public section ကို class ရဲ့ အပေါ်ဆုံးမှာ ထားပါ — ဖတ်တဲ့သူက class ကို ဘယ်လို သုံးရမလဲ အရင် သိချင်လို့ပါ။
  • Private data member တွေကို cents_ လို suffix ဒါမှမဟုတ် team convention တစ်ခုနဲ့ ခွဲခြားပြီး parameter နာမည်နဲ့ မရောအောင် လုပ်ပါ။
  • ဖတ်ရုံ member function တိုင်းကို const လုပ်ပါ။

Mini challenge

Mini challenge: အသံ အတိုးအကျယ် ထိန်းတဲ့ class

TV remote ရဲ့ volume ကို ထိန်းတဲ့ VolumeControl class ရေးပါ။ Volume level က ဘယ်တော့မှ 0 အောက် မရောက်ရ၊ 100 အထက် မကျော်ရပါဘူး၊ mute လုပ်ပြီး unmute ပြန်လုပ်ရင် အရင် level ပြန်ရရပါမယ်။

  • level_ (စတင်တန်ဖိုး 50) နဲ့ muted_ (စတင်တန်ဖိုး false) ကို private ထားရမယ်
  • level_ ကို ရေးတဲ့ code အားလုံး std::clamp သုံးတဲ့ private helper set() တစ်ခုတည်းကို ဖြတ်သွားရမယ်
  • up(int)၊ down(int)၊ mute()၊ unmute()၊ level() const၊ report() const တို့ကို public ပေးရမယ်
  • Mute ဖြစ်နေရင် level() က 0 ပြန်ပြီး report() က "(muted)" ကို ထပ်ပြရမယ်
volume.cpp
#include <algorithm>   // std::clamp
#include <iostream>

class VolumeControl {
public:
    void up(int step) { /* TODO */ }
    void down(int step) { /* TODO */ }
    void mute() { /* TODO */ }
    void unmute() { /* TODO */ }

    int level() const { /* TODO: 0 while muted */ }
    void report() const { /* TODO: "volume N" plus " (muted)" */ }

private:
    // TODO: a helper that is the ONLY code writing the level (keep 0..100)
    // TODO: the level (starts at 50) and the muted flag
};

int main() {
    // TODO: up 20, up 50, mute, unmute, down 150 - report after each step
}

ရလဒ်: volume 70 volume 100 volume 0 (muted) volume 100 volume 0

အရိပ်အမြွက် ကြည့်ရန်

std::clamp(value, 0, 100) က value ကို 0 နဲ့ 100 ကြားထဲ ညှပ်ပေးပါတယ်။ Mute လုပ်တဲ့အခါ level_ ကို 0 မပြောင်းပါနဲ့ — flag ကိုပဲ ပြောင်းပြီး level() ထဲမှာ muted_ ? 0 : level_ လို့ ပြန်ပေးရင် unmute က အရင် level ကို အလိုအလျောက် ပြန်ရပါတယ်။

အဖြေ ကြည့်ရန်
volume.cpp
#include <algorithm>
#include <iostream>

class VolumeControl {
public:
    void up(int step) { set(level_ + step); }
    void down(int step) { set(level_ - step); }
    void mute() { muted_ = true; }
    void unmute() { muted_ = false; }

    int level() const { return muted_ ? 0 : level_; }

    void report() const {
        std::cout << "volume " << level() << (muted_ ? " (muted)" : "") << '\n';
    }

private:
    void set(int value) { level_ = std::clamp(value, 0, 100); }  // only writer

    int level_ = 50;        // invariant: 0 <= level_ <= 100
    bool muted_ = false;
};

int main() {
    VolumeControl tv;
    tv.up(20);
    tv.report();
    tv.up(50);
    tv.report();
    tv.mute();
    tv.report();
    tv.unmute();
    tv.report();
    tv.down(150);
    tv.report();
}

Quick Quiz 1 / 3

class Wallet { public: long long balance() const; private: long long cents_ = 0; }; ရှိရင် main() ထဲက ဘယ် line က compile ဖြစ်လဲ။

Quick Quiz 2 / 3

BankAccount::transferTo(BankAccount& other, long long c) ထဲမှာ other.cents_ += c; လို့ ရေးရင် (cents_ က private) ဘာဖြစ်မလဲ။

Quick Quiz 3 / 3

"quantity က အနုတ် မဖြစ်ရ" ဆိုတဲ့ invariant ကို တကယ် ကာကွယ်ပေးတဲ့ ဒီဇိုင်းက ဘာလဲ။

မှတ်ထားရမယ့် အချက်များ

  • public က ဘယ်သူမဆို၊ protected က class နဲ့ derived class များ၊ private က class ကိုယ်တိုင်ပဲ access လုပ်နိုင်ပါတယ်၊ စည်းမျဉ်းကို compile time မှာ စစ်ပါတယ်။
  • Data ကို private ထားပြီး စည်းမျဉ်း စစ်တဲ့ public operation တွေကနေပဲ ပြောင်းခွင့်ပေးတာက invariant ကို အာမခံပေးတဲ့ encapsulation ပါ။
  • Constructor ကလည်း invariant ကို စတင်ရမယ့် နေရာ ဖြစ်ပြီး access ကို class အလိုက် စစ်ပါတယ်။
  • ဘာမဆို လက်ခံတဲ့ setter နဲ့ non-const reference ပြန်ပေးတာက encapsulation ကို ဖျက်ပစ်ပါတယ်၊ private က security tool တော့ မဟုတ်ပါဘူး။

The Current ISO C++ Standard (C++23) — Standard C++ Foundation

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Access Specifiers | Thuta Learning