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

Mini Project: Student Manager - Part 2

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

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

  • Function တစ်ခုက caller ရဲ့ object၊ read-only view ဒါမှမဟုတ် ကိုယ်ပိုင် copy ထဲက ဘာကို လိုသလဲ ဆုံးဖြတ်ပြီး parameter type ကို ရွေးနိုင်ဖို့
  • မအောင်မြင်နိုင်တဲ့ search ကို pointer-or-nullptr return နဲ့ ရေးပြီး ပထမဆုံး တွေ့တာနဲ့ return ပြန်နိုင်ဖို့
  • Member function တွေကို const မှတ်ပြီး const StudentManager& ပေါ်ကနေ ခေါ်လို့ရအောင် လုပ်နိုင်ဖို့
  • std::format ရဲ့ width specifier တွေနဲ့ column ညီတဲ့ table ကို print ထုတ်နိုင်ဖို့

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

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

Part 1 မှာ စာရင်းစာအုပ်ကို ပိုင်တဲ့ စာရေး (StudentManager) ရှိနေပြီ၊ ဒါပေမဲ့ သူ ဘာအလုပ်မှ မလုပ်တတ်သေးပါဘူး။ Part 2 မှာ သူ့ကို အလုပ်သုံးခု သင်ပေးပါမယ်။ ကျောင်းသားအသစ် စာရင်းသွင်းတာ၊ စာရင်းတစ်ခုလုံး ဖတ်ပြတာ၊ roll number နဲ့ ရှာပေးတာပါ။

စာရေးကို အလုပ်တစ်ခု ခိုင်းတိုင်း မူရင်းစာအုပ်ကိုပဲ ပေးရမလား၊ ကြည့်ရုံပဲ ကြည့်ခွင့်ပေးရမလား၊ photocopy တစ်စောင် ပေးရမလား ဆိုတာ ဆုံးဖြတ်ရပါတယ်။ Photocopy ပေါ်မှာ စာရင်းသွင်းရင် မူရင်းစာအုပ်ထဲ ဘယ်တော့မှ မရောက်ပါဘူး။ C++ မှာ ဒီရွေးချယ်မှု သုံးမျိုးက reference (&)၊ const reference (const &) နဲ့ by value ဖြစ်ပြီး ဒီ lesson ရဲ့ bug အများစုက မှားရွေးမိတာကနေ လာပါတယ်။

Problem → Why → Solution

ပြဿနာ: compile ဖြစ်ပေမယ့် မှားနေတဲ့ helper နှစ်ခု

အောက်က draft ထဲက function နှစ်ခုလုံး warning မရှိဘဲ compile ဖြစ်ပါတယ်။ addStudent() က "Student added!" လို့ ပြပေမယ့် နောက်တစ်ကြောင်းမှာ caller ရဲ့ vector size က 0 ပဲ ရှိနေပါတယ်။ Parameter မှာ & မပါလို့ function က vector ရဲ့ copy တစ်ခုကို ရပြီး အဲဒီ copy ထဲကို ထည့်လိုက်တာပါ၊ function ပြီးတာနဲ့ copy လည်း ပျောက်သွားပါတယ်။

searchByRoll() ကတော့ roll 2 ကို တွေ့ပြီး "Found: Bo" လို့ ပြပြီးမှ "Student not found." လို့ ထပ်ပြပါတယ်။ Match တွေ့တဲ့အခါ return မပြန်လို့ loop က ဆက်ပတ်ပြီး loop ပြီးတဲ့နောက်က line ကလည်း အမြဲ run နေလို့ပါ။ Search လုပ်တာနဲ့ print ထုတ်တာကို ရောထားတာ ဒီ bug ကို ဖြစ်လွယ်စေပါတယ်။

draft_helpers.cpp
// Draft helpers: both compile, both are wrong.
void addStudent(std::vector<Student> students, const Student& s) {
    students.push_back(s);  // pushes into a copy
    std::cout << "Student added!\n";
}

void searchByRoll(const std::vector<Student>& students, int roll) {
    for (const Student& s : students) {
        if (s.getRoll() == roll) {
            std::cout << "Found: " << s.getName() << '\n';
        }  // no return: the loop keeps going
    }
    std::cout << "Student not found.\n";
}

