ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
C++ ကို tool box ပိုကြီးတဲ့ C လို့ မြင်ကြည့်ပါ။ C က number, array, function တွေ ပေးပြီး memory အပါအဝင် ကျန်တာအားလုံးကို လက်နဲ့ စီမံခိုင်းပါတယ်။ C++ က အဲဒါတွေ အကုန် ထိန်းထားပြီး စာရင်းကိုင်တဲ့ အလုပ်ကို ကိုယ်စားလုပ်ပေးတဲ့ tool တွေ ထပ်ထည့်ထားပါတယ် — ကိုယ့်အရွယ်အစားကို ကိုယ်ပြောင်းတဲ့ string၊ std::vector လို container၊ data နဲ့ အဲဒီ data ကို သုံးတဲ့ function တွေကို ချည်ထားတဲ့ class၊ type တစ်မျိုးစီအတွက် code ကို ကိုယ်စားရေးပေးတဲ့ template တွေပါ။
အရေးကြီးတဲ့ အချက်ကတော့ အဲဒီ tool တွေက run time မှာ ကုန်ကျစရိတ် မရှိသလောက်ပဲ ဖြစ်တာပါ။ C++ program ကို machine code အဖြစ် တိုက်ရိုက် compile လုပ်ဆဲမို့ C လောက် မြန်နိုင်ပြီး ပြဿနာအကြောင်း ကိုယ်တွေးတဲ့ပုံနဲ့ ပိုနီးတဲ့ code ကို ရေးလို့ရပါတယ်။
Problem → Why → Solution
ပြဿနာ: C မှာ text ဆိုတာ ကိုယ်တိုင် စီမံရတဲ့ အရွယ်အစား ပုံသေ array
နာမည်တစ်ခုကနေ နှုတ်ဆက်စာ တည်ဆောက်ချင်တယ် ဆိုပါစို့။ C မှာ text က char array ထဲမှာ နေရပြီး အဲဒီ array ရဲ့ အရွယ်အစားကို ကြိုရွေးထားရပါတယ်။ နာမည်က ထင်ထားတာထက် ရှည်သွားရင် strcpy နဲ့ strcat က array ရဲ့ အဆုံးကို ကျော်ပြီး ဘေးမှာရှိတဲ့ memory ထဲ ဆက်ရေးပါတယ်။ အောက်က code ကို g++ -Wall -Wextra က warning တစ်ခုမှ မပြဘဲ compile လုပ်ပေးပါတယ်။ ကျွန်တော်တို့ စမ်းခဲ့တဲ့ စက်ပေါ်မှာတော့ program က segmentation fault နဲ့ crash ဖြစ်သွားပါတယ်။ တခြားစက်တစ်လုံးမှာ ပုံမှန်လို run သွားနိုင်တာက ပိုတောင် ဆိုးပါသေးတယ်။
#include <cstdio>
#include <cstring>
void greet(const char* name) {
char greeting[16]; // room for 15 characters plus '\0'
std::strcpy(greeting, "Hello, ");
std::strcat(greeting, name); // no size check at all
std::printf("%s\n", greeting);
}
int main() {
greet("Thuta"); // fits: 12 characters
greet("Thuta from the C++ class"); // 31 characters: writes past the end
}ဒါက ဘာကြောင့် ဖြစ်တာလဲ
C မှာ string type မရှိပါဘူး၊ '\0' byte နဲ့ ဆုံးတဲ့ character array တွေပဲ ရှိပြီး array က ဘယ်တော့မှ မကြီးလာပါဘူး။ Text ရေးတဲ့ function တိုင်းက နေရာလုံလောက်အောင် ကြိုချန်ထားတယ်လို့ သင့်ကို ယုံပါတယ်။ Bjarne Stroustrup က C ရဲ့ မြန်ဆန်မှုကို ထိန်းထားရင်း ဒီ စာရင်းကိုင်အလုပ်ကို ကိုယ်တိုင် သယ်ဆောင်တဲ့ type တွေ ထည့်ဖို့ C++ ကို ဖန်တီးခဲ့တာပါ — std::string က သူ့အရှည်ကို သူသိပြီး append လုပ်တဲ့အခါ ကြီးလာကာ scope ကနေ ထွက်တဲ့အခါ memory ကို ပြန်လွှတ်ပါတယ်။
Core concept: C ရဲ့ မြန်ဆန်မှု + အပိုကုန်ကျစရိတ် မရှိတဲ့ abstraction
Bjarne Stroustrup က ၁၉၇၉ ခုနှစ်မှာ Bell Labs မှာ "C with Classes" ဆိုပြီး C++ ကို စခဲ့ပါတယ် — Simula language ကနေ class idea ကို C ထဲ ယူလာတာပါ။ ၁၉၈၃ မှာ C++ လို့ နာမည်ပြောင်းခဲ့ပြီး ++ က C ရဲ့ increment operator ပါ၊ C ထက် တစ်လှမ်း ကျော်တယ်လို့ ဆိုလိုတာပါ။ C ရဲ့ extension အဖြစ် စခဲ့တာမို့ C code အများစု C++ အဖြစ် compile ဖြစ်ဆဲဖြစ်ပြီး C++ program တွေက C library တွေကို တိုက်ရိုက် ခေါ်သုံးနိုင်ပါတယ်။ ဒီ compatibility က C++ ကျယ်ကျယ်ပြန့်ပြန့် ပျံ့နှံ့ခဲ့ရတဲ့ အကြောင်းရင်းကြီး တစ်ခုပါ။
C လိုပဲ C++ ကို ကြိုတင်ပြီး native machine code အဖြစ် compile လုပ်ပြီး statically typed ဖြစ်ပါတယ် — variable တိုင်းရဲ့ type ကို compile time မှာ သတ်မှတ်ပြီးသားပါ။ Run time မှာ interpreter လည်း မရှိ၊ garbage collector လည်း မရှိပါဘူး။ Object တွေကို code ထဲမှာ မြင်ရတဲ့ နေရာတွေမှာ ဖန်တီးပြီး ဖျက်တာမို့ performance ကို ကြိုခန့်မှန်းနိုင်ပါတယ်။ Frame တစ်ခုကို millisecond ၁၆ အတွင်း ဆွဲရတဲ့ game တွေ၊ microsecond နဲ့ တိုင်းတာတဲ့ trading system တွေအတွက် ဒီ property က အရေးကြီးပါတယ်။
Stroustrup ရဲ့ design စည်းမျဉ်းက zero-overhead principle ပါ — မသုံးတဲ့ feature အတွက် ပေးဆပ်စရာ မလိုဘူး၊ သုံးတဲ့ feature ကိုလည်း ကိုယ်တိုင် လက်နဲ့ရေးရင် ဒီထက် ပိုကောင်းအောင် မရေးနိုင်ဘူး ဆိုတာပါ။ std::string က သူ့ character တွေဆီ pointer တစ်ခုနဲ့ အရှည်ကို ကိုင်ထားပြီး C မှာ လက်နဲ့ ရေးရမယ့် memory ယူတာ၊ copy ကူးတာတွေကိုပဲ လုပ်ပါတယ် — library ထဲမှာ တစ်ခါတည်း မှန်မှန်ကန်ကန် ရေးထားတာပဲ ကွာပါတယ်။
C++ က multi-paradigm language ပါ — C လို ရိုးရိုး function တွေ ရေးနိုင်တယ်၊ class နဲ့ အရာဝတ္ထုတွေကို model လုပ်နိုင်တယ်၊ template နဲ့ generic code ရေးနိုင်တယ်၊ function သေးသေးလေးတွေကို lambda အဖြစ် ပို့နိုင်တယ်။ Language ကို ISO committee က standardize လုပ်ပါတယ်။ C++98 က ပထမဆုံး standard ဖြစ်ပြီး C++11 က language ကို အရမ်းပြောင်းလဲခဲ့လို့ ရလဒ်ကို "modern C++" လို့ ခေါ်ကြပါတယ်။ အဲဒီနောက် သုံးနှစ်တစ်ခါ version အသစ် ထွက်ခဲ့ပါတယ် — C++14, C++17, C++20, C++23။ ဒီ course က C++20 ကို သုံးပါတယ်။
- C core: type, array, function
- Abstraction: string, vector, class
- Compiler က အကုန်လုံးကို machine code ပြောင်း
ဘယ်လောက် အမြင့်ကနေ ရေးမလဲ ကိုယ်ရွေးနိုင်ပြီး abstraction ရဲ့ ကုန်ကျစရိတ်ကို compiler က ဖယ်ရှားပေးပါတယ်။
အထွေထွေ ပုံစံ: std::string နဲ့ text ကိုင်တွယ်တာ
#include <string>
std::string text = "initial value";
text += " more text"; // append: the string grows by itself
std::string joined = first + second; // + joins two strings
text.size(); // number of characters, no '\0' countedstd::string က C ရဲ့ char array အတွက် C++ ရဲ့ အဖြေပါ။ Literal ကနေ ဖန်တီးပြီး += ဒါမှမဟုတ် + နဲ့ ဆက်ကာ size() နဲ့ အရှည်ကို မေးနိုင်ပါတယ်။ Buffer အရွယ်အစား ကြိုရွေးစရာ မလိုပါဘူး။ Strings lesson မှာ အသေးစိတ် လေ့လာရဦးမှာဖြစ်ပြီး ဒီမှာတော့ type တစ်ခုက ကိုယ့် memory ကို ကိုယ်စီမံတဲ့ C++ ပုံစံကို ပြဖို့ပါ။
Example: overflow မဖြစ်နိုင်တဲ့ နှုတ်ဆက်စာ
Problem section ထဲက အလုပ်ကိုပဲ std::string နဲ့ လုပ်ကြည့်ရအောင်။ Array အရွယ်အစား မရှိ၊ strcpy မရှိ၊ '\0' ကို စိုးရိမ်စရာ မရှိပါဘူး။ Text ကို ဘယ်လောက်ပဲ ထပ်ဆက်ဆက် string က လိုအပ်တဲ့ memory ကို သူ့ဟာသူ ယူပါတယ်။
#include <iostream>
#include <string>
int main() {
std::string language = "C++";
std::string greeting = "Hello from " + language + "!";
greeting += " This text grows as needed.";
std::cout << greeting << '\n';
std::cout << "Characters: " << greeting.size() << '\n';
return 0;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 2
#include <string>std::string ကို declare လုပ်ထားတဲ့ header ပါ။ ဒါက C ရဲ့ <cstring> (strcpy, strcat) နဲ့ မတူပါဘူး၊ <string> က class type တစ်ခုကို ပေးပါတယ်။
- Line 6
std::string greeting = "Hello from " + language + "!";+ က string တွေကို ဆက်ပြီး result နဲ့ ကိုက်တဲ့ အရွယ်အစားရှိ string အသစ်တစ်ခု ဖန်တီးပါတယ်။ ဘယ်နှလုံး ဆံ့မလဲ ဆိုတာ ကိုယ်တိုင် တွက်စရာ မလိုပါဘူး။
- Line 7
greeting += " This text grows as needed.";strcat နဲ့ အလုပ်တူပေမယ့် နေရာမလောက်ရင် string က ပိုကြီးတဲ့ memory ကို ယူပြီး ရှိပြီးသား text ကို ရွှေ့ပါတယ်။ Overflow ဖြစ်စရာ နေရာ မရှိပါဘူး။
- Line 10
greeting.size()String က ကိုယ့်အရှည်ကို မှတ်ထားလို့ size() က ချက်ချင်း 42 ပြန်ပေးပါတယ်။ C ရဲ့ strlen ကတော့ '\0' ကို ရှာတွေ့တဲ့အထိ character တိုင်းကို လိုက်ရေရပါတယ်။
- Line 11
return 0;main ပြီးသွားတဲ့အခါ language နဲ့ greeting ကို ဖျက်ပြီး သူတို့ memory ကို အလိုအလျောက် ပြန်လွှတ်ပါတယ် — free() ခေါ်စရာ မလိုပါဘူး။
Hello from C++! This text grows as needed.
Characters: 42C နဲ့ C++ ကို ဘေးချင်းယှဉ်ကြည့်မယ်
| လုပ်ချင်တဲ့ အလုပ် | C | C++ |
|---|---|---|
| Text သိမ်းတာ | အရွယ်အစား ပုံသေ char array | ကြီးလာနိုင်တဲ့ std::string |
| ကြီးလာနိုင်တဲ့ list | malloc နဲ့ realloc ကို လက်နဲ့ | std::vector |
| Data နဲ့ သူ့ကို သုံးတဲ့ function | struct နဲ့ သီးခြား function တွေ | Member function ပါတဲ့ class |
| Memory ရှင်းတာ | free() ကို ကိုယ်တိုင် ခေါ် | Destructor တွေ အလိုအလျောက် run (RAII) |
| Type အမျိုးမျိုးအတွက် algorithm တစ်ခု | Macro ဒါမှမဟုတ် void pointer | Template |
C ကော်လံထဲက အရာအားလုံး C++ မှာ compile ဖြစ်ဆဲမို့ code ဟောင်းတွေထဲမှာ တွေ့ရပါလိမ့်မယ်။ ဒီ course က C++ ကော်လံကို သုံးပြီး RAII, class နဲ့ template တွေကို သူတို့ကိုယ်ပိုင် lesson တွေမှာ အသေးစိတ် သင်ပါမယ်။
C++ ကို Python နဲ့ ယှဉ်ကြည့်ရင်
| မေးခွန်း | C++ | Python |
|---|---|---|
| Code ကို ဘယ်တော့ ဘာသာပြန်လဲ | Run မလုပ်ခင် compiler က တစ်ခါတည်း | Run နေစဉ် interpreter က |
| Type error ကို ဘယ်တော့ တွေ့လဲ | Compile time မှာ | အဲဒီ line ကို run တဲ့အချိန် |
| Memory ကို ဘယ်သူ ပြန်လွှတ်လဲ | Object ရဲ့ scope ဆုံးချိန်မှာ destructor တွေ | Reference counting နဲ့ cycle collector သုံးပြီး runtime က |
| ဘာကို လဲလှယ်ရလဲ | သင်ရမယ့် စည်းမျဉ်း ပိုများပြီး မြန်ဆန်မှုနဲ့ ထိန်းချုပ်မှု ရ | မြန်ဆန်မှု လျော့ပြီး အမြန် စမ်းသပ်နိုင်မှု ရ |
ဒီ language နှစ်ခုက ပြိုင်ဘက်တွေထက် မိတ်ဖက်တွေ ဖြစ်တတ်ပါတယ်။ Python library ကြီးတော်တော်များများရဲ့ အထဲမှာ အလုပ်အများဆုံး လုပ်တဲ့ အပိုင်းကို C ဒါမှမဟုတ် C++ နဲ့ ရေးထားပြီး Python က အဲဒါကို ခေါ်သုံးဖို့ လွယ်ကူတဲ့ interface ပေးတာပါ။
လက်တွေ့ project တွေမှာ
ဒီနေ့ C++ ကို ဘယ်မှာ ရွေးချယ်ကြလဲ၊ ဘာကြောင့်လဲ
Chrome နဲ့ Node.js ထဲမှာ JavaScript ကို run ပေးတဲ့ V8 engine၊ Godot game engine၊ MongoDB database server နဲ့ TensorFlow ရဲ့ core တို့ကို C++ နဲ့ ရေးထားပါတယ်။ Robotics မှာ ROS 2 ရဲ့ C++ client library ကို robot ထိန်းချုပ်တဲ့ loop တွေအတွက် သုံးကြပြီး ကားနဲ့ လေကြောင်း software အတော်များများကိုလည်း MISRA C++ လို တင်းကျပ်တဲ့ coding standard တွေအောက်မှာ C++ နဲ့ ရေးကြပါတယ်။ Financial trading firm တွေကလည်း microsecond တွေ အရေးကြီးတဲ့ system တွေအတွက် C++ ကို ရွေးကြပါတယ်။
ရွေးချယ်ရတဲ့ အကြောင်းရင်းတွေက ဆင်တူပါတယ် — garbage collector ကြောင့် ရပ်တန့်သွားတာ မရှိတဲ့ ကြိုခန့်မှန်းနိုင်တဲ့ performance၊ memory layout ကို ထိန်းချုပ်နိုင်မှု၊ operating system နဲ့ hardware API တွေကို တိုက်ရိုက် ခေါ်နိုင်မှုနဲ့ ဆယ်စုနှစ်များစွာ ရှိခဲ့ပြီးသား library တွေပါ။ အဲဒီ code တွေ အများစုက ဟောင်းနေတာမို့ အလုပ်ခွင်မှာ C ပုံစံနဲ့ modern C++ ပုံစံ နှစ်မျိုးလုံး ဖတ်ရပါလိမ့်မယ်၊ ဒါပေမယ့် code အသစ်တွေကိုတော့ team တွေက modern ပုံစံနဲ့ ရေးဖို့ ပိုပိုပြီး တောင်းဆိုလာကြပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. .cpp file ထဲမှာ C ကို ရေးတာ
char array, malloc/free နဲ့ printf ကိုပဲ သုံးထားတဲ့ program က C++ compiler နဲ့ compile ဖြစ်ပေမယ့် C++ ပေးတဲ့ ဘေးကင်းမှုကို လုံးဝ မရပါဘူး။ Text အတွက် std::string၊ ကြီးလာမယ့် list အတွက် std::vector ကို default အဖြစ် ရွေးပါ။ C ပုံစံကို C library တစ်ခုနဲ့ ချိတ်ဆက်ရမယ့် နေရာလောက်မှာပဲ ချန်ထားပါ။
2. C code တိုင်း C++ အဖြစ် compile ဖြစ်မယ်လို့ ထင်တာ
C++ က C ထက် ပိုတင်းကျပ်ပါတယ်။ int* p = malloc(sizeof(int)); က C မှာ ရပေမယ့် g++ က "invalid conversion from 'void*' to 'int*'" လို့ ငြင်းပါတယ်၊ C++ က void* ကနေ ပြောင်းတာကို အလိုအလျောက် မလုပ်ပေးလို့ပါ။ new, class, delete လို C++ keyword တွေကို variable နာမည်အဖြစ် သုံးထားတဲ့ C code ကလည်း compile မဖြစ်ပါဘူး။
3. std::string က access တိုင်းကို ဘေးကင်းစေတယ်လို့ ထင်တာ
std::string က overflow ကို ကာကွယ်ပေးပေမယ့် square bracket နဲ့ index လုပ်တာကို မစစ်ပါဘူး — character 3 လုံးပဲ ရှိတဲ့ string မှာ s[10] ဆိုတာ undefined behavior ပါပဲ။ စစ်ချင်ရင် s.at(10) ကို သုံးပါ၊ index မှားရင် std::out_of_range exception ပစ်ပါတယ်။
Mini challenge
Mini challenge: နာမည်ကတ်
std::string တွေကိုပဲ သုံးပြီး နာမည်ကတ် တစ်ခု ဆောက်ပြီး print ထုတ်ပါ။ Char array နဲ့ အရွယ်အစား ကြိုတွက်တာ မပါရပါဘူး။
- ပထမနာမည်၊ နောက်ဆုံးနာမည်နဲ့ role ကို std::string variable သုံးခုထဲ သိမ်းရမယ်
- + နဲ့ += ကို သုံးပြီး "Name: <first> <last> (<role>)" ဆိုတဲ့ string တစ်ခု ဆောက်ရမယ်
- Badge ကို print ထုတ်ပြီး character အရေအတွက်ကိုလည်း print ထုတ်ရမယ်
- std::string(count, '=') နဲ့ badge နဲ့ အရှည်တူတဲ့ = line ကို badge ရဲ့ အပေါ်နဲ့ အောက်မှာ print ထုတ်ရမယ်
#include <iostream>
#include <string>
int main() {
// TODO: three std::string variables: first name, last name, role
// TODO: badge = "Name: " + first + " " + last
// TODO: append " (" + role + ")" with +=
// TODO: a line of '=' as long as the badge: std::string line(count, '=');
// TODO: print line, badge, line, then "Badge length: <n>"
return 0;
}ရလဒ်: Thuta Aung (C++ learner) အတွက် = သုံးဆယ်လုံးရှိတဲ့ line၊ Name: Thuta Aung (C++ learner)၊ နောက်ထပ် = line တစ်ကြောင်း၊ ပြီးရင် Badge length: 30။
အရိပ်အမြွက် ကြည့်ရန်
"Name: " + first က ရလို့ပါတယ်၊ ဘက်တစ်ဖက်က std::string ဖြစ်နေလို့ပါ။ std::string line(badge.size(), '='); က badge.size() လုံးရှိတဲ့ '=' string ကို ဖန်တီးပါတယ်။
အဖြေ ကြည့်ရန်
#include <iostream>
#include <string>
int main() {
std::string first = "Thuta";
std::string last = "Aung";
std::string role = "C++ learner";
std::string badge = "Name: " + first + " " + last;
badge += " (" + role + ")";
std::string line(badge.size(), '=');
std::cout << line << '\n' << badge << '\n' << line << '\n';
std::cout << "Badge length: " << badge.size() << '\n';
return 0;
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- C++ ကို Bjarne Stroustrup က "C with Classes" အဖြစ် ၁၉၇၉ မှာ စခဲ့ပြီး ၁၉၈၃ မှာ C++ လို့ နာမည်ပြောင်းခဲ့ပါတယ်။
- C++ က native code အဖြစ် compile လုပ်တဲ့ statically typed language ဖြစ်ပြီး garbage collector မရှိပါဘူး။
- Zero-overhead principle: မသုံးတာအတွက် မပေးရ၊ သုံးတာကို လက်နဲ့ရေးရင် ဒီထက် မကောင်းနိုင်။
- std::string လို type တွေက C မှာ လက်နဲ့ လုပ်ရတဲ့ memory စာရင်းကိုင် အလုပ်ကို ကိုယ်တိုင် လုပ်ပေးပါတယ်။
- C++98 ကနေ C++23 အထိ standard တွေ ရှိပြီး ဒီ course က C++20 ကို သုံးပါတယ်။
The Current ISO C++ Standard (C++23) — Standard C++ Foundation