Thuta Learning
C++
ProjectsProgrammingအခြေခံ

Mini Project: Student Manager - Part 1

C++သင်ခန်းစာ 3135 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Private data ကို စစ်ဆေးပြီးသား member function တွေကနေပဲ ပြောင်းလို့ရတဲ့ Student class ကို design လုပ်နိုင်ဖို့
  • Constructor member initializer list နဲ့ const getter တွေကို မှန်မှန်ကန်ကန် သုံးနိုင်ဖို့
  • main() ထဲက လွတ်နေတဲ့ vector အစား StudentManager class တစ်ခုကို student list ရဲ့ တစ်ဦးတည်းသော owner အဖြစ် ထားနိုင်ဖို့
  • std::istream ကနေ ဖတ်ပြီး input ကုန်သွားရင် သပ်သပ်ရပ်ရပ် ရပ်သွားတဲ့ menu loop ကို ရေးနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

နောက် lesson သုံးခုမှာ console program သေးသေးလေး တစ်ခုတည်းကို ဆက်တိုက် ဆောက်သွားပါမယ်။ ကျောင်းသားတစ်ယောက်ချင်းရဲ့ နာမည်၊ roll number နဲ့ marks ကို သိမ်းပေးတဲ့ Student Manager ပါ။ Part 1 မှာ အရိုးစု (skeleton) ကို ဆောက်ပါမယ်။ Part 2 မှာ add၊ list၊ search feature တွေ ထည့်ပြီး Part 3 မှာ input မှားတာကို ခံနိုင်အောင်နဲ့ program ပိတ်ပြီး ပြန်ဖွင့်ရင်လည်း data မပျောက်အောင် လုပ်ပါမယ်။

ကျောင်းရုံးခန်းတစ်ခုကို မြင်ကြည့်ပါ။ စာရင်းစာအုပ်ထဲမှာ record တွေ ရှိတယ်၊ အဲဒီစာအုပ်ထဲ ရေးခွင့်ရှိတာ စာရေးတစ်ယောက်တည်းပဲ၊ တောင်းဆိုချက်တွေက ကောင်တာပေါက်ကနေ ဝင်လာပါတယ်။ ကျွန်တော်တို့ program မှာ Student class က record တစ်ခု၊ StudentManager က စာရင်းစာအုပ်ကို ပိုင်တဲ့ စာရေး၊ menu loop က ကောင်တာပေါက် ဖြစ်ပါတယ်။ Part 1 က ဒီအပိုင်းသုံးခုကို ဆောက်ပြီး အချင်းချင်း ချိတ်မိရဲ့လားဆိုတာ စမ်းကြည့်ပါတယ်။

Problem → Why → Solution

ပြဿနာ: ဘယ်သူမဆို ပြင်လို့ရတဲ့ record

ပထမဆုံး ရေးကြည့်တဲ့ version က field အားလုံးကို public ထားပြီး list ကို main() ထဲက ရိုးရိုး vector အဖြစ် ထားလေ့ရှိပါတယ်။ Run တော့ run ပါတယ်၊ ဒါပေမဲ့ bug တစ်ခု ဒါမှမဟုတ် team member တစ်ယောက်က record ထဲကို အဓိပ္ပာယ်မရှိတဲ့ value ရေးတာကို ဘာမှ မတားပါဘူး။ အောက်က draft မှာ line တစ်ကြောင်းက Bo ရဲ့ marks ကို 94 အစား 940 လို့ ထားလိုက်ပြီး နောက်တစ်ကြောင်းက နာမည်အလွတ်နဲ့ student တစ်ယောက်ကို ထည့်လိုက်ပါတယ်။ Program က နှစ်ခုလုံးကို ဘာမှမပြောဘဲ လက်ခံလိုက်လို့ class average က အဓိပ္ပာယ်မရှိတော့ပါဘူး။

