ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Variable ဆိုတာ type တစ်မျိုးရဲ့ value တစ်ခုကို ကိုင်ထားတဲ့ နာမည်ပေးထားသော memory အပိုင်းလေးပါ။ int age{20}; လို့ ရေးလိုက်ရင် int တစ်ခု ဆံ့လောက်တဲ့ နေရာတစ်ခု တောင်းပြီး age လို့ label ကပ်ကာ 20 ကို ထည့်လိုက်တာပါ။ နောက်ပိုင်း value ကို ဖတ်နိုင်၊ print ထုတ်နိုင်၊ value အသစ်နဲ့ အစားထိုးနိုင်ပေမယ့် variable ရဲ့ သက်တမ်းတစ်လျှောက်လုံး type က int ပဲ ဖြစ်နေမှာပါ။
Python ဒါမှမဟုတ် JavaScript ကနေ လာသူတွေ အံ့ဩတတ်တာက type ပါ။ C++ မှာ variable တစ်ခုက အခု number ကိုင်ထားပြီး နောက်မှ text ကိုင်လို့ မရပါဘူး။ ဒီ စည်းမျဉ်းကြောင့် compiler က အမှားတွေကို စောစောဖမ်းနိုင်ပြီး value တစ်ခုစီအတွက် memory ပမာဏ အတိအကျနဲ့ machine instruction အမှန်တွေကို ရွေးပေးနိုင်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: တိတ်တဆိတ် ပြောင်းသွားတဲ့ value နဲ့ ဘယ်တုန်းကမှ မထည့်ခဲ့တဲ့ value
စလေ့လာသူ အများစုရဲ့ ပထမဆုံး C++ program တွေမှာ bug နှစ်မျိုး တွေ့ရပါတယ်။ ပထမတစ်မျိုးက value မပေးဘဲ ကြေညာထားတဲ့ variable ကို သုံးတာပါ — initializer မပါတဲ့ local int က အဲဒီ memory ထဲ ကျန်နေခဲ့တဲ့ bit တွေကို ကိုင်ထားတာမို့ running total တစ်ခုက ကျပန်း number တစ်ခုကနေ စသွားနိုင်ပါတယ်။ ဒုတိယတစ်မျိုးက သိမ်းလိုက်တဲ့အချိန်မှာ ပြောင်းသွားတဲ့ value ပါ — 19.99 ကို int ထဲ ထည့်ရင် 19 ရပြီး warning အပို မတောင်းရင် error message ဘာမှ မပေါ်ပါဘူး။
#include <iostream>
int main() {
int total; // no value yet: reading it is undefined behavior
total += 5;
int price = 19.99; // compiles: the .99 is silently dropped
std::cout << total << ' ' << price << '\n';
}C++ က ဒါတွေကို ဘာကြောင့် ခွင့်ပြုတာလဲ
Built-in type ရဲ့ local variable တွေကို အလိုအလျောက် သုည မထည့်ပေးတဲ့ C ရဲ့ စည်းမျဉ်းကို C++ က အမွေဆက်ခံထားပါတယ်၊ သုည ထည့်တာက အချိန်ကုန်ပြီး language က လျှို့ဝှက်အလုပ် မထည့်ချင်လို့ပါ။ Double ကို int ထဲ ညှပ်ထည့်ခွင့်ပေးတဲ့ C ရဲ့ implicit conversion တွေကိုလည်း ဆက်ခံထားပါတယ်။ -Wall ပါရင် GCC က total ကို initialize မလုပ်ဘဲ သုံးကြောင်း warning ပေးပေမယ့် price line ကတော့ -Wall -Wextra ကို တိတ်တဆိတ် ဖြတ်ကျော်သွားပြီး -Wconversion မှပဲ report လုပ်ပါတယ်။ Modern ဖြေရှင်းနည်းက အမြဲ initialize လုပ်ပြီး brace သုံးတာပါ — brace သုံးရင် price line က compile error ဖြစ်သွားပါတယ်။
Core concept: ကြေညာ၊ initialize လုပ်၊ ပြီးမှ သုံး
Declaration က type နဲ့ variable နာမည်ကို ပြောပါတယ်: int lessons; Initializer ကတော့ statement တစ်ခုတည်းမှာပဲ ပထမဆုံး value ကို ပေးပါတယ်။ C++ မှာ ရေးနည်း အမျိုးမျိုး ရှိပြီး code အစစ်တွေထဲမှာ အကုန် တွေ့ရပါမယ် — int a = 5; (copy initialization, C ကနေ)၊ int b(5); (direct initialization) နဲ့ int c{5}; (C++11 မှာ ထပ်ထည့်ခဲ့တဲ့ brace ဒါမှမဟုတ် list initialization)။ ရိုးရိုး type တွေအတွက် ရလဒ် value အတူတူပါပဲ။
Brace တွေကို modern default အဖြစ် ရွေးရတဲ့ အကြောင်းရင်း တစ်ခု ရှိပါတယ် — information ပျောက်စေတဲ့ narrowing conversion တွေကို ငြင်းလို့ပါ။ int price{19.99}; က compile မဖြစ်ပါဘူး၊ GCC က "narrowing conversion of '1.9989999999999998e+1' from 'double' to 'int'" လို့ report လုပ်ပါတယ်။ အဲဒီ ထူးဆန်းတဲ့ number က 19.99 နဲ့ အနီးဆုံး double ကို GCC print ထုတ်ပုံပါ — value က အစကတည်းက အတိအကျ မဟုတ်ခဲ့ဘူး ဆိုတဲ့ အရိပ်အမြွက်ပါ။ Brace အလွတ်က value-initialize လုပ်ပါတယ် — int count{}; က 0 ကနေ စမှာ သေချာပါတယ်။
auto က initializer ကနေ type ကို compiler ခန့်မှန်းခိုင်းတာပါ — auto lessons = 35; က int ဖြစ်ပြီး auto ratio = 0.5; က double ပါ။ auto မှာ initializer မဖြစ်မနေ လိုတာမို့ initialize မလုပ်ထားတဲ့ variable တွေကနေလည်း ကာကွယ်ပေးပါတယ်။ Text မှာတော့ သတိထားပါ — auto name = "Thuta"; က std::string မဟုတ်ဘဲ character တွေဆီ ညွှန်တဲ့ pointer ဖြစ်တဲ့ const char* ကို ခန့်မှန်းပါတယ်။
const က initialize လုပ်ပြီးနောက် variable ကို read-only ဖြစ်စေပါတယ်၊ နောက်မှ assign လုပ်ရင် compile error ပါ။ constexpr က တစ်ဆင့် ပိုသွားပါတယ် — value ကို compile time မှာ သိရမယ်၊ ဒါကြောင့် constexpr int maxSeats{40}; ကို array size လို compiler က constant လိုအပ်တဲ့ နေရာမှာ သုံးနိုင်ပါတယ်။ Program run နေစဉ် တွက်ထုတ်တဲ့ value အတွက် const၊ ပုံသေ constant တွေအတွက် constexpr ကို သုံးပါ။
Initialization နဲ့ assignment က အတူတူ မဟုတ်ပါဘူး၊ ပုံစံလည်း ဆင်တူပါတယ်။ int score{70}; က variable ကို ဖန်တီးပြီး ပထမဆုံး value ကို ပေးတာဖြစ်ပြီး score = 80; ကတော့ ရှိပြီးသား variable ရဲ့ value ကို အစားထိုးတာပါ။ const variable ကို initialize လုပ်လို့ရပေမယ့် assign လုပ်လို့ မရတာ ဒီကွာခြားချက်ကြောင့်ပါ။ std::string လို class type တွေမှာလည်း အလုပ်နှစ်ခု ကွဲပြားပါတယ်။ ဒါကြောင့် ကြေညာတဲ့နေရာမှာတင် တကယ့် value ကို ပေးထားတာက variable ကို အလွတ်အနေနဲ့ ဖန်တီးပြီး နောက်မှ ဖြည့်တာထက် ပိုရိုးရှင်းပြီး မကြာခဏ ပိုမြန်ပါတယ်။
- Type က ဘူးရဲ့ အရွယ်နဲ့ ပုံသဏ္ဌာန်ကို ရွေး
- နာမည်က ညွှန်းဖို့ label
- Initializer က ချက်ချင်း ဖြည့်
- const က ဖြည့်ပြီးတာနဲ့ အဖုံးပိတ်
Initializer မပါတဲ့ ဘူးက အလွတ် မဟုတ်ပါဘူး၊ အရင်ကကျန်ခဲ့တဲ့ bit တွေ ကိုင်ထားပါတယ်။
အထွေထွေ ပုံစံ: variable ကြေညာနည်းများ
Type name{value}; // brace initialization (preferred)
Type name = value; // copy initialization
Type name{}; // value-initialized: 0, 0.0, false, ""
auto name = value; // type deduced from the value
const Type name{value}; // read-only after initialization
constexpr Type name{value}; // must be known at compile time
name = newValue; // assignment: replace the value laterDeclaration တိုင်းမှာ type (ဒါမှမဟုတ် auto) နဲ့ နာမည် ပါပြီး initializer ပါသင့်ပါတယ်။ နောက်ဆုံး line က declaration မဟုတ်ပါဘူး — type မပါတဲ့ assignment က ရှိပြီးသား variable ရဲ့ value ကို အစားထိုးတာပါ။
Example: Thuta ရဲ့ profile
ဒီ program က type မတူတဲ့ variable ခုနစ်ခုကို brace နဲ့ initialize လုပ်ပြီး constexpr constant တစ်ခု၊ auto variable တစ်ခု ပါဝင်ပါတယ်။ bool တစ်ခုကို ပုံမှန်နည်းနဲ့ တစ်ခါ၊ std::boolalpha နဲ့ တစ်ခါ print ထုတ်ပြီး နောက်ဆုံးမှာ assignment နဲ့ value တစ်ခုကို ပြောင်းပါတယ်။ Output ခြောက်ကြောင်းကို ခန့်မှန်းကြည့်ပါ၊ အထူးသဖြင့် bool line နှစ်ကြောင်းကိုပါ။
#include <iostream>
#include <string>
int main() {
std::string name{"Thuta"};
int age{20};
double height{5.8};
char grade{'A'};
bool isLearning{true};
constexpr int passMark{50};
auto lessonsDone = 4; // deduced as int
std::cout << name << " is " << age << " years old.\n";
std::cout << "Height: " << height << '\n';
std::cout << "Grade: " << grade << '\n';
std::cout << "Learning C++: " << isLearning << '\n';
std::cout << std::boolalpha << "Learning C++: " << isLearning << '\n';
lessonsDone = lessonsDone + 1; // assignment replaces the value
std::cout << "Lessons done: " << lessonsDone << ", pass mark: " << passMark << '\n';
return 0;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 5
std::string name{"Thuta"};Brace တွေက std::string လို class type တွေမှာလည်း အလုပ်လုပ်ပါတယ်။ <string> ကို include လုပ်ထားရပါမယ်။
- Line 8
char grade{'A'};Single quote က character တစ်လုံးပါ။ char grade{"A"}; လို့ double quote နဲ့ ရေးရင် "A" က string literal ဖြစ်လို့ compile မဖြစ်ပါဘူး။
- Line 10
constexpr int passMark{50};Compile time ကတည်းက သိတဲ့ ပုံသေ value ပါ။ နောက်မှ passMark = 60; လို့ ရေးရင် compile error ပါ။
- Line 11
auto lessonsDone = 4;4 က int literal ဖြစ်လို့ lessonsDone ရဲ့ type က int ပါ။ auto က dynamic type မဟုတ်ပါဘူး — type ကို ဒီ line မှာ တစ်ခါတည်း ဆုံးဖြတ်ပြီး ပုံသေ ဖြစ်သွားပါတယ်။
- Lines 16-17
std::cout << std::boolalpha << "Learning C++: " << isLearning << '\n';Default အနေနဲ့ std::cout က bool ကို 1 ဒါမှမဟုတ် 0 အဖြစ် print ထုတ်ပါတယ်။ std::boolalpha ကို တစ်ခါ ပို့လိုက်ရင် နောက်ပိုင်း bool တွေကို true နဲ့ false အဖြစ် ထုတ်ပါတယ်၊ ဒီ setting က std::cout ပေါ်မှာ ဆက်ရှိနေပါတယ်။
- Line 19
lessonsDone = lessonsDone + 1;Type မပါတဲ့ assignment ပါ။ ညာဘက်ကို အရင် တွက်ပြီး (4 + 1) ရလဒ် 5 ကို ဘူးထဲ ပြန်ထည့်ပါတယ်။
Thuta is 20 years old.
Height: 5.8
Grade: A
Learning C++: 1
Learning C++: true
Lessons done: 5, pass mark: 50Initialization ပုံစံတွေကို နှိုင်းယှဉ်ကြည့်မယ်
| ပုံစံ | ဥပမာ | 2.5 ကို int ထဲ ထည့်ရင် |
|---|---|---|
| Copy | int a = 2.5; | Compile ဖြစ်ပြီး a က 2 ဖြစ်တယ် |
| Direct | int b(2.5); | Compile ဖြစ်ပြီး b က 2 ဖြစ်တယ် |
| Brace | int c{2.5}; | Narrowing compile error |
| Brace အလွတ် | int d{}; | Value မလို — d က 0 |
GCC က variable ကနေ narrowing ကို warning ပဲ ပေးတယ်
Standard အရ brace ထဲက narrowing မှန်သမျှ ill-formed ပါ။ ဒါပေမယ့် GCC ကတော့ source က 2.5 လို constant ဖြစ်ရင် error ပေးပေမယ့် double price{1.5}; int whole{price}; လို variable ဖြစ်ရင် warning ပဲ ပေးပါတယ်။ -Werror နဲ့ compile လုပ်ရင် အဲဒီ warning ကလည်း error ဖြစ်သွားလို့ ဒီ course ရဲ့ -Wall -Wextra -Werror command နဲ့ဆိုရင် narrowing တိုင်းကို ရပ်တန့်စေပါတယ်။
နာမည်ပေး စည်းမျဉ်းနဲ့ scope
- နာမည်တွေမှာ စာလုံး၊ ဂဏန်းနဲ့ underscore ပါနိုင်ပေမယ့် ဂဏန်းနဲ့ မစရပါဘူး — 2ndPlace က မရပေမယ့် secondPlace က ရပါတယ်။
- C++ က case-sensitive ပါ — score နဲ့ Score က variable နှစ်ခု ကွဲပါတယ်။
- int, class, new, return လို keyword တွေကို နာမည်အဖြစ် မသုံးရပါဘူး။
- Underscore နှစ်ခုပါတဲ့ နာမည်တွေနဲ့ underscore ပြီးရင် စာလုံးအကြီးနဲ့ စတဲ့ နာမည်တွေကို compiler နဲ့ library အတွက် reserve လုပ်ထားလို့ ရှောင်ပါ။
- camelCase ဒါမှမဟုတ် snake_case ကို တစ်မျိုးရွေးပြီး project တစ်ခုလုံးမှာ ညီညီညာညာ သုံးပါ။
Variable တစ်ခုက ကြေညာတဲ့ line ကနေ သူ့ block ကို ပိတ်တဲ့ brace အထိ အသက်ရှင်ပါတယ်၊ အဲဒါကို scope လို့ ခေါ်ပါတယ်။ အောက်က bonus ကို inner block ထဲမှာ ကြေညာထားလို့ block ပြင်ပမှာ သုံးရင် GCC က "'bonus' was not declared in this scope" လို့ ပြောပါတယ်။ Variable တွေကို ပထမဆုံး သုံးမယ့်နေရာနဲ့ အနီးဆုံးမှာ ကြေညာပါ။
#include <iostream>
int main() {
int score{70};
{
int bonus{5}; // bonus exists only inside these braces
score = score + bonus;
}
// bonus is gone here: using it would be a compile error
std::cout << "Score: " << score << '\n';
return 0;
}လက်တွေ့ project တွေမှာ
Project အစစ်တွေမှာ variable တွေ
Professional code တွေမှာ const ကို default အဖြစ် သုံးကြပါတယ် — နောက်မှ ပြောင်းဖို့ မလိုတဲ့ value မှန်သမျှကို const လို့ မှတ်ထားလို့ code ဖတ်သူက value ပြောင်းနိုင်တဲ့ variable အနည်းငယ်ကိုပဲ စောင့်ကြည့်ရပါတယ်။ Buffer size, retry limit, timeout လို configuration constant တွေကို constexpr နဲ့ ရေးကြပြီး code ဟောင်းတွေက #define macro တွေကို အစားထိုးပါတယ်၊ constexpr variable မှာ type နဲ့ scope ရှိလို့ပါ။
auto ကိုတော့ type နာမည် ရှည်လွန်းတဲ့နေရာ (iterator, std::chrono duration) တွေမှာ သုံးပြီး int ဒါမှမဟုတ် double လို type က အဓိပ္ပာယ် ဖော်ပြတဲ့နေရာတွေမှာ type ကို အတိအကျ ရေးကြပါတယ်။ C++ Core Guidelines ရဲ့ ES.20 "Always initialize an object" စည်းမျဉ်းကို clang-tidy ရဲ့ cppcoreguidelines-init-variables check လို tool တွေက အလိုအလျောက် စစ်ပေးပြီး team အများစုက ဒီ check တွေကို continuous integration ထဲမှာ run ကြပါတယ်။
အမှားများ
Variable တွေနဲ့ အဖြစ်များတဲ့ အမှားတွေ
1. Value မထည့်ခင် variable ကို ဖတ်တာ
int total; total += 5; ကို -Wall နဲ့ compile လုပ်ရင် GCC က "'total' is used uninitialized" warning ပေးပါတယ်။ Warning ပိတ်ထားရင် program က ကျပန်း number တစ်ခုနဲ့ ဆက် run ပါတယ်။ int total{}; လို့ ရေးလိုက်ရုံနဲ့ 0 ကနေ စပါတယ်။
2. Quote အမျိုးအစား မှားသုံးတာ
char grade = "A"; က "invalid conversion from 'const char*' to 'char'" error ပေးပါတယ်၊ double quote က string literal ဖြစ်လို့ပါ။ ပြောင်းပြန် std::string name = 'Thuta'; ကလည်း multi-character literal warning နဲ့ conversion error ပေးပါတယ်။ Character တစ်လုံးအတွက် 'A'၊ text အတွက် "Thuta" ကို သုံးပါ။
3. auto က std::string ပေးမယ်လို့ ထင်တာ
auto name = "Thuta"; ပြီးရင် name + " Aung" ကို ရေးကြည့်ပါ — GCC က "invalid operands of types 'const char*' and 'const char [6]' to binary 'operator+'" လို့ ပြောပါတယ်၊ pointer နှစ်ခုကို + နဲ့ ဆက်လို့ မရလို့ပါ။ Text ကို ပြောင်းလဲ ချိတ်ဆက်ချင်ရင် std::string name{"Thuta"}; လို့ type ကို အတိအကျ ရေးပါ။
4. <string> ကို include မလုပ်ဘဲ အလုပ်လုပ်တယ်လို့ ယုံတာ
GCC မှာ <iostream> က <string> ကို သွယ်ဝိုက်ပြီး ဆွဲထည့်တာမို့ include မပါဘဲ std::string သုံးတဲ့ program က compile ဖြစ်သွားနိုင်ပါတယ်။ Standard က ဒါကို အာမမခံလို့ compiler ဒါမှမဟုတ် library version တခြားမှာ ကျိုးနိုင်ပါတယ်။ ကိုယ်သုံးတဲ့ အရာတိုင်းအတွက် header ကို ကိုယ်တိုင် include လုပ်ပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Variable တိုင်းကို ကြေညာတဲ့နေရာမှာပဲ initialize လုပ်ပါ၊ နောက်မှ ဖြည့်မှာဆိုရင် {} ကို သုံးပါ။
- Brace initialization ကို default အဖြစ် သုံးပြီး narrowing ကို compiler ဖမ်းခိုင်းပါ။
- ပြောင်းစရာ မလိုတဲ့ value တွေကို const၊ compile time မှာ သိတဲ့ constant တွေကို constexpr လို့ မှတ်ပါ။
- score, totalPrice လို ဘာကို ကိုင်ထားလဲ ပြောတဲ့ နာမည်တွေ ပေးပါ၊ x, temp, data လို နာမည်တွေကို scope သေးသေးလေးတွေမှာပဲ သုံးပါ။
Mini challenge
Mini challenge: လက်ဖက်ရည်ဆိုင် order
လက်ဖက်ရည်ဆိုင် order တစ်ခုအတွက် variable တွေ ကြေညာပြီး tax ပါတဲ့ စုစုပေါင်းကို print ထုတ်ပါ။ ပြီးရင် quantity ကို ပြောင်းပြီး ထပ် print ထုတ်ပါ။
- constexpr tax rate (double 0.05) နဲ့ const std::string item နာမည် ပါရမယ်
- int quantity နဲ့ double unitPrice ကို brace နဲ့ initialize လုပ်ရမယ်
- auto subtotal ကို quantity * unitPrice ကနေ တွက်ပြီး tax ပါတဲ့ စုစုပေါင်းကို print ထုတ်ရမယ်
- quantity ကို assignment နဲ့ ပြောင်းပြီး subtotal ကို ပြန်တွက်ကာ ထပ် print ထုတ်ရမယ်
#include <iostream>
#include <string>
int main() {
// TODO: constexpr tax rate 0.05 and a const std::string item name
// TODO: quantity (int) and unitPrice (double), both with braces
// TODO: auto subtotal = ...; print "<item> x<quantity> = <total with tax>"
// TODO: assign a new quantity, recompute subtotal, print again
return 0;
}ရလဒ်: Milk tea, unit price 1.50 နဲ့ ဆိုရင်: Milk tea x2 = 3.15 Milk tea x4 = 6.3
အရိပ်အမြွက် ကြည့်ရန်
int * double က double ဖြစ်လို့ auto subtotal က double ရပါတယ်။ Tax ပါတဲ့ စုစုပေါင်းက subtotal * (1 + taxRate) ပါ။ const item ကို နောက်မှ ပြန် assign လုပ်ကြည့်ရင် compiler က ဘာပြောလဲ စမ်းကြည့်ပါ။
အဖြေ ကြည့်ရန်
#include <iostream>
#include <string>
int main() {
constexpr double taxRate{0.05};
const std::string item{"Milk tea"};
int quantity{2};
double unitPrice{1.50};
auto subtotal = quantity * unitPrice; // int * double gives a double
std::cout << item << " x" << quantity << " = " << subtotal * (1 + taxRate) << '\n';
quantity = 4; // assignment: new value, same type
subtotal = quantity * unitPrice;
std::cout << item << " x" << quantity << " = " << subtotal * (1 + taxRate) << '\n';
return 0;
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Variable တစ်ခုမှာ ပုံသေ type တစ်ခု၊ နာမည်တစ်ခုနဲ့ value တစ်ခု ရှိပြီး type က ဘယ်တော့မှ မပြောင်းပါဘူး။
- Brace initialization က narrowing ကို ငြင်းပြီး {} က 0 ကို ပေးပါတယ်၊ initialize မလုပ်ထားတဲ့ local variable ကို ဖတ်တာက undefined behavior ပါ။
- auto က initializer ကနေ type ကို ခန့်မှန်းပါတယ် — "text" က std::string မဟုတ်ဘဲ const char* ရပါတယ်။
- const က run time မှာ read-only ဖြစ်စေပြီး constexpr က compile time မှာ သိရတဲ့ value ကို လိုအပ်ပါတယ်။
The Current ISO C++ Standard (C++23) — Standard C++ Foundation