# Class and Object

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2F4ApeYg0LzPBNYU0C27Xr%2Fimage.png?alt=media\&token=c2632cc5-0491-4d86-b26c-9ed8ec162313)

## **Class**

&#x20;    หน่วยการสร้างที่นำไปสู่การเขียนโปรแกรมเชิงวัตถุ เป็นการกำหนดชนิดข้อมูลที่ผู้ใช้กำหนดเอง ประกอบไปด้วยตัวแปร (Attributes or Variables) และกระบวนการดำเนินการ (Function or Method) สามารถเข้าถึงและใช้งานได้โดยการสร้างวัตถุของคลาสนั้น ซึ่ง class เปรียบเสมือนพิมพ์เขียวของวัตถุ (Object)

```cpp
class ClassName {
    public:
    int variable;
    void myMethode() {
        //..
    }
};
```

{% hint style="info" %}
**ฟังก์ชันที่อยู่ใน class จะมีอีกชื่อเรียกว่า เมดธอด(Method)**&#x20;
{% endhint %}

####

### Access specifier

&#x20; ตัวระบุการเข้าถึงสมาชิก (Attributes และ Methods) ภายใน class&#x20;

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MQwR29aMuImdjSxEO0H%2F-MQwlw-cQ9VG5PCZrl7j%2FScreenshot%20from%202021-01-14%2000-24-46.png?alt=media\&token=88ba4247-175f-4ce4-b263-5b3500f9976f)

* **public** ->  สามารถเข้าถึงข้อมูลได้จากภายนอก class
* **private** ->  ไม่สามารถเข้าถึงข้อมูลได้จากภายนอก class แต่สามารถเข้าถึงได้จากหลักการของ Encapsulation
* **protected** -> ไม่สามารถเข้าถึงข้อมูลจากภายนอก class แต่สามารถเข้าถึงได้จาก Inherited class&#x20;

{% hint style="success" %}
**คำถาม** Access specifier มีความสำคัญอย่างไร?

**คำตอบ** เนื่องจากภายใน class มีการตั้งค่าพื้นฐานการเข้าถึงเป็น private ทำให้ไม่สามารถเข้าถึงข้อมูลได้ หากต้องการเข้าถึงข้อมูลภายใน class นั้นต้องทำการระบุการเข้าถึงเป็น public&#x20;
{% endhint %}

###

## **Object**

&#x20;    เนื่องจาก class เป็นเพียงข้อมูลจำเพาะบอกลักษณะ ไม่สามารถใช้ในโปรแกรมได้จึงต้องสร้างสิ่งที่เรียกว่าวัตถุ (object) ที่เป็นสิ่งที่สามารถระบุตัวตนได้โดยอาศัยลักษณะและพฤติกรรมบางอย่าง เปรียบเสมือนการประกอบพิมพ์เขียวให้สมบูรณ์กลายเป็นวัตถุชิ้นหนึ่ง

{% tabs %}
{% tab title="Example code C++" %}

```cpp
// oop_p1_1.cpp
#include <iostream>
using namespace std;

class Book{
    public:
          string title;
          string author;
          int numPages;
};

int main(){

    Book book1;
    book1.title = "Harry Potter";
    book1.author = "JK Rowling";
    book1.numPages = 1500;

    cout << book1.title << endl;

    Book book2;
    book2.title = "Lord of the Rings";
    book2.author = "JRR Tolkien";
    book2.numPages = 1300;

    cout << book2.title << endl;

    return 0;
}
```

{% endtab %}

{% tab title="Method" %}

```cpp
// oop_p1_2.cpp
#include <iostream>
using namespace std;

class Book{
    public:
    string title;
    string author;
    int numPages;

    void display(string title,string author,int numPages){
        cout << title << endl;
        cout << author << endl;
        cout << numPages << endl;
    }
};

int main(){

    Book book1;
    book1.title = "Harry Potter";
    book1.author = "JK Rowling";
    book1.numPages = 1500;

    cout << "object 1" << endl;
    book1.display(book1.title,book1.author,book1.numPages);

    Book book2;
    book2.title = "Lord of the Rings";
    book2.author = "JRR Tolkien";
    book2.numPages = 1300;

    cout << "object 2" << endl;
    book2.display(book2.title,book2.author,book2.numPages);

    return 0;
};

```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Output" %}

```cpp
Harry Potter
Lord of the Rings
```

{% endtab %}

{% tab title="Output Method" %}

```
object 1
Harry Potter
JK Rowling
1500
object 2
Lord of the Rings
JRR Tolkien
1300
```

{% endtab %}
{% endtabs %}

&#x20;    จากตัวอย่างจะเห็นว่า ผู้ใช้กำหนดว่าวัตถุที่เป็นหนังสือนั้นจะประกอบไปด้วย ชื่อเรื่อง ผู้เขียนและจำนวนหน้า ในกรณีนี้ object ที่ชื่อว่า book1 และ book2 เป็นหนังสือที่มีการกำหนดชื่อหนังสือ ผู้แต่งและจำนวนหน้าเรียบร้อยแล้ว

{% hint style="danger" %}
1 class สามารถนำไปใช้ได้กับหลาย object แต่ 1 object สามารถมีได้เพียง 1 class เท่านั้น
{% endhint %}

{% hint style="success" %}
**คำถาม** ใน 1 ไฟล์ สามารถมีหลาย class ได้หรือไม่?

**คำตอบ** ได้&#x20;
{% endhint %}

{% tabs %}
{% tab title="Example code C++" %}

```cpp
// oop_p1_3.cpp
#include <iostream>
using namespace std;

class Book{
    public:
    string title;
    string author;
    int numPages;
};

class people{
   public:
   string name;
   int age;
};


int main(){

    Book book1;
    book1.title = "Harry Potter";
    book1.author = "JK Rowling";
    book1.numPages = 1500;

    cout << book1.title << endl;

    Book book2;
    book2.title = "Lord of the Rings";
    book2.author = "JRR Tolkien";
    book2.numPages = 1300;

    cout << book2.title << endl;

    people obj1;
    obj1.name = "AA" ;
    obj1.age = 20;

    cout << "My name is " << obj1.name << endl;
    cout << "I am " << obj1.age << " years old" << endl;

    return 0;
}
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Output" %}

```cpp
Harry Potter
Lord of the Rings
My name is AA
I am 20 years old
```

{% endtab %}
{% endtabs %}

แหล่งอ้างอิงเพิ่มเติม

* <https://www.mikedane.com/programming-languages/c++/classes-objects/>
