2. Data Members and MemberFunctions

Class (คลาส) เป็นโครงสร้างข้อมูลที่สามารถเก็บข้อมูลและใส่ฟังก์ชั้นไว้ด้านในได้ โดยคลาสประกอบด้วย

  • data members (attributes): ข้อมูลสมาชิกที่อยู่ในคลาส
  • member functions (methods): ฟังก์ชันสมาชิกที่อยู่ในคลาส
        class Rect{
            public:
                int width,height;
                int getArea(){
                    return width*height;
                }
        };

        int main(){
            Rect myrect;
            myrect.width=2;
            myrect.height=3;
            cout<<"myrect.area() is "<<myrect.getArea();
            return 0;
        }

output

myrect.area() is 6

results matching ""

    No results matching ""