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

Classes & Objects

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

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

  • Data member နဲ့ member function ပါတဲ့ class တစ်ခု သတ်မှတ်ပြီး object အများအပြား create လုပ်နိုင်ဖို့
  • Object တစ်ခုစီမှာ ကိုယ်ပိုင် data ရှိပြီး member function ထဲက နာမည်တွေက this ကနေ ခေါ်တဲ့ object ကို ညွှန်းကြောင်း ရှင်းပြနိုင်ဖို့
  • class နဲ့ struct ရဲ့ default access ကွာခြားချက်ကို ပြောနိုင်ဖို့
  • Default member initializer နဲ့ const member function တွေကို သုံးပြီး ပိုလုံခြုံတဲ့ class ရေးနိုင်ဖို့

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

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

Class ဆိုတာ အိမ်ဆောက်ဖို့ ပုံစံကြမ်း (blueprint) လိုပါပဲ။ ပုံစံကြမ်းပေါ်မှာ အခန်းဘယ်နှခန်း ပါမလဲ၊ တံခါး ဘယ်လို ဖွင့်မလဲ ရေးထားပေမယ့် အဲဒီစာရွက်ထဲမှာ ဘယ်သူမှ နေလို့ မရပါဘူး။ Object ကတော့ အဲဒီ ပုံစံကြမ်းနဲ့ တကယ် ဆောက်ထားတဲ့ အိမ်ပါ — memory ထဲမှာ နေရာယူပြီး ကိုယ်ပိုင် data ရှိပါတယ်။

Blueprint တစ်ခုတည်းကနေ အိမ် အများကြီး ဆောက်လို့ ရသလို class တစ်ခုကနေ object အများကြီး create လုပ်လို့ ရပါတယ်။ Class ထဲမှာ object တစ်ခုစီ မှတ်ထားရမယ့် data (data member) နဲ့ အဲဒီ data ပေါ်မှာ လုပ်ဆောင်တဲ့ function (member function) နှစ်မျိုးကို အတူတူ ရေးပါတယ်။ ဒါကြောင့် data နဲ့ အဲဒီ data ကို ကိုင်တွယ်တဲ့ code က နေရာတစ်ခုတည်းမှာ ရှိနေပါတယ်။

Problem → Why → Solution

ပြဿနာ: ဆက်စပ်နေတဲ့ variable တွေ ပြန့်ကျဲနေတာ

သင်တန်း နှစ်ခုရဲ့ နာမည်နဲ့ သင်ခန်းစာ အရေအတွက်ကို class မသုံးဘဲ သိမ်းကြည့်ရအောင်။ သင်တန်းတစ်ခုစီအတွက် variable နှစ်ခုစီ လိုပြီး function ဆီ ပို့တိုင်း မှန်ကန်တဲ့ အတွဲကို လက်နဲ့ ရွေးပေးရပါတယ်။ အောက်က code က compile ဖြစ်ပြီး run လည်း ရပါတယ်၊ ဒါပေမယ့် Python သင်တန်းမှာ C++ ရဲ့ သင်ခန်းစာ အရေအတွက်ကို ပြနေပါတယ်။

courses.cpp
#include <iostream>
#include <string>

void showInfo(std::string title, int lessons) {
    std::cout << title << " has " << lessons << " lessons.\n";
}

int main() {
    std::string title1 = "C++ Foundation";
    int lessons1 = 20;
    std::string title2 = "Python Basics";
    int lessons2 = 12;

    showInfo(title1, lessons1);
    showInfo(title2, lessons1);   // wrong pair: nothing stops this
}

ဘာကြောင့် ဒီ bug က လွယ်လွယ် ဖြစ်တာလဲ