Menu မှာလည်း ထောင်ချောက် နောက်တစ်ခု ရှိပါတယ်။ while (true) နဲ့ loop ပတ်ပြီး std::cin >> choice နဲ့ ဖတ်ပေမယ့် ဖတ်တာ အောင်မြင်ရဲ့လား မစစ်ပါဘူး။ User က စာလုံးတစ်လုံး ရိုက်လိုက်ရင် read က fail ဖြစ်၊ choice က 0 ဖြစ်သွားပြီး နောက်ပိုင်း read တိုင်း ချက်ချင်း fail ဖြစ်တော့တာမို့ program က "Invalid choice!" ကို အဆုံးမရှိ print ထုတ်နေပါတော့တယ်။

draft.cpp
struct Student {  // every field is public
    std::string name;
    int roll;
    double marks;
};

int main() {
    std::vector<Student> students;
    students.push_back({"Bo", 2, 94});
    students[0].marks = 940;          // typo: nothing objects
    students.push_back({"", 3, 70});  // empty name: accepted

    int choice;
    while (true) {
        std::cin >> choice;  // type a letter: the read fails every time
        if (choice == 3) {
            return 0;
        }
        std::cout << "Invalid choice!\n";  // printed forever
    }
}

Compiler က ဘာကြောင့် မကူညီနိုင်တာလဲ

Public field ပါတဲ့ struct က "ဘယ် code မဆို ဒီနေရာမှာ ဘယ် value မဆို ရေးလို့ရတယ်" လို့ ကတိပေးထားတာပါ။ Compiler က အဲဒီကတိကိုပဲ အတိအကျ လိုက်နာပေးတာမို့ 940 က တရားဝင်ပါတယ်။ "marks က 0 ကနေ 100 အတွင်း" ဆိုတဲ့ စည်းကမ်းကို အတင်းလိုက်နာစေချင်ရင် အဲဒီစည်းကမ်းက type ထဲမှာ ရှိနေရပါမယ်၊ ဆိုလိုတာက private data နဲ့ ပြောင်းလဲမှုတိုင်းကို စစ်တဲ့ member function တွေပါ။ Menu bug ကလည်း ဆင်တူပါတယ်။ Read တစ်ခု fail ဖြစ်ခဲ့တာကို stream က မှတ်ထားပြီး value ကို မသုံးခင် အဲဒါကို စစ်ဖို့က ကိုယ့်တာဝန်ပါ။

Core concept: rule ကို စောင့်တဲ့ class နဲ့ owner တစ်ဦးတည်း

Class ဆိုတာ field တွေထည့်ထားတဲ့ အိတ်တစ်လုံးထက် ပိုပါတယ်။ သူ့ရဲ့ တကယ့်အလုပ်က invariant ကို ထိန်းထားဖို့ပါ။ Invariant ဆိုတာ object တစ်ခု ဆောက်ပြီးချိန်ကနေ ဖျက်တဲ့အချိန်အထိ အမြဲမှန်နေရမယ့် စည်းကမ်းပါ။ Student အတွက် စည်းကမ်းက ရိုးရှင်းပါတယ်၊ marks က 0 နဲ့ 100 ကြားမှာ ရှိရပါမယ်။ Part 1 က ဒါကို အဆင့်လေးဆင့်နဲ့ အကောင်အထည်ဖော်ပါတယ်။

၁။ Data ကို private ထားပါ

name_၊ roll_ နဲ့ marks_ က private section ထဲမှာ ရှိလို့ class အပြင်က code က သူတို့ကို assign လုပ်လို့ မရပါဘူး။ နောက်ဆုံးက underscore က member variable မှန်း သိသာအောင်နဲ့ name လို parameter နာမည်တွေနဲ့ မရောအောင် လူသုံးများတဲ့ naming အလေ့အထပါ။

၂။ Constructor ထဲမှာ စစ်ပါ

Constructor က member initializer list ကို သုံးလို့ member တစ်ခုချင်းကို default value နဲ့ အရင်ဆောက်ပြီးမှ assign ပြန်လုပ်တာ မဟုတ်ဘဲ တစ်ခါတည်း တိုက်ရိုက် initialize လုပ်ပါတယ်။ marks_ ကို checkedMarks(marks) ကနေ initialize လုပ်ပါတယ်။ ဒါက 0 ကနေ 100 အပြင်က value ဆိုရင် std::invalid_argument throw လုပ်တဲ့ private static helper ပါ။ Throw ဖြစ်သွားရင် Student object လုံးဝ မရှိလာတော့လို့ မမှန်တဲ့ student တစ်ယောက်ကို ဘယ်တော့မှ သိမ်းမိမှာ မဟုတ်ပါဘူး။