int main() {
    std::vector<Student> students;
    addStudent(students, Student("Aye", 1, 78.5));
    std::cout << students.size() << '\n';  // 0

    students.push_back(Student("Bo", 2, 91.0));
    searchByRoll(students, 2);  // prints Found AND not found
}

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

C++ က parameter တွေကို default အနေနဲ့ value နဲ့ pass လုပ်ပါတယ်၊ ဆိုလိုတာက function တိုင်းက ကိုယ်ပိုင် copy ကို ရပါတယ်။ Vector တစ်ခုကို copy ကူးတာက element တိုင်းကို copy ကူးတာမို့ နှေးတဲ့အပြင် ပြောင်းလဲမှုတွေကိုလည်း ပျောက်စေပါတယ်။ Search bug ကတော့ control flow ပြဿနာပါ။ "တွေ့ပြီ" ဆိုတဲ့ အချက်ကို return နဲ့ တန်းပြီး ပြန်မပေးရင် function က ဆက် run နေမှာပါ။

Core concept: မူရင်း၊ view ဒါမှမဟုတ် copy

Part 2 က placeholder တွေကို တကယ့် feature တွေအဖြစ် ပြောင်းပါတယ်။ သုံးခုလုံး StudentManager ထဲမှာ ရှိပြီး မေးခွန်းတစ်ခုတည်းပေါ် မူတည်ပါတယ်။ ဒီ code က မူရင်း object ကို လိုတာလား၊ read-only view လိုတာလား၊ ကိုယ်ပိုင် copy လိုတာလား။ C++ မှာ ဒါကို ကိုယ်တိုင် ရွေးပြောရပြီး အဲဒီရွေးချယ်မှုက မှန်ကန်မှုနဲ့ မြန်နှုန်း နှစ်ခုလုံးကို ဆုံးဖြတ်ပါတယ်။

Parameter ပုံစံFunction က ဘာရလဲဘယ်အချိန် သုံးမလဲ
Student sကိုယ်ပိုင် copyCopy ကို သိမ်းမယ် ဒါမှမဟုတ် ပြင်မယ်
Student& sCaller ရဲ့ object၊ ပြင်လို့ရCaller ရဲ့ object ကို ပြောင်းရမယ်
const Student& sCaller ရဲ့ object၊ ဖတ်ရုံသာဖတ်ရုံပဲ ဖတ်မယ် (အသုံးအများဆုံး)

add(): ထပ်နေတာကို ငြင်းပြီးမှ push_back

add() က argument ကို ဖတ်ရုံပဲ ဖတ်လို့ const Student& ကို လက်ခံပါတယ်။ push_back ကတော့ vector ထဲမှာ ကိုယ်ပိုင် copy တစ်ခုကို သိမ်းပါတယ်။ မသိမ်းခင် add() က findByRoll() ကို ခေါ်ပြီး roll number ကို သုံးပြီးသားဆိုရင် false ပြန်ပေးပါတယ်။ ဒါကြောင့် "roll number တွေ မထပ်ရ" ဆိုတဲ့ list စည်းကမ်းက နေရာတစ်ခုတည်းမှာပဲ ရှိပါတယ်။ bool return က ပြဿနာကို ဘယ်လို ပြောပြမလဲဆိုတာကို caller ကို ဆုံးဖြတ်ခွင့်ပေးပါတယ်။

findByRoll(): pointer ဒါမှမဟုတ် nullptr ပြန်ပေး

Search က မတွေ့နိုင်တာမို့ "ဘာမှ မတွေ့ဘူး" လို့ ပြောနိုင်တဲ့ နည်းလမ်း လိုပါတယ်။ const Student* ကို return ပြန်တာက ဒါကို လုပ်ပေးပါတယ်၊ ကိုက်တဲ့ element ရဲ့ address ဒါမှမဟုတ် nullptr ပါ။ Loop က match တွေ့တာနဲ့ ချက်ချင်း return ပြန်လို့ ဆက်မရှာတော့သလို နှစ်ခါလည်း မပြောတော့ပါဘူး။ Function က ဖတ်ရုံပဲ ဖတ်လို့ const လို့ မှတ်ထားပြီး pointer ကလည်း const ကို ညွှန်တာမို့ caller က အဲဒီ pointer ကနေ student ကို ပြင်လို့ မရပါဘူး။

