Skip to content
Siamcoder

แนวคิดเชิงวัตถุใน C++

c++2 min read

คอนเซ็ปต์ที่เกี่ยวข้องกับการเขียนโปรแกรมเชิงวัตถุ (Object-Oriented Programming) เป็นส่วนสำคัญในภาษา C++ เป็นการออกแบบและสร้างโครงสร้างของโปรแกรมโดยใช้วัตถุ (Object) เป็นหลักการ โดยมีคุณสมบัติสำคัญดังนี้:

  1. คลาส (Class): คลาสเป็นแม่แบบของวัตถุที่กำหนดคุณสมบัติและพฤติกรรมของวัตถุ โดยประกอบด้วยข้อมูล (data) และฟังก์ชัน (function) หรือเมท็อด (method) ตัวอย่างการสร้างคลาส Person ในภาษา C++:
#include <iostream>
#include <string>
class Person {
private:
std::string name;
int age;
public:
// Constructor
Person(const std::string& name, int age) {
this->name = name;
this->age = age;
}
// Method
void displayInfo() {
std::cout << "ชื่อ: " << name << std::endl;
std::cout << "อายุ: " << age << " ปี" << std::endl;
}
};
int main() {
// สร้างวัตถุ (Object) จากคลาส Person
Person person("John Doe", 25);
// เรียกใช้เมท็อด displayInfo()
person.displayInfo();
return 0;
}`

ในตัวอย่างนี้ เราสร้างคลาส Person ที่มีข้อมูล name และ age และเมท็อด displayInfo() ที่ใช้สำหรับแสดงข้อมูลบนหน้าจอ ในฟังก์ชัน main() เราสร้างวัตถุ person จากคลาส Person และเรียกใช้เมท็อด displayInfo() เพื่อแสดงข้อมูลของ person บนหน้าจอ

  1. การสืบทอด (Inheritance): เป็นกระบวนการที่ใช้สร้างคลาสใหม่โดยอิงคุณสมบัติและพฤติกรรมจากคลาสที่มีอยู่แล้ว (ซึ่งเรียกว่าคลาสแม่หรือซูเปอร์คลาส) ตัวอย่างการสืบทอดคลาส Teacher จากคลาส Person:
#include <iostream>
#include <string>
class Person {
protected:
std::string name;
int age;
public:
Person(const std::string& name, int age) {
this->name = name;
this->age = age;
}
void displayInfo() {
std::cout << "ชื่อ: " << name << std::endl;
std::cout << "อายุ: " << age << " ปี" << std::endl;
}
};
class Teacher : public Person {
private:
std::string subject;
public:
Teacher(const std::string& name, int age, const std::string& subject)
: Person(name, age) {
this->subject = subject;
}
void displayInfo() {
Person::displayInfo(); // เรียกใช้เมท็อดของคลาสแม่
std::cout << "สาขาวิชา: " << subject << std::endl;
}
};
int main() {
Teacher teacher("Jane Smith", 35, "คณิตศาสตร์");
teacher.displayInfo();
return 0;
}`

ในตัวอย่างนี้ เราสร้างคลาส Teacher ที่สืบทอดมาจากคลาส Person โดยมีฟิลด์เพิ่มเติมคือ subject และเมท็อด displayInfo() ที่เรียกใช้เมท็อดของคลาสแม่ ในฟังก์ชัน main() เราสร้างวัตถุ teacher จากคลาส Teacher และเรียกใช้เมท็อด displayInfo() ที่อยู่ในคลาส Teacher

คอนเซ็ปต์ที่เกี่ยวข้องกับการเขียนโปรแกรมเชิงวัตถุในภาษา C++ ช่วยให้เราสามารถโครงสร้างและจัดการโปรแกรมอย่างมีระเบียบและยืดหยุ่นมากยิ่งขึ้น และช่วยให้เราสามารถนำโค้ดซ้ำใช้ได้ และให้โปรแกรมมีความเป็นองค์กรภายในที่มีความสัมพันธ์และการทำงานร่วมกันอย่างมีประสิทธิภาพ

  1. การห่อหุ้ม (Encapsulation): เป็นกระบวนการที่ใช้ในการกำหนดการเข้าถึงและการปกปิดข้อมูลภายในคลาส โดยใช้คีย์เวิร์ด private, protected, public ในการกำหนดระดับการเข้าถึงของฟิลด์และเมท็อดของคลาส ซึ่งช่วยให้ข้อมูลที่สำคัญและการดำเนินการต่างๆ ในคลาสสามารถเข้าถึงได้เฉพาะผ่านอินเตอร์เฟซที่กำหนดไว้
#include <iostream>
#include <string>
class BankAccount {
private:
std::string accountNumber;
double balance;
public:
BankAccount(const std::string& accountNumber) {
this->accountNumber = accountNumber;
this->balance = 0.0;
}
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
std::cout << "ยอดเงินไม่เพียงพอ" << std::endl;
}
}
void displayBalance() {
std::cout << "เลขที่บัญชี: " << accountNumber << std::endl;
std::cout << "ยอดเงิน: " << balance << std::endl;
}
};
int main() {
BankAccount account("1234567890");
account.deposit(1000.0);
account.withdraw(500.0);
account.displayBalance();
return 0;
}`

ในตัวอย่างนี้ เรากำหนดฟิลด์ accountNumber และ balance ในคลาส BankAccount เป็น private เพื่อปกปิดการเข้าถึง ซึ่งเราสามารถเข้าถึงและปรับปรุงค่าผ่านเมท็อด deposit, withdraw, และ displayBalance ที่กำหนดเป็น public

การใช้งานคอนเซ็ปต์เชิงวัตถุในภาษา C++ ช่วยให้โปรแกรมมีความเป็นระเบียบและยืดหยุ่น และช่วยให้การเขียนและบริหารจัดการโค้ดเป็นไปอย่างมีประสิทธิภาพ อีกทั้งยังช่วยให้โค้ดสามารถนำไปใช้ซ้ำใช้กันในโปรแกรมอื่นได้ง่ายและมีความยืดหยุ่นสูง