၃။ const getter တွေနဲ့ ဖတ်ပါ

getName()၊ getRoll() နဲ့ getMarks() ကို const လို့ မှတ်ထားလို့ object ကို မပြင်ဘူးလို့ ကတိပေးထားပါတယ်။ ဒီကတိကြောင့် Part 2 မှာ const Student& ပေါ်ကနေ သူတို့ကို ခေါ်လို့ရပါတယ်။ getName() က const std::string& ကို return ပြန်လို့ နာမည်ဖတ်တိုင်း string ကို copy မကူးပါဘူး။

၄။ စစ်ထားတဲ့ တံခါးတစ်ပေါက်တည်းကနေ ပြောင်းပါ

setMarks() က checkedMarks() helper တစ်ခုတည်းကိုပဲ ခေါ်လို့ စည်းကမ်းက တစ်နေရာတည်းမှာပဲ ရှိပါတယ်။ setMarks မရှိရင် typo တစ်ခုကို ပြင်ဖို့ လမ်းမရှိပါဘူး၊ field က public ဆိုရင်တော့ typo ကို တားဖို့ လမ်းမရှိပါဘူး။

Student list မှာလည်း owner တစ်ယောက် လိုပါတယ်။ Function တိုင်းကို လက်ကမ်းပေးနေရတဲ့ main() ထဲက std::vector<Student> အစား StudentManager က vector ကို private member အဖြစ် ထားပြီး add() နဲ့ count() လို operation တွေကိုပဲ ဖွင့်ပေးထားပါတယ်။ Part 2 မှာ search နဲ့ display ကို ဒီ class ထဲပဲ ထည့်ပြီး Part 3 မှာ save လုပ်တာကို ထည့်ပါမယ်။ main() က vector ကို တိုက်ရိုက် ဘယ်တော့မှ မထိရပါဘူး။

နောက်ဆုံးအနေနဲ့ menu က std::cin ကို hard-code မလုပ်ဘဲ std::istream& ကနေ ဖတ်ပါတယ်။ std::cin က istream တစ်ခုဖြစ်သလို string ထဲကနေ ဖတ်တဲ့ std::istringstream လည်း istream ပါပဲ။ Demo မှာ key နှိပ်တာတွေကို string script အဖြစ် ထည့်ပေးလို့ run တိုင်း output တူတူ ထွက်ပါတယ်၊ တကယ့် program မှာတော့ std::cin ကို ပြောင်းစရာမလိုဘဲ ထည့်ပေးလိုက်ရုံပါပဲ။ while (in >> choice) ဆိုတဲ့ condition က number တစ်ခုကို အောင်အောင်မြင်မြင် ဖတ်နိုင်တဲ့အချိန်မှာပဲ true ဖြစ်လို့ input ကုန်သွားရင် ဒါမှမဟုတ် number မဟုတ်တာ ရောက်လာရင် loop က ရပ်သွားပါတယ်။

Mental model: record၊ စာရေး၊ ကောင်တာ
  1. Student: rule ကို ကိုယ်တိုင်စောင့်တဲ့ record
  2. StudentManager: vector ရဲ့ owner
  3. Menu loop: choice ဖတ်ပြီး manager ကို ခေါ်
  4. Value မှားရင် exception၊ ဘယ်တော့မှ မသိမ်း

Data နဲ့ပတ်သက်တဲ့ စည်းကမ်းတစ်ခုက အရေးကြီးရင် class ထဲမှာ ထည့်ထားပါ။ ဒါဆိုရင် ဘယ် caller ကမှ မေ့ကျန်ခဲ့လို့ မရတော့ပါဘူး။

အခြေခံ ပုံစံ