displayAll(): const ဖြစ်ပြီး empty case ကို အရင်စစ်

displayAll() က list အလွတ်ကို အရင်ကိုင်တွယ်ပြီး စောစော return ပြန်ပါတယ်။ ပြီးမှ header တစ်ကြောင်းနဲ့ std::format သုံးပြီး student တစ်ယောက်ကို row တစ်ကြောင်းစီ ပြပါတယ်။ {:>4} နဲ့ {:<8} လို width specifier တွေက column တွေကို ညီစေပြီး {:>6.1f} က marks ကို ဒဿမ တစ်နေရာနဲ့ ပြပါတယ်။

Input အတွက် free function တွေ

addFromInput() နဲ့ searchFromInput() က command တစ်ခုအတွက် value တွေကို stream ကနေ ဖတ်ပြီး manager ကို ခေါ်ပါတယ်။ Manager ကို ပြောင်းမလား မပြောင်းဘူးလား ပေါ်မူတည်ပြီး StudentManager& ဒါမှမဟုတ် const StudentManager& ကို လက်ခံပါတယ်။ searchFromInput က const member function တွေကိုပဲ ခေါ်လို့ရတာမို့ findByRoll က const ဖြစ်ကိုဖြစ်ရပါမယ်။

Mental model: မူရင်း၊ view လား copy လား
  1. Caller ရဲ့ object ကို ပြောင်းမှာလား? & သုံး
  2. ဖတ်ရုံပဲလား? const & သုံး
  3. မတွေ့နိုင်တဲ့ search လား? pointer/nullptr
  4. တွေ့ပြီလား? ချက်ချင်း return

ဖတ်ရုံပဲ ဖတ်မယ့် object တွေအတွက် const & ကို default အဖြစ် သုံးပါ။ Function က မူရင်းကို တကယ်ပြောင်းရမှသာ const ကို ဖြုတ်ပါ။

အခြေခံ ပုံစံ

cpp
class Manager {
public:
    bool add(const Item& item);       // reads the argument, stores a copy
    const Item* find(Key key) const;  // nullptr when nothing matches
    void show() const;                // read-only member function
private:
    std::vector<Item> items_;
};

void readCommand(std::istream& in, Manager& manager);       // may change it
void reportOnly(std::istream& in, const Manager& manager);  // only reads it

List ကို ပြောင်းတဲ့ operation တွေက non-const ဖြစ်ပြီး ဖတ်ရုံပဲ ဖတ်တဲ့ operation တွေက const ဖြစ်ပါတယ်။ မတွေ့နိုင်တဲ့ search က pointer ပြန်ပေးပြီး Manager ကို လက်ခံတဲ့ function တွေက & ဒါမှမဟုတ် const & ကို ရည်ရွယ်ချက်ရှိရှိ ရွေးပါတယ်။

Part 2 code: add၊ display နဲ့ search

Student class က Part 1 အတိုင်းပါပဲ။ StudentManager မှာ duplicate စစ်တဲ့ add()၊ findByRoll() နဲ့ displayAll() တိုးလာပြီး menu မှာ search အတွက် option 4 တိုးလာပါတယ်။ Script က list အလွတ်ကို အရင်ပြခိုင်းတယ်၊ student သုံးယောက် ထည့်တယ် (တတိယတစ်ယောက်က roll 1 ကို ထပ်သုံးတယ်)၊ list ကို ပြတယ်၊ ပြီးရင် ရှိတဲ့ roll တစ်ခုနဲ့ မရှိတဲ့ roll တစ်ခုကို ရှာပါတယ်။ Option 1 ရဲ့ နောက်က "Aye 1 78.5" လို value တွေကို addFromInput() က ဖတ်ပါတယ်။