Compiler အမြင်မှာ title2 နဲ့ lessons1 က သီးခြား variable နှစ်ခုသာ ဖြစ်ပြီး တစ်ခုနဲ့တစ်ခု ဆက်စပ်ကြောင်း မသိပါဘူး။ သင်တန်း ဆယ်ခု၊ field ငါးခု ဖြစ်လာရင် variable ငါးဆယ် ဖြစ်ပြီး အတွဲမှားဖို့ အခွင့်အလမ်းလည်း များလာပါတယ်။ Class တစ်ခုက ဒီ field တွေကို object တစ်ခုတည်းထဲ ချည်ထားပေးလို့ course.showInfo() လို့ ခေါ်လိုက်တာနဲ့ အမြဲ မှန်ကန်တဲ့ data ကိုပဲ သုံးပါတယ်။

Core concept: class က type အသစ်၊ object က အဲဒီ type ရဲ့ value

class Course { ... }; လို့ ရေးလိုက်တာက int၊ std::string တို့လို type အသစ်တစ်ခု ထပ်ထည့်လိုက်တာပါ။ Definition ကိုယ်တိုင်က memory မယူပါဘူး။ Course cppCourse; လို့ ရေးမှ object တစ်ခု ပေါ်လာပြီး အဲဒီ object ထဲမှာ std::string တစ်ခုနဲ့ int တစ်ခုစာ နေရာ ရှိလာပါတယ်။ Closing brace နောက်က semicolon ကိုလည်း မမေ့ပါနဲ့ — class definition က declaration တစ်ခုမို့ ; နဲ့ အဆုံးသတ်ရပါတယ်။

Class ထဲက member တွေ နှစ်မျိုး ရှိပါတယ်။ Data member (title, lessons) တွေက object တစ်ခုစီရဲ့ state ပါ၊ object တိုင်းမှာ ကိုယ်ပိုင် copy ရှိပါတယ်။ Member function (showInfo) တွေကတော့ behavior ပါ၊ code က object အားလုံး မျှသုံးပြီး object တစ်ခုစီထဲ ထပ်ပွားမနေပါဘူး။ cppCourse.showInfo() လို့ ခေါ်တဲ့အခါ compiler က hidden pointer this ကို &cppCourse အဖြစ် ပို့ပေးလို့ function ထဲက title ဆိုတာ this->title၊ ဆိုလိုတာက cppCourse ရဲ့ title ဖြစ်သွားပါတယ်။

Member အမျိုးအစားဘာကို ကိုယ်စားပြုလဲObject တစ်ခုစီမှာ
Data member (title)Object ရဲ့ stateကိုယ်ပိုင် copy တစ်ခုစီ
Member function (showInfo)Object လုပ်နိုင်တဲ့ အလုပ်Code တစ်စုတည်းကို မျှသုံး

class နဲ့ struct က C++ မှာ နီးပါး တူပါတယ်။ Language အရ ကွာတာက default access တစ်ခုပါ — class ထဲမှာ label မရေးရင် member တွေက private ဖြစ်ပြီး struct ထဲမှာဆို public ဖြစ်ပါတယ် (base class တွေရဲ့ default access မှာလည်း ဒီအတိုင်းပါပဲ)။ struct မှာလည်း member function၊ constructor တွေ ရေးလို့ ရပါတယ်။ အလေ့အထအရ စည်းမျဉ်း မရှိတဲ့ data အစုအဝေး သက်သက်ကို struct နဲ့ ရေးပြီး ထိန်းထားရမယ့် စည်းမျဉ်း (invariant) ရှိတဲ့ type တွေကို class နဲ့ ရေးကြပါတယ်။

int၊ double လို built-in type member တွေကို initializer မပေးထားရင် Course c; လို့ create လုပ်တဲ့အခါ အဲဒီ member တွေမှာ indeterminate value (ကျပန်း အမှိုက်) ရှိနေပါတယ်။ အဲဒါကို ဖတ်တာက undefined behavior ပါ။ ဒါကြောင့် int lessons = 0; လို default member initializer ပေးထားတာ အကောင်းဆုံးပါ — object ဘယ်လိုပဲ create လုပ်လုပ် စတင်ချိန်ကတည်းက အဓိပ္ပာယ်ရှိတဲ့ value ရှိနေပါမယ်။

