ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
C++ code ဆိုတာ ပုံစံတင်းကျပ်တဲ့ ညွှန်ကြားချက် စာရင်းတစ်ခုပါ — စာလုံးတိုင်းကို အတိအကျ လိုက်လုပ်တဲ့ စားဖိုမှူးတစ်ယောက်အတွက် ချက်နည်းစာအုပ်လိုပါပဲ။ ညွှန်ကြားချက်တစ်ခုစီက statement တစ်ခုဖြစ်ပြီး စာကြောင်းတစ်ကြောင်း ပုဒ်မနဲ့ ဆုံးသလို semicolon နဲ့ ဆုံးပါတယ်။ ဆက်စပ်တဲ့ statement တွေကို curly brace { } ထဲမှာ စုထားပြီး main လို့ နာမည်ပေးထားတဲ့ အုပ်စုက စားဖိုမှူး စဖတ်မယ့် နေရာပါ။
main ရဲ့ အပေါ်မှာ #include နဲ့ လိုအပ်တဲ့ library တွေကို စာရင်းပြုစုပါတယ် — ချက်နည်း မစခင် မီးဖိုချောင်သုံး ပစ္စည်းတွေ စာရင်းပြုစုသလိုပါ။ ပစ္စည်းတစ်ခု ကျန်ခဲ့ရင် ဒါမှမဟုတ် ပုဒ်မတစ်ခု မေ့သွားရင် ဘာမှ မ run ခင်မှာပဲ compiler က ရပ်ပြီး ဘယ် line မှာလဲ ပြောပြပါတယ်။
Problem → Why → Solution
ပြဿနာ: character တစ်လုံး ကျန်ခဲ့တာနဲ့ error တွေ
စလေ့လာသူတစ်ယောက်က line နှစ်ကြောင်းပါတဲ့ program တစ်ခု ရေးပြီး semicolon တစ်ခု မေ့သွားပါတယ်။ Compiler က "line 4 မှာ semicolon ထည့်ပါ" လို့ မပြောပါဘူး။ Line 4 ရဲ့ အဆုံးကို ညွှန်ပြပြီး နောက် line က 'return' မတိုင်ခင် ';' ကို မျှော်လင့်ခဲ့တယ်လို့ report လုပ်ပါတယ်။ Quote တစ်ခု ကျန်ခဲ့တာ၊ brace တစ်ခု ပိုနေတာလို အမှားသေးသေးလေးတွေက တခြားနေရာ လုံးလုံးကို ညွှန်ပြတဲ့ message တွေ ထုတ်ပေးတတ်ပါတယ်။
#include <iostream>
int main() {
std::cout << "Hello World!"
return 0;
}
// g++ -std=c++20 hello.cpp -o hello
// hello.cpp:4:32: error: expected ';' before 'return'Error က နောက် line ကို ဘာကြောင့် ညွှန်တာလဲ
Compiler က code ကို token (စကားလုံး၊ သင်္ကေတ) အစဉ်တန်းအဖြစ် ဖတ်ပြီး line break တွေက သူ့အတွက် အဓိပ္ပာယ် မရှိပါဘူး။ Statement တစ်ခုက semicolon ရောက်မှပဲ ဆုံးတာမို့ std::cout << "Hello World!" return 0 ကို ကျိုးနေတဲ့ statement ရှည်ကြီး တစ်ခုအဖြစ် ဖတ်ပြီး return ဆိုတဲ့ token ရောက်လာမှ ပြဿနာကို သိပါတယ်။ ဒါကြောင့် report လုပ်တဲ့ နေရာက အမှားအစစ်ရဲ့ နောက်မှာ ဖြစ်နေတတ်ပါတယ် — ကောင်းနေသလို ထင်ရတဲ့ line ကို error က ညွှန်ရင် အပေါ်က line ရဲ့ အဆုံးကို အရင် ကြည့်ပါ။
Core concept: program တစ်ခုရဲ့ အစိတ်အပိုင်းတွေ
1. #include directive
#include <iostream> က preprocessor directive ပါ၊ statement မဟုတ်လို့ semicolon မလိုပါဘူး။ std::cout ရဲ့ declaration တွေကို file ထဲ ကူးထည့်ပေးပါတယ်။ Angle bracket < > က standard library header တွေအတွက်ပါ။
2. main function
Program တိုင်းမှာ main တစ်ခုတည်း ရှိပြီး operating system က အဲဒါကို ခေါ်ပါတယ်။ int က return type (exit status) ပါ၊ void main() က C++ မှာ တရားဝင် မဟုတ်ပါဘူး။ main အတွက်ပဲ ထူးခြားတဲ့ စည်းမျဉ်းတစ်ခု ရှိပါတယ် — return statement မရောက်ဘဲ အဆုံးထိ ရောက်သွားရင် return 0 လုပ်သလို ဆက်ဆံပါတယ်။
3. Statement နဲ့ block
Statement တစ်ခုစီ ; နဲ့ ဆုံးပြီး brace တွေက statement တွေကို block တစ်ခုအဖြစ် စုပါတယ်။ Compiler က space နဲ့ indentation ကို လျစ်လျူရှုပေမယ့် လူတွေအတွက်တော့ မရှိမဖြစ်ပါ — block အတွင်းက code ကို space လေးခုနဲ့ indent လုပ်ပါ။
4. std:: နဲ့ namespace
Standard library က သူ့နာမည်တွေကို std ဆိုတဲ့ namespace ထဲမှာ ထားတာမို့ cout ရဲ့ နာမည်အပြည့်က std::cout ပါ။ :: ကို scope resolution operator လို့ ခေါ်ပါတယ်။ using namespace std; က std ထဲက နာမည်အားလုံးကို ဆွဲထုတ်လို့ program သေးသေးလေးတွေမှာ အဆင်ပြေပေမယ့် ကိုယ့်နာမည်တွေနဲ့ တိုက်မိနိုင်ပြီး header file ထဲမှာ ဘယ်တော့မှ မထည့်သင့်ပါဘူး။ ဒီ course က std:: ကို အပြည့် ရေးပါတယ်။
5. Comment
// က line ရဲ့ အဆုံးထိ comment ဖြစ်ပြီး /* ... */ က line အများကြီး ဖုံးနိုင်ပါတယ်။ Compiler က comment တွေကို ဖယ်ပစ်ပါတယ်၊ ဒါကြောင့် code က ဘာလုပ်လဲ ဆိုတာထက် ဘာကြောင့် လုပ်လဲ ဆိုတာကို ရှင်းပြဖို့ သုံးပါ။
Output ထုတ်တာက << operator နဲ့ပါ — std::cout << value က value တစ်ခုကို standard output ဆီ ပို့ပြီး statement တစ်ခုထဲမှာ << အများကြီး ဆက်ရေးလို့ ရပါတယ်။ '\n' ဒါမှမဟုတ် std::endl ကို print ထုတ်မှပဲ line ဆုံးပါတယ်။ စကားလုံးနဲ့ number ရောနေတဲ့ text အတွက် <format> (C++20) ထဲက std::format က {} placeholder ပါတဲ့ ပုံစံတစ်ခုကနေ string အပြည့်ကို ဆောက်ပေးပြီး << ရှည်ရှည်ကြီး ဆက်ထားတာထက် ဖတ်ရ ပိုလွယ်တတ်ပါတယ်။
- #include က library နာမည်တွေ ကူးထည့်
- Token တွေ ဖတ်၊ line break ကို လျစ်လျူရှု
- ; တစ်ခုစီက statement တစ်ခု ပိတ်
- main ရဲ့ block ကို အပေါ်က အောက် run
Error က ကောင်းနေတဲ့ line ကို ညွှန်ရင် အပေါ်က line ရဲ့ အဆုံးကို စစ်ပါ။
အထွေထွေ ပုံစံ: program တစ်ခုရဲ့ ဖွဲ့စည်းပုံ
#include <header> // directives: no semicolon
// single-line comment
/* multi-line
comment */
int main() {
statement; // every statement ends with ;
{
statement; // braces group statements into a block
}
return 0;
}Directive တွေက file ရဲ့ ထိပ်မှာ ရှိပြီး semicolon မလိုပါဘူး။ main ရဲ့ brace ထဲမှာ statement တွေကို အစဉ်လိုက် ရေးပြီး block တစ်ခုထဲမှာ နောက်ထပ် block တွေ ထည့်လို့ ရပါတယ်။
Example: line သုံးကြောင်း၊ ထုတ်နည်း သုံးမျိုး
ဒီ program က std::cout နဲ့ '\n'၊ std::cout နဲ့ std::endl၊ ပြီးတော့ std::format ဆိုပြီး output ထုတ်နည်း သုံးမျိုးကို ပြပါတယ်။ Comment တွေကလည်း program ရဲ့ အစိတ်အပိုင်းတစ်ခုစီကို နာမည်တပ်ပေးထားပါတယ်။ Output သုံးကြောင်းကို ခန့်မှန်းပြီးမှ run ကြည့်ပါ။
#include <format> // std::format (C++20)
#include <iostream> // std::cout, std::endl
/* A first program: two plain lines of output,
then one line built with std::format. */
int main() {
std::cout << "Hello World!\n"; // '\n' ends the line
std::cout << "C++ is ready." << std::endl; // endl ends it and flushes
std::cout << std::format("{} + {} = {}\n", 2, 3, 2 + 3);
return 0; // 0 tells the system: success
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Lines 1-2
#include <format> // std::format (C++20)Header တစ်ခုစီက library ရဲ့ အပိုင်းတစ်ခုကို ယူလာပါတယ် — <format> က std::format၊ <iostream> က std::cout ပါ။ Header အစဉ်က အရေးမကြီးပါဘူး၊ ဒါပေမယ့် အက္ခရာစဉ်အတိုင်း ထားရင် ရှာရလွယ်ပါတယ်။
- Line 6
int main() {Program ဒီနေရာကနေ စ run ပါတယ်။ { က main ရဲ့ block ကို ဖွင့်ပြီး line 12 က } နဲ့ ပိတ်ပါတယ်။
- Line 7
std::cout << "Hello World!\n";Quote ထဲက \n က newline character တစ်လုံးပါ၊ ဒါကြောင့် နောက် output က line အသစ်ကနေ စပါတယ်။ Semicolon က statement ကို ပိတ်ပါတယ်။
- Line 8
std::cout << "C++ is ready." << std::endl;std::endl ကလည်း line ကို ဆုံးစေပေမယ့် output buffer ကို flush လုပ်ပါသေးတယ် — စောင့်နေတဲ့ text ကို ချက်ချင်း ထုတ်ခိုင်းတာပါ။ Line တစ်ကြောင်းအတွက် ကွာခြားမှု မမြင်ရပေမယ့် line ထောင်ချီ print ထုတ်တဲ့ loop မှာ နှေးစေပါတယ်။
- Line 10
std::format("{} + {} = {}\n", 2, 3, 2 + 3){} တစ်ခုစီကို နောက်က argument တွေနဲ့ အစဉ်လိုက် အစားထိုးပြီး string တစ်ခု ဆောက်ပါတယ် — 2, 3 နဲ့ 2 + 3 ရဲ့ ရလဒ် 5။ ဆောက်ပြီးသား string ကို std::cout က print ထုတ်ပါတယ်။
- Line 11
return 0; // 0 tells the system: successmain ကနေ 0 ပြန်ပေးတာက program အောင်မြင်စွာ ပြီးကြောင်း shell ကို ပြောတာပါ။ main အတွက်ဆိုရင် ဒီ line ကို ချန်ခဲ့လည်း ရလဒ် တူတူပါပဲ။
Hello World!
C++ is ready.
2 + 3 = 5std:: ကို အပြည့်ရေးမလား၊ using သုံးမလား
std ထဲမှာ နာမည် ထောင်ချီ ရှိပါတယ် — count, size, min, max, distance... using namespace std; ရေးလိုက်ရင် အဲဒါတွေ အကုန် ကိုယ့် code ထဲ ရောက်လာပါတယ်။ ကျွန်တော်တို့ GCC 16 နဲ့ စမ်းကြည့်တော့ #include <algorithm> နဲ့ using namespace std; ပါတဲ့ program မှာ count ဆိုတဲ့ global variable တစ်ခု ကြေညာလိုက်တာနဲ့ "reference to 'count' is ambiguous" error တက်ပါတယ်၊ ကိုယ့် count နဲ့ std::count ကို compiler က ခွဲမရလို့ပါ။ ဒီ ပြဿနာက include တစ်ခု ထပ်ထည့်လိုက်ရုံနဲ့ ရုတ်တရက် ပေါ်လာနိုင်တာ ပိုဆိုးပါတယ်။
အလယ်အလတ်လမ်းက using-declaration ပါ — using std::cout; လို့ ရေးရင် အဲဒီ နာမည်တစ်ခုတည်းကိုပဲ ယူလာပြီး ကျန်တာတွေကို std ထဲမှာ ချန်ထားပါတယ်။ ဒါကို .cpp file ထဲမှာ အသုံးများပြီး header file တွေမှာတော့ std:: ကို အပြည့်ရေးတာ ပိုလုံခြုံပါတယ်။
#include <iostream>
#include <string>
using std::cout; // bring in just this one name
using std::string;
int main() {
string city = "Yangon";
cout << "Hello from " << city << '\n';
std::cout << "The full name std::cout still works.\n";
return 0;
}std::format နဲ့ output ကို ပုံဖော်မယ်
std::format ရဲ့ {} ထဲမှာ format spec ထည့်နိုင်ပါတယ်။ {:.2f} က ဒသမ နှစ်နေရာ၊ {:>6} က ညာဘက်ကပ်ပြီး character ခြောက်လုံး၊ {:<6} က ဘယ်ဘက်ကပ်၊ {:^6} က အလယ်ပါ။ {1} လို နံပါတ်တွေက argument ကို အစဉ်မလိုက်ဘဲ ရွေးပြီး {{ နဲ့ }} က brace အစစ်ကို ထုတ်ပါတယ်။ Format string ကို compile time မှာ စစ်တာမို့ argument လိုနေရင် ရှည်လျားတဲ့ compile error တက်ပါတယ်၊ runtime မှာ မှားသွားတာ မဟုတ်ပါဘူး။
#include <format>
#include <iostream>
int main() {
std::cout << std::format("{} scored {} points\n", "Aye", 87);
std::cout << std::format("Average: {:.2f}\n", 82.756);
std::cout << std::format("[{:>6}] [{:<6}] [{:^6}]\n", 42, 42, 42);
std::cout << std::format("{1} before {0}\n", "second", "first");
std::cout << std::format("Literal braces: {{ and }}\n");
return 0;
}Aye scored 87 points
Average: 82.76
[ 42] [42 ] [ 42 ]
first before second
Literal braces: { and }C++23 ရဲ့ std::println
C++23 က <print> header ထဲမှာ std::print နဲ့ std::println ကို ထည့်ထားပြီး format လုပ်ထားတဲ့ text ကို တိုက်ရိုက် print ထုတ်ပါတယ် — std::println("{} + {} = {}", 2, 3, 5); လိုပါ။ Toolchain တိုင်း မ support သေးတာမို့ ဒီ course က std::cout နဲ့ std::format ကို သုံးပါတယ်၊ ပုံစံ {} တွေကတော့ အတူတူပါပဲ။
လက်တွေ့ project တွေမှာ
Project အစစ်တွေမှာ syntax စည်းကမ်း
Compiler က space တွေကို ဂရုမစိုက်ပေမယ့် team တွေကတော့ ဂရုစိုက်ကြပါတယ်။ Project အများစုက brace ကို ဘယ်မှာ ထားမလဲ၊ indent ဘယ်လောက်လဲ၊ line အရှည် ဘယ်လောက်ထိလဲ ဆိုတာကို style guide တစ်ခုမှာ သတ်မှတ်ပြီး clang-format လို tool နဲ့ save လုပ်တိုင်း အလိုအလျောက် format လုပ်ကြပါတယ်။ ဒါကြောင့် code review တွေမှာ space အကြောင်း ငြင်းခုံစရာ မလိုဘဲ logic ကိုပဲ အာရုံစိုက်နိုင်ပါတယ်။ Project root မှာ .clang-format file တစ်ခု ထားလိုက်ရင် team အဖွဲ့ဝင်တိုင်းရဲ့ editor က စည်းမျဉ်း တစ်ခုတည်းကို လိုက်နာပါတယ်။
Style guide ကြီးတွေက ဒီ lesson ရဲ့ အကြံပေးချက်တွေကို စည်းမျဉ်းအဖြစ် ပြောင်းထားပါတယ် — Google C++ Style Guide က using namespace လို using-directive တွေကို တားမြစ်ထားပြီး log အများကြီး ရေးတဲ့ code မှာ std::endl အစား '\n' ကို ရွေးကြပါတယ်၊ flush တိုင်းက system call တစ်ခု ကုန်ကျနိုင်လို့ပါ။ Comment တွေကိုလည်း Doxygen လို tool တွေက /// နဲ့ စတဲ့ comment တွေကနေ documentation website အဖြစ် ထုတ်ပေးနိုင်ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ syntax အမှားတွေ
1. Document ကနေ ကူးလာတဲ့ "smart quote" တွေ
Word processor ဒါမှမဟုတ် chat app တွေက " ကို “ ” အဖြစ် ပြောင်းပစ်တတ်ပါတယ်။ အဲဒီ code ကို ကူးထည့်ရင် GCC 16 က "extended character “ is not valid in an identifier" လို့ ပြောပါတယ်။ Quote တွေကို editor ထဲမှာ ကိုယ်တိုင် ပြန်ရိုက်ပါ။
2. Semicolon ကို မလိုတဲ့နေရာ ထည့်တာ
#include <iostream>; က warning "extra tokens at end of '#include' directive" ပေးပြီး int main(); { ကတော့ error "expected unqualified-id before '{' token" ပေးပါတယ် — ; က main ကို body မပါတဲ့ declaration အဖြစ် ပိတ်လိုက်လို့ နောက်က block က ဘယ် function နဲ့မှ မဆိုင်တော့ပါဘူး။ Directive တွေနဲ့ function header တွေမှာ semicolon မထည့်ပါနဲ့။
3. Brace ပိတ်ဖို့ မေ့တာ
{ တစ်ခု မပိတ်ရင် GCC က file ရဲ့ အဆုံးကို ရောက်မှ "expected '}' at end of input" လို့ ပြောပြီး ဘယ် { နဲ့ တွဲချင်လဲ ဆိုတဲ့ note ကိုလည်း ပြပါတယ်။ Block တစ်ခုကို ဖွင့်တိုင်း ပိတ်တဲ့ brace ကို ချက်ချင်း ရေးပြီးမှ အထဲကို ဖြည့်ပါ၊ indentation ကိုလည်း ညီညီညာညာ ထားပါ။
4. using namespace std; ကို header ထဲ ထည့်တာ
Header file ထဲက using namespace std; က အဲဒီ header ကို include လုပ်တဲ့ file တိုင်းထဲ ပြန့်သွားပြီး အပေါ်က count ambiguity လို ပြဿနာတွေကို ကိုယ်မမြင်ရတဲ့ file တွေထဲ ဖြစ်စေပါတယ်။ Header တွေမှာ std:: ကို အပြည့်ရေးပါ။
Mini challenge
Mini challenge: လေ့လာမယ့် အစီအစဉ် ဇယား
std::format သုံးပြီး lesson သုံးခုနဲ့ စုစုပေါင်း မိနစ်ကို ကော်လံ ညီညီ ပြတဲ့ ဇယားလေး print ထုတ်ပါ။
- std:: နာမည်တွေကိုပဲ သုံးရမယ် (using namespace std; မပါရ)
- ခေါင်းစဉ် line တစ်ကြောင်းပြီးရင် lesson နာမည်ကို character ၁၂ လုံးမှာ ဘယ်ကပ်၊ မိနစ်ကို character ၄ လုံးမှာ ညာကပ်တဲ့ line သုံးကြောင်း
- Total line ရဲ့ number ကို program က တွက်ရမယ်
- // comment တစ်ခုနဲ့ /* */ comment တစ်ခု ပါရမယ်
#include <format>
#include <iostream>
// TODO: a /* */ comment that says what the program prints
int main() {
// TODO: print the title "Study plan"
// TODO: three rows: name left-aligned in 12, minutes right-aligned in 4
// hint: std::format("{:<12}{:>4}\n", name, minutes)
// TODO: a Total row whose number the program computes
return 0;
}ရလဒ်: Output က ဒီလို ဖြစ်ရပါမယ်: Study plan Intro 15 Setup 20 Syntax 25 Total 60
အရိပ်အမြွက် ကြည့်ရန်
{:<12} က value ကို ဘယ်ကပ်ပြီး character ၁၂ လုံးအထိ space ဖြည့်ပါတယ်၊ {:>4} က ညာကပ်ပြီး ၄ လုံးအထိ ဖြည့်ပါတယ်။ Row တိုင်းမှာ format string တူတူ သုံးရင် ကော်လံတွေ ညီသွားပါတယ်။
အဖြေ ကြည့်ရန်
#include <format>
#include <iostream>
/* Prints a small study plan.
Every row uses the same format string, so the columns line up. */
int main() {
std::cout << "Study plan\n";
std::cout << std::format("{:<12}{:>4}\n", "Intro", 15);
std::cout << std::format("{:<12}{:>4}\n", "Setup", 20);
std::cout << std::format("{:<12}{:>4}\n", "Syntax", 25);
// the program does the adding, not us
std::cout << std::format("{:<12}{:>4}\n", "Total", 15 + 20 + 25);
return 0;
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- #include directive တွေက ထိပ်မှာ ရှိပြီး semicolon မလို၊ program က int main() ကနေ စ run ပါတယ်။
- Statement တိုင်း ; နဲ့ ဆုံးပြီး brace တွေက block ကို သတ်မှတ်ပါတယ်၊ compiler က line break တွေကို ဂရုမစိုက်ပါဘူး။
- '\n' က line ကို ဆုံးစေပြီး std::endl က flush ပါ လုပ်ပါတယ်၊ std::format က {} placeholder နဲ့ text ကို ဆောက်ပါတယ်။
- Standard library နာမည်တွေက std ထဲမှာ ရှိပါတယ် — std:: ကို အပြည့်ရေး ဒါမှမဟုတ် using std::cout; လို နာမည်တစ်ခုချင်း ယူပါ။
The Current ISO C++ Standard (C++23) — Standard C++ Foundation