student_manager_part2.cpp
#include <format>
#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:
    bool add(const Student& student) {
        if (findByRoll(student.getRoll()) != nullptr) {
            return false;  // roll numbers must stay unique
        }
        students_.push_back(student);
        return true;
    }

    const Student* findByRoll(int roll) const {
        for (const Student& s : students_) {
            if (s.getRoll() == roll) {
                return &s;  // first match: stop searching
            }
        }
        return nullptr;
    }

    void displayAll() const {
        if (students_.empty()) {
            std::cout << "No students yet.\n";
            return;
        }
        std::cout << "Roll  Name     Marks\n";
        for (const Student& s : students_) {
            std::cout << std::format("{:>4}  {:<8}{:>6.1f}\n",
                                     s.getRoll(), s.getName(), s.getMarks());
        }
    }

    std::size_t count() const { return students_.size(); }

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

void addFromInput(std::istream& in, StudentManager& manager) {
    std::string name;
    int roll = 0;
    double marks = 0.0;
    in >> name >> roll >> marks;
    if (manager.add(Student(name, roll, marks))) {
        std::cout << "Added " << name << '\n';
    } else {
        std::cout << "Roll " << roll << " already exists\n";
    }
}

void searchFromInput(std::istream& in, const StudentManager& manager) {
    int roll = 0;
    in >> roll;
    if (const Student* found = manager.findByRoll(roll)) {
        std::cout << std::format("Found: {} ({:.1f})\n",
                                 found->getName(), found->getMarks());
    } else {
        std::cout << "Roll " << roll << " not found\n";
    }
}

void runMenu(std::istream& in, StudentManager& manager) {
    int choice = 0;
    while (in >> choice) {
        std::cout << "> " << choice << '\n';
        switch (choice) {
        case 1: addFromInput(in, manager); break;
        case 2: manager.displayAll(); break;
        case 3: std::cout << "Goodbye!\n"; return;
        case 4: searchFromInput(in, manager); break;
        default: std::cout << "Invalid choice: " << choice << '\n';
        }
    }
}