မှတ်ထားရမယ့် ပုံစံ: blueprint တစ်ခု၊ object အများ
  1. Class က type ပုံစံကို ဖော်ပြ
  2. Object တစ်ခုစီက data ကိုယ်ပိုင် ရ
  3. obj.f() က this = &obj ပို့
  4. f() ထဲက member နာမည် = obj ရဲ့ member

Data က object တစ်ခုစီမှာ သီးသန့် ရှိပြီး member function code ကိုတော့ object အားလုံး မျှသုံးပါတယ်။

အထွေထွေ ပုံစံ

cpp
class ClassName {
public:
    Type dataMember = initialValue;          // state, one copy per object

    ReturnType memberFunction(Param p) const { // behavior, shared code
        return /* something using dataMember */;
    }
};                                           // the semicolon is required

ClassName objectName;                        // create an object
objectName.memberFunction(argument);         // call a member with .

public: label နောက်က member တွေကို class အပြင်ကနေ သုံးလို့ ရပါတယ်။ Data member ကို default value နဲ့ initialize လုပ်ပြီး object ကို မပြောင်းတဲ့ member function တွေကို const လို့ မှတ်ပါ။ Object create လုပ်ပြီးရင် objectName.member နဲ့ data ကိုရော function ကိုပါ ခေါ်ပါတယ်။

Example: Course class နဲ့ object တစ်ခု

ဒီ example မှာ Course type ကို သတ်မှတ်ပြီး main() ထဲမှာ cppCourse ဆိုတဲ့ object တစ်ခု create လုပ်ပါတယ်။ Field တွေကို dot operator နဲ့ ဖြည့်ပြီး showInfo() ကို ခေါ်တဲ့အခါ function က ဘယ် object ရဲ့ data ကို ဖတ်နေလဲ ဆိုတာကို သတိထားကြည့်ပါ။

cpp
#include <iostream>
#include <string>
using namespace std;

class Course {
  public:
    string title;
    int lessons;

    void showInfo() {
        cout << title << " has " << lessons << " lessons.";
    }
};

