# Class and Object

## **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;

![](/files/-MQwlw-cQ9VG5PCZrl7j)

* **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/>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aic-eec.com/c-c++-for-embedded-programming/c-programming-techniques/object-oriented-programming-oop-in-c++/class-and-object.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
