ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
ဒီ project မှာ Basic chapter က variable, function, loop အကြောင်း၊ Intermediate chapter က array, pointer, reference အကြောင်း၊ Advanced chapter က class, object, access specifier အကြောင်း သင်ခဲ့တာတွေအားလုံးကို ပေါင်းစပ်သုံးပြီး Student Record Manager လို့ခေါ်တဲ့ small console application တစ်ခု တည်ဆောက်ပါမယ်။ Part 1 မှာတော့ program ရဲ့ foundation ဖြစ်တဲ့ Student class ကို design လုပ်ပြီး, private data member တွေကို encapsulation နည်းနဲ့ ကာကွယ်ပြီး public method တွေကနေပဲ access ဖြစ်အောင်စီစဉ်ပါမယ်။ vector<Student> ကို သုံးပြီး student list တစ်ခုလုံးကို memory ထဲမှာ dynamic ဖြစ်အောင် သိမ်းထားမှာပါ။ info Part 2 မှာ add/search/display feature တွေ ထည့်မှာဖြစ်ပြီး Part 3 မှာ file save/load နဲ့ error handling နဲ့ project ပြီးအောင် ပြီးစီးပါမယ်။
လက်တွေ့ ဆောက်ကြည့်မယ်
Student class ကို name (string), roll (int), marks (double) ဆိုပြီး private member 3 ခုနဲ့ တည်ဆောက်ပါ။ Constructor တစ်ခုနဲ့ getName(), getRoll(), getMarks() လို getter method တွေကို public အောက်မှာထားပါ။ main() function ထဲမှာ vector<Student> students; declare လုပ်ပြီး, menu loop တစ်ခုကို while(true) နဲ့ ဖန်တီးပါ - user ကို 1) Add Student 2) Display All 3) Exit ဆိုပြီး choice ပြပါ။ switch statement နဲ့ user ရွေးချယ်မှုအလိုက် function တွေခေါ်နိုင်အောင် skeleton ချထားပါ - Part 2 မှာ logic တွေ ဆက်ဖြည့်သွားပါမယ်။
Code နမူနာ
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Student {
private:
string name;
int roll;
double marks;
public:
Student(string n, int r, double m) {
name = n;
roll = r;
marks = m;
}
string getName() const { return name; }
int getRoll() const { return roll; }
double getMarks() const { return marks; }
};
int main() {
vector<Student> students;
int choice;
while (true) {
cout << "\n--- Student Manager ---\n";
cout << "1. Add Student\n";
cout << "2. Display All\n";
cout << "3. Exit\n";
cout << "Choice: ";
cin >> choice;
switch (choice) {
case 1:
// Part 2 မှာ ဖြည့်ပါမယ်
break;
case 2:
// Part 2 မှာ ဖြည့်ပါမယ်
break;
case 3:
cout << "Goodbye!\n";
return 0;
default:
cout << "Invalid choice!\n";
}
}
}Program run ရင် menu 3 ခု ပြပြီး, choice 3 ရွေးလိုက်ရင် "Goodbye!" ပြပြီး program ပြီးဆုံးသွားမှာဖြစ်ပါတယ်။၅ မိနစ် စမ်းကြည့်
Student class ထဲမှာ setMarks(double newMarks) ဆိုတဲ့ setter method တစ်ခု ထပ်ထည့်ပြီး marks ကို update လုပ်နိုင်အောင် ကြိုးစားကြည့်ပါ။
သတိလေးတစ်ချက်
Encapsulation ကို အစကတည်းက သေချာစီစဉ်ထားမှ Part 2, 3 မှာ code ပိုင်းတွေ ထပ်ပေါင်းရင် ချောမွေ့ပါမယ်။ getter/setter pattern ကို ဒီအတိုင်း အစဉ်လိုက်ထားပါ။