int main() {
    Course cppCourse;
    cppCourse.title = "C++ Foundation";
    cppCourse.lessons = 20;

    cppCourse.showInfo();
    return 0;
}

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

  1. Line 5class Course {

    Course ဆိုတဲ့ type အသစ်ကို စတင် သတ်မှတ်ပါတယ်။ ဒီ line ကြောင့် memory ထဲမှာ ဘာမှ မပေါ်လာသေးပါဘူး၊ ပုံစံကြမ်းပဲ ရှိပါသေးတယ်။

  2. Line 6public:

    ဒီ label နောက်က member တွေကို main() ကနေ သုံးခွင့် ရပါတယ်။ class ရဲ့ default က private ဖြစ်လို့ ဒီ line မပါရင် cppCourse.title ဆိုတာ compile error ဖြစ်ပါတယ်။

  3. Line 8int lessons;

    Initializer မပါလို့ main() က 20 ကို assign မလုပ်ခင်အထိ lessons ထဲမှာ indeterminate value ရှိနေပါတယ်။ int lessons = 0; လို့ ရေးထားရင် ပိုလုံခြုံပါတယ်။

  4. Line 10void showInfo() {

    Member function ပါ။ အထဲက title နဲ့ lessons က this->title၊ this->lessons ကို ဆိုလိုပြီး function ကို ခေါ်တဲ့ object ရဲ့ data ကို ဖတ်ပါတယ်။ Object ကို မပြောင်းလို့ void showInfo() const လို့ ရေးရင် ပိုကောင်းပါတယ်။

  5. Line 16Course cppCourse;

    Object တကယ် ပေါ်လာတဲ့ နေရာပါ။ title က empty string အဖြစ် construct ဖြစ်ပြီး lessons ကတော့ မသတ်မှတ်ရသေးပါဘူး။

  6. Line 20cppCourse.showInfo();

    Dot operator နဲ့ member function ကို ခေါ်ပါတယ်။ ဒီ call အတွက် this က &cppCourse ဖြစ်လို့ "C++ Foundation has 20 lessons." ကို print ထုတ်ပါတယ်။

You should see
C++ Foundation has 20 lessons.

Object တစ်ခုစီမှာ ကိုယ်ပိုင် data ရှိတယ်

အောက်က program က Course object နှစ်ခု create လုပ်ပြီး တစ်ခုကို နောက်တစ်ခုထဲ copy ကူးပါတယ်။ Object တစ်ခုကို assign လုပ်ရင် data member တိုင်းကို copy ကူးတာမို့ copy ရဲ့ lessons ကို ပြောင်းလိုက်တာက မူလ object ကို မထိခိုက်ပါဘူး။ Default member initializer ကြောင့် draft object ကလည်း 0 lessons နဲ့ အန္တရာယ်ကင်းကင်း စတင်ပါတယ်။

objects.cpp
#include <iostream>
#include <string>

class Course {
public:
    std::string title = "Untitled";
    int lessons = 0;

    void showInfo() const {
        std::cout << title << " has " << lessons << " lessons.\n";
    }
};

int main() {
    Course draft;                 // uses the default member initializers
    Course cpp;
    cpp.title = "C++ Foundation";
    cpp.lessons = 20;

    Course copy = cpp;            // copies title and lessons
    copy.lessons = 35;            // changes only the copy

    draft.showInfo();
    cpp.showInfo();
    copy.showInfo();
}

Output က "Untitled has 0 lessons."၊ "C++ Foundation has 20 lessons."၊ "C++ Foundation has 35 lessons." ဖြစ်ပါတယ်။ cpp ရဲ့ 20 က မပြောင်းဘဲ ကျန်နေတာကို သတိထားပါ — object နှစ်ခုက memory ထဲမှာ သီးခြား နေရာ နှစ်ခု ဖြစ်လို့ပါ။

const member function နဲ့ object ကို reference နဲ့ ပို့ခြင်း

Object တွေကို function ဆီ ပို့တဲ့အခါ copy မကူးရအောင် const reference (const Course&) နဲ့ ပို့လေ့ ရှိပါတယ်။ ဒါပေမယ့် const object ပေါ်မှာ ခေါ်လို့ရတာက const လို့ မှတ်ထားတဲ့ member function တွေပဲ ဖြစ်ပါတယ်။ const က "ဒီ function က object ကို မပြောင်းပါဘူး" ဆိုတဲ့ ကတိဖြစ်ပြီး function ထဲမှာ member ကို ပြောင်းဖို့ ကြိုးစားရင် compiler က တားပါတယ်။

const_members.cpp
#include <iostream>
#include <string>

class Course {
public:
    std::string title = "Untitled";
    int lessons = 0;

    bool isLong() const { return lessons > 15; }   // reads only
    void addLesson() { ++lessons; }                // modifies the object
};

void report(const Course& c) {                     // no copy is made
    std::cout << c.title << (c.isLong() ? ": long course\n" : ": short course\n");
    // c.addLesson();  would not compile: c is const
}

int main() {
    Course rust;
    rust.title = "Rust Basics";
    rust.lessons = 15;
    report(rust);
    rust.addLesson();
    report(rust);
}

ပထမ report() က "Rust Basics: short course" ကို ပြပြီး addLesson() က lessons ကို 16 ဖြစ်စေလို့ ဒုတိယ report() က "Rust Basics: long course" ကို ပြပါတယ်။ isLong() မှာ const မပါရင် report() ထဲက c.isLong() call က compile မဖြစ်ပါဘူး။

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

Real project တွေမှာ ဘယ်လို သုံးလဲ

သင်ဟာ class တွေကို ပထမ lesson ကတည်းက သုံးနေခဲ့ပြီးပါပြီ။ std::string နဲ့ std::vector က standard library ထဲက class တွေဖြစ်ပြီး name.size()၊ scores.push_back(90) ဆိုတာ member function call တွေပါ။ ကိုယ်ပိုင် class ရေးတာက ဒီ library type တွေလို အသုံးပြုရလွယ်တဲ့ type တွေကို ကိုယ့် domain အတွက် ဆောက်တာပါပဲ — game ထဲက Player၊ ဆိုင်ရဲ့ Order၊ server တစ်ခုရဲ့ HttpRequest စတာတွေပါ။

Project အကြီးတွေမှာ class definition ကို header file (course.h) ထဲ ထားပြီး member function ရဲ့ body တွေကို source file (course.cpp) ထဲမှာ void Course::showInfo() const { ... } ပုံစံနဲ့ ရေးလေ့ ရှိပါတယ်။ :: (scope resolution) က "ဒါက Course ရဲ့ showInfo ပါ" လို့ compiler ကို ပြောတာပါ။ ဒီလို ခွဲထားရင် class ကို သုံးတဲ့ file တွေက header ကိုပဲ include လုပ်ရပြီး implementation ပြောင်းတိုင်း အားလုံး ပြန် compile လုပ်စရာ မလိုပါဘူး။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Class နောက်က semicolon မေ့တာ

class Course { ... } လို့ ပိတ်ပြီး ; မထည့်ရင် g++ က "expected ';' after class definition" error ပေးပါတယ်။ တခြား compiler အချို့မှာ error က နောက် line မှာမှ ပေါ်လို့ ရှုပ်ထွေးစေနိုင်ပါတယ်။ Class ကို ရေးပြီးတိုင်း }; ဆိုတဲ့ ပုံစံကို စစ်ပါ။

2. class ထဲမှာ public: မေ့တာ

class Point { int x; int y; }; ဆိုရင် member တွေက private ဖြစ်လို့ main() ထဲမှာ p.x = 3; လို့ ရေးတာနဲ့ "'int Point::x' is private within this context" error တက်ပါတယ်။ ရည်ရွယ်ချက်က data အစုအဝေး သက်သက်ဆိုရင် struct ကို သုံးပါ၊ class ဆိုရင် public interface ကို ရည်ရွယ်ချက်ရှိရှိ ရွေးပါ။

3. Built-in member တွေကို initialize မလုပ်တာ

int lessons; လို member ကို assign မလုပ်ခင် ဖတ်မိရင် run တိုင်း မတူတဲ့ ကျပန်း number ထွက်နိုင်ပါတယ်၊ ဒါက undefined behavior ပါ။ int lessons = 0; လို default member initializer ကို အမြဲ ပေးထားပါ။

4. Read-only function မှာ const မပါတာ

showInfo() မှာ const မပါဘဲ const Course& c ကနေ c.showInfo() လို့ ခေါ်ရင် "passing 'const Course' as 'this' argument discards qualifiers" error ရပါတယ်။ Object ကို မပြောင်းတဲ့ member function တိုင်းကို const လို့ မှတ်ပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Class name ကို PascalCase (Course)၊ object name ကို camelCase (cppCourse) နဲ့ ရေးပါ — ဖတ်တဲ့သူက type နဲ့ value ကို ချက်ချင်း ခွဲနိုင်ပါတယ်။
  • Built-in type data member တိုင်းကို default member initializer ပေးပါ။
  • Object ကို မပြောင်းတဲ့ member function တွေကို const လို့ မှတ်ပြီး object တွေကို const reference နဲ့ ပို့ပါ။
  • Header file ထဲမှာ using namespace std; မရေးပါနဲ့၊ std:: ကို အပြည့်ရေးပါ။

Mini challenge

Mini challenge: စာအုပ် ဖတ်ပြီးသလောက် မှတ်တဲ့ class

စာအုပ်တစ်အုပ်ရဲ့ ခေါင်းစဉ်၊ စုစုပေါင်း စာမျက်နှာ နဲ့ ဖတ်ပြီးသား စာမျက်နှာကို မှတ်ထားတဲ့ Book class ရေးပြီး object နှစ်ခုနဲ့ စမ်းကြည့်ပါ။

  • title၊ pages၊ pagesRead data member တွေ ရှိရမယ်၊ pagesRead ရဲ့ default က 0 ဖြစ်ရမယ်
  • read(int n) က pagesRead ကို n တိုးပေမယ့် pages ထက် ဘယ်တော့မှ မကျော်ရ
  • percent() const က ဖတ်ပြီး ရာခိုင်နှုန်းကို integer အဖြစ် ပြန်ပေးရမယ်
  • print() const က "Title: read/pages pages (N%)" ပုံစံ ထုတ်ရမယ်၊ object နှစ်ခုကို သီးခြား ဖတ်ပြရမယ်
book.cpp
#include <iostream>
#include <string>

class Book {
public:
    // TODO: title, pages, and pagesRead (starting at 0)

    void read(int n) {
        // TODO: add n, but cap the result at pages
    }

    int percent() const {
        // TODO: pagesRead as a whole-number percentage of pages
    }

    void print() const {
        // TODO: print "Title: read/pages pages (N%)"
    }
};

int main() {
    // TODO: create two books, read 100 then 50 pages of a 400-page book,
    //       read 500 pages of a 320-page book, and print both
}

ရလဒ်: Dune: 150/400 pages (37%) Clean Code: 320/320 pages (100%)

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

Cap လုပ်ဖို့ if (pagesRead > pages) pagesRead = pages; ဒါမှမဟုတ် <algorithm> ထဲက std::min ကို သုံးပါ။ Integer ရာခိုင်နှုန်းအတွက် pagesRead * 100 / pages လို့ အမြှောက်ကို အရင် လုပ်ပါ၊ မဟုတ်ရင် integer division ကြောင့် 0 ရပါမယ်။

အဖြေ ကြည့်ရန်
book.cpp
#include <algorithm>
#include <iostream>
#include <string>

class Book {
public:
    std::string title = "Untitled";
    int pages = 0;
    int pagesRead = 0;

    void read(int n) {
        pagesRead = std::min(pagesRead + n, pages);
    }

    int percent() const {
        return pages == 0 ? 0 : pagesRead * 100 / pages;
    }

    void print() const {
        std::cout << title << ": " << pagesRead << "/" << pages
                  << " pages (" << percent() << "%)\n";
    }
};

int main() {
    Book dune;
    dune.title = "Dune";
    dune.pages = 400;

    Book cleanCode;
    cleanCode.title = "Clean Code";
    cleanCode.pages = 320;

    dune.read(100);
    dune.read(50);
    cleanCode.read(500);

    dune.print();
    cleanCode.print();
}

Quick Quiz 1 / 3

class Point { int x; int y; }; ရှိပြီး main() ထဲမှာ Point p; p.x = 3; လို့ ရေးရင် ဘာဖြစ်မလဲ။

Quick Quiz 2 / 3

Course a; a.lessons = 5; Course b = a; b.lessons = 9; std::cout << a.lessons; က ဘာ print ထုတ်မလဲ။

Quick Quiz 3 / 3

C++ မှာ class နဲ့ struct ကြား language အရ တကယ်ကွာခြားချက်က ဘာလဲ။

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

  • Class က type အသစ်တစ်ခုဖြစ်ပြီး object က memory ထဲမှာ ကိုယ်ပိုင် data နဲ့ ရှိနေတဲ့ အဲဒီ type ရဲ့ value ပါ။
  • obj.member နဲ့ member တွေကို သုံးပြီး member function ထဲက နာမည်တွေက this ကနေ ခေါ်တဲ့ object ကို ညွှန်းပါတယ်။
  • class က default private၊ struct က default public ဖြစ်ပြီး ကျန်တာ အားလုံး တူပါတယ်။
  • Built-in member တွေကို default value ပေးပြီး read-only member function တွေကို const လို့ မှတ်ပါ၊ class definition ကို }; နဲ့ ပိတ်ပါ။

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

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

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

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

Classes & Objects | Thuta Learning