cpp
class Thing {
public:
    explicit Thing(Type value) : value_(checked(value)) {}
    Type getValue() const { return value_; }
    void setValue(Type value) { value_ = checked(value); }
private:
    static Type checked(Type value);  // throws when the rule is broken
    Type value_;
};

class ThingManager { /* private: std::vector<Thing> items_; */ };

Data ကို private ထား၊ constructor နဲ့ setter နှစ်ခုလုံးက စစ်ဆေးတဲ့ helper တစ်ခုတည်းကို ဖြတ်သွားရပါမယ်။ ဖတ်တဲ့ function တွေကို const လုပ်ပြီး object list ကိုတော့ manager class တစ်ခုက private vector အဖြစ် ပိုင်ပါတယ်။

Part 1 code: model နဲ့ menu skeleton

အောက်က file ထဲမှာ Part 1 program တစ်ခုလုံး ပါပါတယ်။ main() က model ကို အရင် တိုက်ရိုက် စမ်းပါတယ်။ Student နှစ်ယောက် ထည့်တယ်၊ Chit ရဲ့ marks ကို setMarks() နဲ့ ပြောင်းတယ်၊ marks 120 နဲ့ student တစ်ယောက်ကို ဆောက်ဖို့ ကြိုးစားတယ်။ ပြီးမှ keyboard ရှေ့က user ကိုယ်စား 1 2 7 3 ဆိုတဲ့ key script နဲ့ menu ကို run ပါတယ်။ Option 1 နဲ့ 2 က Part 2 မှာ ဖြည့်မယ့် placeholder တွေပါ။

Keyboard နဲ့ run ချင်ရင်

main() ထဲမှာ runMenu(keys, manager) အစား runMenu(std::cin, manager) လို့ ပြောင်းလိုက်ပါ။ runMenu က istream ဘယ်လိုအမျိုးအစားမဆို လက်ခံလို့ တခြားဘာမှ ပြင်စရာ မလိုပါဘူး။ Terminal က ကိုယ်ရိုက်တာကို ပြပြီးသားမို့ "> " echo line ကိုတော့ ဖျက်လို့ရပါတယ်။

student_manager_part1.cpp
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>

class Student {
public:
    Student(std::string name, int roll, double marks)
        : name_(std::move(name)), roll_(roll), marks_(checkedMarks(marks)) {}

    const std::string& getName() const { return name_; }
    int getRoll() const { return roll_; }
    double getMarks() const { return marks_; }

    void setMarks(double marks) { marks_ = checkedMarks(marks); }

private:
    static double checkedMarks(double marks) {
        if (marks < 0.0 || marks > 100.0) {
            throw std::invalid_argument("marks must be between 0 and 100");
        }
        return marks;
    }

    std::string name_;
    int roll_;
    double marks_;
};

class StudentManager {
public:
    void add(const Student& student) { students_.push_back(student); }
    std::size_t count() const { return students_.size(); }

private:
    std::vector<Student> students_;
};

void runMenu(std::istream& in, StudentManager& manager) {
    std::cout << "--- Student Manager ---\n"
              << "1. Add Student  2. Display All  3. Exit\n";
    int choice = 0;
    while (in >> choice) {
        std::cout << "> " << choice << '\n';  // echo: scripted keys are not typed
        switch (choice) {
        case 1:
            std::cout << "Add Student: coming in Part 2\n";
            break;
        case 2:
            std::cout << "Display All: coming in Part 2\n";
            break;
        case 3:
            std::cout << "Goodbye! " << manager.count() << " student(s) in memory.\n";
            return;
        default:
            std::cout << "Invalid choice: " << choice << '\n';
        }
    }
    std::cout << "Input ended before Exit.\n";
}

