ទិដ្ឋភាពទូទៅ C++ និង Program ដំបូង
Introduction to C++ — What is C++, Setup & Hello World
C++ គឺជាភាសា Programming ដែលត្រូវបានបង្កើតឡើងដោយ Bjarne Stroustrup ក្នុងឆ្នាំ ១៩៧៩។ វាគឺជាការពង្រីកបន្ថែមពី C Language ដោយបន្ថែម Object-Oriented Programming (OOP)។
ហេតុអ្វីត្រូវរៀន C++?
- ដំណើរការលឿន (Fast performance)
- ប្រើក្នុង Game Development, System Programming, Embedded Systems
- ជួយយល់ Memory Management ដ៏ស៊ីជម្រៅ
- Base ល្អសម្រាប់ភាសាផ្សេងទៀត
Program C++ ដំបូងបំផុត:
#include <iostream> using namespace std; int main() { // បោះពុម្ពពាក្យ Hello World cout << "Hello, World!" << endl; return 0; }
main() — គឺជា function ចម្បង ដែល Program ចាប់ផ្តើមដំណើរការ
cout — ប្រើសម្រាប់បោះពុម្ព (print) ទៅ Screen
Variable និង Data Types
Variables, Data Types & Constants
Variable គឺជាកន្លែងស្តុកទុកទិន្នន័យ (data) ក្នុង Memory។ C++ មាន Data Types ជាច្រើន:
| Data Type | ន័យ | ទំហំ | ឧទាហរណ៍ |
|---|---|---|---|
int | ចំនួនគត់ | 4 bytes | int x = 10; |
float | ចំនួនទស្សភាគ | 4 bytes | float f = 3.14; |
double | ចំនួនទស្សភាគ (ច្រើន) | 8 bytes | double d = 3.14159; |
char | តួអក្សរ ១ | 1 byte | char c = 'A'; |
bool | true/false | 1 byte | bool b = true; |
string | ខ្សែអក្សរ | Dynamic | string s = "Hello"; |
#include <iostream> #include <string> using namespace std; int main() { // ប្រកាស Variable int age = 20; float height = 1.75; char grade = 'A'; bool isStudent = true; string name = "សុខ វិចិត្រ"; // Constant — តម្លៃមិនអាចផ្លាស់ប្តូរ const double PI = 3.14159265; cout << "ឈ្មោះ: " << name << endl; cout << "អាយុ: " << age << endl; cout << "កម្ពស់: " << height << " m" << endl; cout << "PI = " << PI << endl; return 0; }
អាយុ: 20
កម្ពស់: 1.75 m
PI = 3.14159
Operators (ប្រមាណវិធី)
Arithmetic, Comparison, Logical & Assignment Operators
| ប្រភេទ | Operators | ន័យ |
|---|---|---|
| Arithmetic | + - * / % | គណនា (+, -, x, /, សំណល់) |
| Comparison | == != > < >= <= | ប្រៀបធៀប |
| Logical | && || ! | AND, OR, NOT |
| Assignment | = += -= *= /= | ដាក់តម្លៃ |
| Increment | ++ -- | បន្ថែម/ដក ១ |
#include <iostream> using namespace std; int main() { int a = 10, b = 3; cout << "a + b = " << (a + b) << endl; // 13 cout << "a - b = " << (a - b) << endl; // 7 cout << "a * b = " << (a * b) << endl; // 30 cout << "a / b = " << (a / b) << endl; // 3 cout << "a % b = " << (a % b) << endl; // 1 // Comparison cout << "a > b: " << (a > b) << endl; // 1 (true) cout << "a == b: " << (a == b) << endl; // 0 (false) return 0; }
Input / Output
cin, cout, getline — ទទួល និង បោះពុម្ព Data
#include <iostream> #include <string> using namespace std; int main() { int age; string name; cout << "សូមបញ្ចូលឈ្មោះ: "; getline(cin, name); // ទទួលខ្សែអក្សរ (រួមទាំងដកឃ្លា) cout << "សូមបញ្ចូលអាយុ: "; cin >> age; cout << "------------------" << endl; cout << "ឈ្មោះ: " << name << endl; cout << "អាយុ: " << age << " ឆ្នាំ" << endl; return 0; }
Control Flow — if / else / switch
Conditional Statements — ការសម្រេចចិត្ត
#include <iostream> using namespace std; int main() { int score; cout << "បញ្ចូលពិន្ទុ: "; cin >> score; // if / else if / else if (score >= 90) { cout << "ថ្នាក់ A - ល្អប្រសើរ!" << endl; } else if (score >= 80) { cout << "ថ្នាក់ B - ល្អ" << endl; } else if (score >= 70) { cout << "ថ្នាក់ C - មធ្យម" << endl; } else { cout << "ថ្នាក់ F - ត្រូវពិនិត្យម្តងទៀត" << endl; } // switch statement int day = 3; switch (day) { case 1: cout << "ថ្ងៃចន្ទ" << endl; break; case 2: cout << "ថ្ងៃអង្គារ" << endl; break; case 3: cout << "ថ្ងៃពុធ" << endl; break; default: cout << "ថ្ងៃផ្សេងទៀត" << endl; } return 0; }
Loops (រង្វិលជុំ)
for, while, do-while loops
#include <iostream> using namespace std; int main() { // for loop cout << "=== for loop ===" << endl; for (int i = 1; i <= 5; i++) { cout << "i = " << i << endl; } // while loop cout << "=== while loop ===" << endl; int n = 1; while (n <= 5) { cout << "n = " << n << endl; n++; } // do-while loop cout << "=== do-while ===" << endl; int x = 1; do { cout << "x = " << x << endl; x++; } while (x <= 3); // break and continue for (int j = 0; j < 10; j++) { if (j == 3) continue; // រំលងលេខ 3 if (j == 6) break; // ឈប់នៅ 6 cout << j << " "; } cout << endl; return 0; }
Functions (មុខងារ)
Function Declaration, Parameters, Return Values, Overloading
#include <iostream> using namespace std; // Function ដែល return តម្លៃ int add(int a, int b) { return a + b; } // Function ដែលមិន return (void) void greet(string name) { cout << "សួស្តី, " << name << "!" << endl; } // Function Overloading (ឈ្មោះដូចគ្នា Parameters ខុសគ្នា) double multiply(double a, double b) { return a * b; } int multiply(int a, int b) { return a * b; } // Default Parameters void showInfo(string name, int age = 18) { cout << name << " | អាយុ: " << age << endl; } int main() { cout << add(5, 3) << endl; // 8 greet("វណ្ណា"); cout << multiply(2.5, 4.0) << endl; // 10.0 showInfo("សុភា"); // age=18 (default) showInfo("វិចិត្រ", 25); return 0; }
Arrays & Strings
1D/2D Arrays, String Manipulation
#include <iostream> #include <string> using namespace std; int main() { // 1D Array int scores[5] = {85, 92, 78, 95, 88}; // Loop តាម Array int sum = 0; for (int i = 0; i < 5; i++) { sum += scores[i]; } cout << "មធ្យម: " << (sum / 5.0) << endl; // 2D Array (Matrix)for (int r = 0; r < 2; r++) { for (int c = 0; c < 3; c++) { cout << matrix[r][c] << " "; } cout << endl; } // String operations string s = "Hello C++"; cout << "ប្រវែង: " << s.length() << endl; cout << "Upper: " << s.substr(0, 5) << endl; return 0; }int matrix[2][3] = {{1,2,3}, {4,5,6}};
Pointers (ចង្អុលបង្ហាញ)
Pointers, Memory Address, Dynamic Memory
Pointer គឺជា Variable ដែលស្តុកទុក Address (អាសយដ្ឋាន) នៅក្នុង Memory ជំនួសឱ្យតម្លៃ។
#include <iostream> using namespace std; int main() { int x = 42; int* ptr = &x; // ptr ចង្អុល Address នៃ x cout << "តម្លៃ x: " << x << endl; // 42 cout << "Address x: " << &x << endl; // 0x...... cout << "ptr ចង្អុល: " << ptr << endl; // Address (ដូចគ្នា) cout << "*ptr = " << *ptr << endl; // 42 (Dereference) // Dynamic Memory Allocation int* arr = new int[5]; // ជួលអង្គចាំ for (int i = 0; i < 5; i++) arr[i] = i * 10; for (int i = 0; i < 5; i++) cout << arr[i] << " "; cout << endl; delete[] arr; // ត្រូតែ Free Memory! return 0; }
Structs (រចនាសម្ព័ន្ធ)
Custom Data Structures with struct
#include <iostream> #include <string> using namespace std; // ប្រកាស Struct struct Student { string name; int age; float gpa; }; void printStudent(Student s) { cout << "ឈ្មោះ: " << s.name << " | អាយុ: " << s.age << " | GPA: " << s.gpa << endl; } int main() { Student s1 = {"វណ្ណា", 20, 3.8}; Student s2 = {"សុភា", 22, 3.5}; printStudent(s1); printStudent(s2); // Array of Structs Student students[3] = { {"ដារ៉ា", 19, 3.9}, {"ចន្ទារ", 21, 3.2}, {"ពិសី", 20, 3.7} }; for (int i = 0; i < 3; i++) printStudent(students[i]); return 0; }
OOP — Classes និង Objects
Object-Oriented Programming, Constructors, Encapsulation
OOP (Object-Oriented Programming) គឺជាការចាត់រៀបចំ Code ដោយប្រើ Class ដែលមាន Data និង Function រួមគ្នា។
#include <iostream> #include <string> using namespace std; class BankAccount { private: // ចូលពីខាងក្រៅមិនបាន string owner; double balance; public: // ចូលពីខាងក្រៅបាន // Constructor BankAccount(string name, double initial) { owner = name; balance = initial; } void deposit(double amount) { balance += amount; cout << "ដាក់ប្រាក់: $" << amount << endl; } void withdraw(double amount) { if (amount > balance) { cout << "ប្រាក់មិនគ្រប់គ្រាន់!" << endl; } else { balance -= amount; cout << "ដក: $" << amount << endl; } } void showBalance() { cout << owner << " | ប្រាក់: $" << balance << endl; } }; int main() { BankAccount acc("វណ្ណា", 1000.0); acc.deposit(500); acc.withdraw(200); acc.showBalance(); return 0; }
ដក: $200
វណ្ណា | ប្រាក់: $1300
Inheritance & Polymorphism
ការទទួលមរតក Class, Virtual Functions
#include <iostream> using namespace std; // Base Class (Parent) class Animal { public: string name; Animal(string n) : name(n) {} virtual void speak() { // virtual = Polymorphism cout << name << ": ...\n"; } }; // Derived Class (Child) class Dog : public Animal { public: Dog(string n) : Animal(n) {} void speak() override { cout << name << ": វូហ្វវូ!\n"; } }; class Cat : public Animal { public: Cat(string n) : Animal(n) {} void speak() override { cout << name << ": ម្យាវវ!\n"; } }; int main() { Animal* animals[] = { new Dog("បូ"), new Cat("មៀវ"), new Dog("រ៉ិចឆ្ហី") }; for (auto a : animals) a->speak(); // Polymorphism! return 0; }
Templates
Generic Programming — Function & Class Templates
#include <iostream> using namespace std; // Function Template — ប្រើជាមួយ Type ណាក៏បាន template<typename T> T maxVal(T a, T b) { return (a > b) ? a : b; } // Class Template template<typename T> class Box { private: T value; public: Box(T v) : value(v) {} void show() { cout << "Box: " << value << endl; } }; int main() { cout << maxVal(3, 7) << endl; // 7 (int) cout << maxVal(3.5, 2.1) << endl; // 3.5 (double) cout << maxVal('z', 'a') << endl; // z (char) Box<int> intBox(42); Box<string> strBox("Hello"); intBox.show(); strBox.show(); return 0; }
STL — Standard Template Library
vector, map, set, algorithm
#include <iostream> #include <vector> #include <map> #include <algorithm> using namespace std; int main() { // VECTOR — Dynamic Array vector<int> v = {5, 2, 8, 1, 9}; v.push_back(4); // បន្ថែមនៅខាងចុង sort(v.begin(), v.end()); // Sort ចំនួន cout << "Vector: "; for (auto n : v) cout << n << " "; cout << endl; // 1 2 4 5 8 9 // MAP — Key-Value Pairs map<string, int> scores; scores["វណ្ណា"] = 95; scores["សុភា"] = 87; scores["ដារ៉ា"] = 92; cout << "-- ពិន្ទុ --" << endl; for (auto& p : scores) { cout << p.first << ": " << p.second << endl; } // Algorithm auto it = max_element(v.begin(), v.end()); cout << "ធំបំផុត: " << *it << endl; return 0; }
File I/O (ការអាន/សរសេរ File)
fstream, ifstream, ofstream
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { // ===== សរសេរ File ===== ofstream outFile("students.txt"); if (outFile.is_open()) { outFile << "វណ្ណា 95\n"; outFile << "សុភា 87\n"; outFile << "ដារ៉ា 92\n"; outFile.close(); cout << "សរសេរ File បានជោគជ័យ!" << endl; } // ===== អាន File ===== ifstream inFile("students.txt"); string line; cout << "=== មាតិកា File ===" << endl; while (getline(inFile, line)) { cout << line << endl; } inFile.close(); return 0; }
Exception Handling (ការគ្រប់គ្រងកំហុស)
try, catch, throw — Error Handling
#include <iostream> #include <stdexcept> using namespace std; double divide(double a, double b) { if (b == 0) { throw invalid_argument("មិនអាចចែកនឹង 0 បាន!"); } return a / b; } int main() { // try-catch Block try { cout << divide(10, 2) << endl; // 5 cout << divide(10, 0) << endl; // Error! } catch (const invalid_argument& e) { cout << "Error: " << e.what() << endl; } catch (...) { cout << "Error មិនស្គាល់!" << endl; } // Multiple exceptions try { vector<int> v = {1, 2, 3}; cout << v.at(10) << endl; // Out of range! } catch (const out_of_range& e) { cout << "Out of range: " << e.what() << endl; } cout << "Program បន្តដំណើរការ..." << endl; return 0; }
Error: មិនអាចចែកនឹង 0 បាន!
Out of range: vector::_M_range_check
Program បន្តដំណើរការ...
🎉 អបអរសាទរ! អ្នកបានបញ្ចប់មេរៀន C++ ទាំងអស់!
ជំហានបន្ទាប់: សាកល្បងសរសេរ Projects ផ្ទាល់ខ្លួន ដើម្បីពង្រឹងជំនាញ!