int main() {
    StudentManager manager;
    std::istringstream keys(
        "2\n"
        "1 Aye 1 78.5\n"
        "1 Bo 2 91\n"
        "1 Chit 1 55\n"
        "2\n"
        "4 2\n"
        "4 9\n"
        "3\n");
    runMenu(keys, manager);
}

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

  1. The list rule in one placeif (findByRoll(student.getRoll()) != nullptr) {

    Chit ရဲ့ roll 1 က ဒီ check မှာ ဖမ်းမိပြီး add() က false ပြန်ပေးလို့ "Roll 1 already exists" ထွက်ပါတယ်။ Vector ထဲ ဘာမှ မထည့်ပါဘူး။

  2. Read-only loop variablefor (const Student& s : students_) {

    const Student& က element တစ်ခုချင်းကို copy မကူးဘဲ reference နဲ့ ပေးပြီး const က loop ကို element ကို မပြင်အောင် တားပါတယ်။

  3. Stop at the first matchreturn &s; // first match: stop searching

    &s က vector ထဲက element ရဲ့ address ပါ။ return က function ကို ချက်ချင်း ထွက်စေလို့ nullptr ကို ပြန်ပေးတဲ့ နောက်ဆုံး line ကို match တစ်ခုမှ မတွေ့မှသာ ရောက်ပါတယ်။

  4. Aligned columnsstd::cout << std::format("{:>4} {:<8}{:>6.1f}\n",

    Roll ကို width 4 ညာဘက်ကပ်၊ နာမည်ကို width 8 ဘယ်ဘက်ကပ်၊ marks ကို width 6 နဲ့ ဒဿမ တစ်နေရာ ပြပါတယ်။ ဒါကြောင့် 91 က 91.0 ဖြစ်ပြီး column တွေ ညီနေပါတယ်။

  5. Pointer declared in the ifif (const Student* found = manager.findByRoll(roll)) {

    found ကို if condition ထဲမှာ ကြေညာလို့ nullptr မဟုတ်ရင် true ဖြစ်ပါတယ်။ found ရဲ့ scope က if/else အတွင်းမှာပဲ ရှိလို့ ပြောင်းသွားနိုင်တဲ့ vector ထဲကို pointer ကို အကြာကြီး ကိုင်ထားဖို့ မဖြစ်နိုင်ပါဘူး။

You should see
> 2
No students yet.
> 1
Added Aye
> 1
Added Bo
> 1
Roll 1 already exists
> 2
Roll  Name     Marks
   1  Aye       78.5
   2  Bo        91.0
> 4
Found: Bo (91.0)
> 4
Roll 9 not found
> 3
Goodbye!

Vector ထဲကို ညွှန်တဲ့ pointer က အမြဲ မခံပါဘူး

findByRoll() က students_ ထဲက element တစ်ခုရဲ့ address ကို ပြန်ပေးပါတယ်။ ဒီ address က vector မပြောင်းမချင်းပဲ မှန်ပါတယ်။ push_back က နေရာ ထပ်လိုတဲ့အခါ vector က ပိုကြီးတဲ့ memory block အသစ်ယူပြီး element အားလုံးကို အဲဒီကို ရွှေ့ကာ block အဟောင်းကို free လုပ်ပါတယ်။ ဒါကြောင့် အရင်က သိမ်းထားတဲ့ pointer က free လုပ်ပြီးသား memory ကို ညွှန်နေပါတော့တယ်။ အဲဒီ pointer ကနေ ဖတ်တာက undefined behaviour ဖြစ်ပြီး နာမည်မှန်ထွက်နိုင်သလို အမှိုက်ထွက်တာ ဒါမှမဟုတ် crash ဖြစ်တာလည်း ဖြစ်နိုင်ပါတယ်။

ဒီ project အတွက် စည်းကမ်းကတော့ searchFromInput() လုပ်သလို pointer ကို ချက်ချင်းသုံးပြီး add() ဒါမှမဟုတ် remove တစ်ခုကို ကျော်ပြီး ဘယ်တော့မှ မသိမ်းထားဖို့ပါ။ Caller က ရလဒ်ကို တကယ် သိမ်းထားဖို့ လိုရင် copy ကို ပြန်ပေးပါ၊ ဥပမာ Student တစ်ယောက် ဒါမှမဟုတ် ဘာမှမရှိတာကို ကိုင်ထားနိုင်တဲ့ std::optional<Student> (C++17) ပါ။

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

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

တကယ့် app တိုင်းမှာ ဒီ operation သုံးခု ရှိပါတယ်၊ record ထည့်တာ၊ list ပြတာ၊ ID နဲ့ ရှာတာပါ။ add() ထဲက duplicate check က database ရဲ့ UNIQUE constraint နဲ့ အတူတူပါပဲ။ ID ထပ်နေတဲ့ row ကို database ကိုယ်တိုင် ငြင်းပြီး app က အဲဒီ error ကို user နားလည်တဲ့ message အဖြစ် ပြောင်းပေးပါတယ်။ "မတွေ့ဘူး" ကို exception အစား nullptr၊ std::optional ဒါမှမဟုတ် web API မှာဆိုရင် 404 response နဲ့ ပြန်ပေးတာလည်း ပုံမှန်ပါပဲ၊ မတွေ့တာက ပုံမှန်ဖြစ်တတ်တဲ့ ရလဒ်တစ်ခုမို့လို့ပါ။

Const-correctness ကလည်း team အလုပ်မှာ အရေးကြီးပါတယ်။ Report ထုတ်တဲ့ module က const StudentManager& ကိုပဲ လက်ခံရင် reviewer က အဲဒီ module က data ကို မပြင်နိုင်ဘူးဆိုတာ code တစ်ကြောင်းချင်း မဖတ်ဘဲ သိနိုင်ပါတယ်၊ compiler က အာမခံပေးထားလို့ပါ။

အမှားများ

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

Vector ကို value နဲ့ pass လုပ်တာ

void addStudent(std::vector<Student> students, ...) က copy ထဲကို ထည့်တာမို့ caller ရဲ့ list မပြောင်းပါဘူး။ ဖတ်ရုံသာ ဖတ်တဲ့ function မှာဆိုရင်တော့ ရလဒ်မှန်ပေမယ့် ခေါ်တိုင်း student အားလုံးကို copy ကူးနေရပါတယ်။ ပြောင်းရမယ်ဆိုရင် &၊ ဖတ်ရုံဆိုရင် const & ကို သုံးပါ။

Match တွေ့ပြီး return မပြန်တာ

Loop ထဲမှာ "Found" ကို print ထုတ်ပြီး return မပြန်ရင် loop က ဆက်ပတ်ပြီး loop အပြီးက "not found" line လည်း run ပါတယ်။ Search function က ရလဒ်ကိုပဲ ပြန်ပေးပြီး print ထုတ်တာကို caller ကို ပေးလုပ်ရင် ဒီ bug ဖြစ်ဖို့ ခက်သွားပါတယ်။

Read-only member function မှာ const မေ့တာ

displayAll() ကို const မပါဘဲ ရေးပြီး const StudentManager& ကနေ ခေါ်ရင် g++ က error: passing 'const StudentManager' as 'this' argument discards qualifiers လို့ ပြပါတယ်။ Caller ရဲ့ const ကို ဖြုတ်တာက မှားတဲ့ဖြေရှင်းနည်းပါ၊ member function ကို const ထည့်ပေးပါ။

findByRoll ရဲ့ pointer ကို သိမ်းထားတာ

Pointer ကို variable တစ်ခုထဲ ထည့်ထားပြီး student အသစ်တွေ ထည့်ပြီးမှ သုံးရင် vector က reallocate လုပ်ပြီးသွားနိုင်လို့ dangling pointer ဖြစ်နေပါမယ်။ ချက်ချင်းသုံးပါ ဒါမှမဟုတ် copy ကို သိမ်းပါ။

Mini challenge

နာမည်နဲ့ရှာတာနဲ့ ဖျက်တာ ထည့်ပါ

StudentManager ကို operation နှစ်ခု ထပ်ထည့်ပါ။ နာမည်တွေက ထပ်နိုင်လို့ findByName() က ကိုက်တဲ့သူ အားလုံးကို copy အဖြစ် ပြန်ပေးရပါမယ်။ removeByRoll() က roll တစ်ခုနဲ့ student ကို ဖျက်ပြီး ဖျက်စရာ ရှိခဲ့လား မရှိခဲ့ဘူးလား ပြောပြရပါမယ်။ နှစ်ခုလုံးကို menu option 6 နဲ့ 7 မှာ ချိတ်ပါ (5 ကို Part 3 မှာ sort အတွက် သုံးမှာမို့ ကျော်ထားပါတယ်)။

  • findByName() က const member function ဖြစ်ပြီး std::vector<Student> ကို return ပြန်ပါ (match မရှိရင် vector အလွတ်)
  • removeByRoll() က ဖျက်လိုက်ရင် true၊ အဲဒီ roll မရှိရင် false ပြန်ပေးပါ
  • Roll တစ်ခုတည်းကို နှစ်ခါ ဖျက်ကြည့်ပြီး ဒုတိယအကြိမ်မှာ "not found" ထွက်ကြောင်း ပြပါ
  • Match နှစ်ခုရှိတဲ့ နာမည်တစ်ခုနဲ့ match မရှိတဲ့ နာမည်တစ်ခုကို စမ်းပါ
challenge.cpp
// Student is unchanged. Add two operations to the lesson's StudentManager.

class StudentManager {
public:
    // ... add(), findByRoll(), displayAll() and count() from the lesson

    // TODO: return every student whose name equals `name`, as copies.
    //       An empty vector means "no match".
    std::vector<Student> findByName(const std::string& name) const;

    // TODO: erase the student with this roll; return false if there was none.
    bool removeByRoll(int roll);

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

void runMenu(std::istream& in, StudentManager& manager) {
    // TODO: case 6 reads a name, prints "> 6 <name>: N match(es)",
    //       then one "  roll R (M)" line per match
    // TODO: case 7 reads a roll, prints "> 7 <roll>: removed" or "... not found"
    // TODO: case 3 prints how many students are left and returns
}

int main() {
    // TODO: add Aye (1, 78.5), Bo (2, 91) and a second Aye (3, 64), then run
    //       the keys "6 Aye\n6 Zaw\n7 2\n7 2\n6 Bo\n3\n"
}

ရလဒ်: "Aye" အတွက် match 2 ခု (roll 1 နဲ့ 3)၊ "Zaw" အတွက် 0၊ roll 2 ကို ပထမအကြိမ် "removed"၊ ဒုတိယအကြိမ် "not found"၊ Bo ကို ဖျက်ပြီးနောက် "Bo" အတွက် 0 ထွက်ပြီး နောက်ဆုံးမှာ student 2 ယောက် ကျန်ပါမယ်။

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

findByName() အတွက် vector အလွတ်တစ်ခု ဖန်တီးပြီး ကိုက်တဲ့ student တွေကို push_back လုပ်ပါ။ removeByRoll() အတွက် C++20 ရဲ့ std::erase_if(students_, lambda) ကို သုံးနိုင်ပါတယ်၊ သူက ဖျက်လိုက်တဲ့ element အရေအတွက်ကို ပြန်ပေးလို့ > 0 နဲ့ စစ်ရုံပါပဲ။

အဖြေ ကြည့်ရန်
challenge.cpp
#include <format>
#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:
    bool add(const Student& student) {
        for (const Student& s : students_) {
            if (s.getRoll() == student.getRoll()) {
                return false;
            }
        }
        students_.push_back(student);
        return true;
    }

    // Names are not unique, so return every match as a copy (safe to keep).
    std::vector<Student> findByName(const std::string& name) const {
        std::vector<Student> matches;
        for (const Student& s : students_) {
            if (s.getName() == name) {
                matches.push_back(s);
            }
        }
        return matches;
    }

    bool removeByRoll(int roll) {
        auto removed = std::erase_if(students_, [roll](const Student& s) {
            return s.getRoll() == roll;
        });
        return removed > 0;
    }

    std::size_t count() const { return students_.size(); }

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

void runMenu(std::istream& in, StudentManager& manager) {
    int choice = 0;
    while (in >> choice) {
        switch (choice) {
        case 6: {
            std::string name;
            in >> name;
            const std::vector<Student> matches = manager.findByName(name);
            std::cout << std::format("> 6 {}: {} match(es)\n", name, matches.size());
            for (const Student& s : matches) {
                std::cout << std::format("  roll {} ({:.1f})\n",
                                         s.getRoll(), s.getMarks());
            }
            break;
        }
        case 7: {
            int roll = 0;
            in >> roll;
            const bool removed = manager.removeByRoll(roll);
            std::cout << std::format("> 7 {}: {}\n", roll,
                                     removed ? "removed" : "not found");
            break;
        }
        case 3:
            std::cout << "Goodbye! " << manager.count() << " student(s) left.\n";
            return;
        default:
            std::cout << "Invalid choice: " << choice << '\n';
        }
    }
}

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

    std::istringstream keys("6 Aye\n6 Zaw\n7 2\n7 2\n6 Bo\n3\n");
    runMenu(keys, manager);
}

Quick Quiz 1 / 3

void addStudent(std::vector<Student> students, const Student& s) { students.push_back(s); } ကို vector အလွတ်နဲ့ ခေါ်ပြီးနောက် caller ရဲ့ size က ဘယ်လောက်လဲ။

Quick Quiz 2 / 3

Script "1 Aye 1 78.5\n1 Bo 1 60\n4 1\n" ကို lesson program နဲ့ run ရင် option 4 က ဘာ print ထုတ်မလဲ။

Quick Quiz 3 / 3

const Student* p = manager.findByRoll(1); ပြီးနောက် student ၃ ယောက် ထပ်ထည့်ပြီးမှ p->getName() ကို ဖတ်ရင် ဘာ risk ရှိလဲ။

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

  • ပြောင်းရမယ့် object အတွက် &၊ ဖတ်ရုံသာဆိုရင် const &၊ copy တကယ်လိုမှ value နဲ့ pass လုပ်ပါ
  • add() ထဲမှာ duplicate roll ကို စစ်လို့ list စည်းကမ်းက နေရာတစ်ခုတည်းမှာ ရှိပါတယ်
  • မတွေ့နိုင်တဲ့ search က pointer ဒါမှမဟုတ် nullptr ပြန်ပေးပြီး ပထမ match မှာ return ပြန်ပါတယ်
  • Read-only member function တွေကို const မှတ်ပြီး vector element pointer ကို ကြာကြာ မသိမ်းပါနဲ့

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

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

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

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

Mini Project: Student Manager - Part 2 | Thuta Learning