int main() {
    StudentManager manager;
    manager.add(Student("Aye", 1, 78.5));
    manager.add(Student("Bo", 2, 91.0));

    Student chit("Chit", 3, 64.0);
    chit.setMarks(70.0);
    std::cout << chit.getName() << " now has " << chit.getMarks() << " marks\n";
    manager.add(chit);

    try {
        manager.add(Student("Daw", 4, 120.0));
    } catch (const std::invalid_argument& e) {
        std::cout << "Rejected Daw: " << e.what() << '\n';
    }

    std::istringstream keys("1 2 7 3");  // the keys a user would press
    runMenu(keys, manager);
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Initializer list with a check: name_(std::move(name)), roll_(roll), marks_(checkedMarks(marks)) {}

    Member တစ်ခုချင်းကို တိုက်ရိုက် initialize လုပ်ပါတယ်။ std::move က name string ကို copy မကူးဘဲ လွှဲပေးလိုက်ပြီး object မပေါ်ခင်မှာ checkedMarks() က run လို့ value မှားရင် construction တစ်ခုလုံး ရပ်သွားပါတယ်။

  2. The one rulethrow std::invalid_argument("marks must be between 0 and 100");

    Daw ရဲ့ 120 က ဒီ line ကို ရောက်လာပါတယ်။ main() ထဲက catch block က message ကို print ထုတ်ပြီး manager.add() ကို လုံးဝ မခေါ်ဖြစ်ပါဘူး၊ ဒါကြောင့် နောက်ဆုံး count က 3 ပဲ ဖြစ်ပါတယ်။

  3. const getter returning a referenceconst std::string& getName() const { return name_; }

    ပထမ const က caller ကို နာမည်ကို ဖတ်ခွင့်ပဲ ပေးပါတယ်။ နောက်ဆုံးက const က ဒီ function က object ကို မပြင်ဘူးလို့ ကတိပေးတာပါ။ Reference ဖြစ်လို့ copy မကူးရပါဘူး။

  4. Private vector, public operationvoid add(const Student& student) { students_.push_back(student); }

    Student တွေကို manager ထဲ ထည့်နိုင်တဲ့ လမ်းက ဒီ function တစ်ခုတည်းပါ။ Part 2 မှာ roll number ထပ်နေတာကို ဒီနေရာမှာ ငြင်းဖို့ ထည့်ရုံပါပဲ၊ caller တွေ ဘာမှ ပြင်စရာ မလိုပါဘူး။

  5. Loop only while reads succeedwhile (in >> choice) {

    in >> choice က stream ကို return ပြန်ပြီး read အောင်မြင်ခဲ့ရင်ပဲ true ဖြစ်ပါတယ်။ Script ကုန်သွားရင် ဒါမှမဟုတ် စာလုံးတစ်လုံး ရောက်လာရင် loop က ရပ်ပြီး "Input ended before Exit." ကို ပြပါတယ်။ အဆုံးမရှိ ပတ်မနေတော့ပါဘူး။

  6. Scripted key pressesstd::istringstream keys("1 2 7 3"); // the keys a user would press

    istringstream က string ကို keyboard input လိုပဲ ဖတ်ပေးပါတယ်။ 7 က switch ရဲ့ default ကို ရောက်ပြီး 3 က Goodbye ကို ပြပြီး return ပြန်ပါတယ်။

You should see
Chit now has 70 marks
Rejected Daw: marks must be between 0 and 100
--- Student Manager ---
1. Add Student  2. Display All  3. Exit
> 1
Add Student: coming in Part 2
> 2
Display All: coming in Part 2
> 7
Invalid choice: 7
> 3
Goodbye! 3 student(s) in memory.
Menu loop တစ်ပတ်
  1. Stream ကနေ number တစ်ခု ဖတ်
  2. Echo ပြပြီး case ရွေး
  3. Case ကို run ပြီး နောက်တစ်ပတ် ပြန်ပတ်

Choice 3

Goodbye! ... then return

Read fail / input ကုန်

Loop stops: no endless spin

ဘာကြောင့် vector ကို main() ထဲ မထားတာလဲ

main() ထဲမှာ vector<Student> students ထားတာ အလုပ်တော့ လုပ်ပါတယ်။ ဒါပေမဲ့ function အသစ်တိုင်းက vector ကို parameter အဖြစ် လက်ခံရပြီး reference နဲ့ လက်ခံဖို့ မေ့သွားတဲ့ function တစ်ခုက copy ပေါ်မှာ အလုပ်လုပ်ပြီး ပြောင်းလဲမှုတွေ ပျောက်သွားပါလိမ့်မယ် (Part 2 မှာ ဒီ bug ကို တကယ် တွေ့ရပါမယ်)။ "roll number တွေ မထပ်ရ" လို list တစ်ခုလုံးနဲ့ ဆိုင်တဲ့ စည်းကမ်းကိုလည်း push_back ခေါ်တဲ့နေရာတိုင်းမှာ ထပ်ရေးရပါမယ်။

StudentManager ထဲ ထည့်လိုက်တော့ list ကို ပြောင်းနိုင်တဲ့ နေရာက class ရဲ့ member function တွေပဲ ဖြစ်သွားပါတယ်။ Student က record တစ်ခုချင်းရဲ့ စည်းကမ်းကို စောင့်သလို StudentManager က list တစ်ခုလုံးရဲ့ စည်းကမ်းကို စောင့်ပါတယ်။

လက်တွေ့ project တွေမှာ

လက်တွေ့ project တွေမှာ

School system၊ clinic app၊ ဆိုင်တစ်ဆိုင်ရဲ့ stock program စတဲ့ business software တိုင်းမှာ ဒီ pattern ကို တွေ့ရပါတယ်။ Domain object (Student၊ Patient၊ Product) က ကိုယ့်ရဲ့ စည်းကမ်းကို ကိုယ်တိုင် စောင့်ပြီး repository ဒါမှမဟုတ် service class တစ်ခုက object collection ကို ပိုင်ပါတယ်။ ဒီလို ခွဲထားတော့ UI က console menu ကနေ GUI ဒါမှမဟုတ် web API ကို ပြောင်းသွားရင်တောင် Student class ကို ပြင်စရာ မလိုပါဘူး။

Input ကို std::istream& အနေနဲ့ လက်ခံတာကလည်း လက်တွေ့ နည်းလမ်းတစ်ခုပါ။ Unit test တွေက istringstream နဲ့ command script ကို ထည့်ပြီး output ကို စစ်ပါတယ်၊ keyboard နဲ့ ထိုင်ရိုက်နေစရာ မလိုပါဘူး။ Command-line tool တွေကလည်း file ကနေ ဖတ်တာနဲ့ keyboard ကနေ ဖတ်တာကို code တစ်ခုတည်းနဲ့ ကိုင်တွယ်ဖို့ ဒီနည်းကို သုံးပါတယ်။ Validation ကို constructor ထဲ ထည့်ထားတော့ file၊ network၊ keyboard ဘယ်ကလာတဲ့ data မဆို တံခါးတစ်ပေါက်တည်းကို ဖြတ်ရပါတယ်။

အမှားများ

ဖြစ်လေ့ရှိတဲ့ အမှားများ

"အဆင်ပြေအောင်" data member ကို public ပြန်လုပ်တာ

Getter ရေးရတာ ပျင်းလို့ marks_ ကို public ထဲ ရွှေ့လိုက်ရင် s.marks_ = 120; လို line က compile ဖြစ်သွားပြီး invariant ပျက်သွားပါတယ်။ Private ထားရင် g++ က error: 'double Student::marks_' is private within this context လို့ ငြင်းပါတယ်။ ဒီ error က ကိုယ့်ကို ကာကွယ်နေတာပါ၊ ဖယ်ရှားရမယ့် အနှောင့်အယှက် မဟုတ်ပါဘူး။

Constructor body ထဲမှာ assign လုပ်ပြီး ဘာမှ မစစ်တာ

Student(std::string n, int r, double m) { name = n; roll = r; marks = m; } က member တွေကို default နဲ့ အရင်ဆောက်ပြီးမှ ထပ် assign လုပ်ပါတယ်၊ ပိုဆိုးတာက ဘာကိုမှ မစစ်ပါဘူး။ Initializer list ကို သုံးပြီး setter သုံးတဲ့ check တစ်ခုတည်းကိုပဲ constructor ကပါ ဖြတ်သွားအောင် လုပ်ပါ။ မဟုတ်ရင် setMarks(120) ကို ငြင်းပေမယ့် Student("Daw", 4, 120) ကို လက်ခံနေပါလိမ့်မယ်။

Read ကို မစစ်တဲ့ while (true) menu

std::cin >> choice fail ဖြစ်ပြီးနောက် stream က failed state ထဲ ရောက်နေပြီး နောက်ထပ် read တိုင်း ချက်ချင်း fail ဖြစ်ပါတယ်။ Loop condition ထဲမှာ read ကို ထည့်ပါ (while (in >> choice))။ Part 3 မှာ fail ဖြစ်ပြီးနောက် clear() နဲ့ ignore() သုံးပြီး ဆက်လုပ်နည်းကို ထည့်ပါမယ်။

Mini challenge

Model ကို ပိုခိုင်အောင် လုပ်ပြီး stats ထည့်ပါ

Lesson ထဲက Student နဲ့ StudentManager ကို ချဲ့ပါ။ Constructor က နာမည်အလွတ်နဲ့ 0 ဒါမှမဟုတ် အနုတ် roll number ကိုပါ ငြင်းရပါမယ်။ Student မှာ hasPassed() ထည့်ပြီး manager မှာ အောင်တဲ့သူ အရေအတွက်နဲ့ ပျမ်းမျှ marks ကို တွက်တဲ့ function နှစ်ခု ထည့်ပါ။

  • checkedName() နဲ့ checkedRoll() helper တွေက std::invalid_argument ကို ရှင်းလင်းတဲ့ message နဲ့ throw လုပ်ပါ
  • hasPassed() က marks >= 40 ဖြစ်ရင် true ပြန်ပါ (40 တိတိဆိုရင် အောင်ပါတယ်)
  • averageMarks() က student မရှိရင် 0 နဲ့ မစားဘဲ 0.0 ကို ပြန်ပေးပါ
  • main() က ငြင်းခံရတဲ့ student တစ်ယောက်ချင်းစီရဲ့ အကြောင်းရင်းကို print ထုတ်ပါ
challenge.cpp
// Start from the lesson's Student and StudentManager classes.

class Student {
public:
    Student(std::string name, int roll, double marks);  // TODO: check all three values
    bool hasPassed() const;  // TODO: true when marks >= 40
    // ... keep the getters and setMarks() from the lesson
private:
    static std::string checkedName(std::string name);  // TODO: throw if empty
    static int checkedRoll(int roll);                   // TODO: throw if <= 0
    static double checkedMarks(double marks);           // from the lesson
    std::string name_;
    int roll_;
    double marks_;
};

class StudentManager {
public:
    void add(const Student& student);
    std::size_t count() const;
    std::size_t countPassed() const;  // TODO: count students who passed
    double averageMarks() const;      // TODO: 0.0 when there are no students
private:
    std::vector<Student> students_;
};

int main() {
    // TODO: print the stats of an empty manager first
    // TODO: try to add ("Aye", 1, 78.5), ("", 2, 50), ("Bo", 0, 66),
    //       ("Chit", 3, 39.5) and ("Daw", 4, 40); print each rejection
    // TODO: print count, countPassed() and averageMarks() with 2 decimals
}

ရလဒ်: Manager အလွတ်အတွက် "0 students, average 0.00"၊ ပြီးရင် နာမည်အလွတ်နဲ့ roll 0 အတွက် rejection line နှစ်ကြောင်း၊ နောက်ဆုံးမှာ "Students: 3, passed: 2, average 52.67" ထွက်ရပါမယ် (39.5 က ကျပြီး 40 က အောင်ပါတယ်)။

အရိပ်အမြွက် ကြည့်ရန်

Initializer list ထဲမှာ name_(checkedName(std::move(name))) လို helper သုံးခုလုံးကို သုံးလို့ရပါတယ်။ Member တွေက class ထဲမှာ ကြေညာထားတဲ့ အစီအစဉ်အတိုင်း initialize ဖြစ်တာကို သတိထားပါ။ Average အတွက် total ကို double နဲ့ စုပြီး static_cast<double>(students_.size()) နဲ့ စားပါ။

အဖြေ ကြည့်ရန်
challenge.cpp
#include <format>
#include <iostream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>

class Student {
public:
    Student(std::string name, int roll, double marks)
        : name_(checkedName(std::move(name))),
          roll_(checkedRoll(roll)),
          marks_(checkedMarks(marks)) {}

    const std::string& getName() const { return name_; }
    int getRoll() const { return roll_; }
    double getMarks() const { return marks_; }
    bool hasPassed() const { return marks_ >= 40.0; }

    void setMarks(double marks) { marks_ = checkedMarks(marks); }

private:
    static std::string checkedName(std::string name) {
        if (name.empty()) {
            throw std::invalid_argument("name must not be empty");
        }
        return name;
    }
    static int checkedRoll(int roll) {
        if (roll <= 0) {
            throw std::invalid_argument("roll must be positive");
        }
        return roll;
    }
    static double checkedMarks(double marks) {
        if (marks < 0.0 || marks > 100.0) {
            throw std::invalid_argument("marks must be between 0 and 100");
        }
        return marks;
    }

    std::string name_;
    int roll_;
    double marks_;
};

class StudentManager {
public:
    void add(const Student& student) { students_.push_back(student); }
    std::size_t count() const { return students_.size(); }

    std::size_t countPassed() const {
        std::size_t passed = 0;
        for (const Student& s : students_) {
            if (s.hasPassed()) {
                ++passed;
            }
        }
        return passed;
    }

    double averageMarks() const {
        if (students_.empty()) {
            return 0.0;  // no students: avoid dividing by zero
        }
        double total = 0.0;
        for (const Student& s : students_) {
            total += s.getMarks();
        }
        return total / static_cast<double>(students_.size());
    }

private:
    std::vector<Student> students_;
};

void tryAdd(StudentManager& manager, const std::string& name, int roll, double marks) {
    try {
        manager.add(Student(name, roll, marks));
    } catch (const std::invalid_argument& e) {
        std::cout << std::format("Rejected '{}' (roll {}): {}\n", name, roll, e.what());
    }
}

int main() {
    StudentManager manager;
    std::cout << std::format("Empty: {} students, average {:.2f}\n",
                             manager.count(), manager.averageMarks());

    tryAdd(manager, "Aye", 1, 78.5);
    tryAdd(manager, "", 2, 50.0);
    tryAdd(manager, "Bo", 0, 66.0);
    tryAdd(manager, "Chit", 3, 39.5);
    tryAdd(manager, "Daw", 4, 40.0);

    std::cout << std::format("Students: {}, passed: {}, average {:.2f}\n",
                             manager.count(), manager.countPassed(),
                             manager.averageMarks());
}

Quick Quiz 1 / 3

Lesson ထဲက Student class နဲ့ Student s("Daw", 4, 120.0); လို့ ရေးရင် ဘာဖြစ်မလဲ။

Quick Quiz 2 / 3

getName() က std::string အစား const std::string& ကို ဘာကြောင့် return ပြန်တာလဲ။

Quick Quiz 3 / 3

runMenu ကို std::istringstream keys("1 x 3") နဲ့ run ရင် "> 1" နဲ့ placeholder line ပြီးနောက် ဘာ print ထုတ်မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Class က invariant ကို စောင့်ပါတယ်: data ကို private ထားပြီး ပြောင်းလဲမှုတိုင်းကို စစ်ပါ
  • Constructor နဲ့ setter နှစ်ခုလုံး check helper တစ်ခုတည်းကို ဖြတ်သွားရပါမယ်
  • Getter တွေကို const လုပ်ပြီး string လို ကြီးတဲ့ member ကို const reference နဲ့ ပြန်ပေးပါ
  • StudentManager က vector ကို ပိုင်ပြီး menu က ဘယ် std::istream ကမဆို ဖတ်ပါတယ်

The Current ISO C++ Standard (C++23) — Standard C++ Foundation

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Mini Project: Student Manager - Part 1 | Thuta Learning