Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++
C++
  1. ความแตกต่างระหว่าง struct และ class ใน C++

    ความแตกต่างเพียงอย่างเดียว: struct: members และ base classes เป็น public โดย default class: members และ base classes เป็น private โดย default นอกจากนี้ ทำงานเหมือนกันทุกประการ (functionally equivalent) ความแตกต่างทาง semantics (การใช้งาน): struct ใช้แทน data structure (เก็บข้อมูล) - passive data

  2. การส่งอาร์เรย์ 1 มิติไปยังฟังก์ชันใน C++

    C++ ไม่อนุญาตส่ง array ทั้งอาร์เรย์ แต่ส่ง pointer ไปยัง element แรกได้ (ใช้ชื่ออาร์เรย์โดยไม่มี index) มี 3 รูปแบบ parameter ทั้งหมดเทียบเท่ากัน (คอมไพเลอร์เห็นว่าเป็น int*): Pointer: void myFunction(int *param) Sized array: void myFunction(int param[10]) (ขนาดถูกละเลย) Unsized array: void myFunc

  3. Undefined Behaviors ที่พบบ่อยใน C++

    รายการสาเหตุหลักของ Undefined Behavior ใน C++ (ระบุในมาตรฐาน) ควรหลีกเลี่ยงทุกประการ: Signed integer overflow Dereference NULL pointer, pointer จาก new ขนาด 0, pointer ไม่ได้ initialize, pointer เกินขอบเขต array ใช้ pointer ชี้ object ที่หมด scope หรือถูก delete แล้ว (dangling pointer) Pointer ari

  4. การส่งอาร์เรย์ 2 มิติไปยังฟังก์ชันใน C++

    C++ ไม่อนุญาตส่ง array ทั้งอาร์เรย์เป็น argument ตรงๆ แต่ส่ง pointer ไปยัง array ได้ (ระบุชื่ออาร์เรย์โดยไม่มี index) มี 3 วิธีส่ง 2D array: 1. ระบุขนาด column (column size ตายตัว) void processArr(int a[][10]) { ... } 2. ส่ง array ของ pointers void processArr(int *a[10]) { ... } // เรียกใช้: int *a

  5. Regular cast vs static_cast vs dynamic_cast ใน C++

    static_cast ใช้สำหรับ type conversion ธรรมดา/ปกติ (ordinary) เช่น float → int, char → int รับผิดชอบ implicit coercion ด้วย dynamic_cast ใช้สำหรับ polymorphism เฉพาะการ cast จาก base class ไป derived class (downcasting) ตรวจสอบ runtime (ต้องมี virtual function) Regular Cast (C-style cast) รูปแบบ (Ty

  6. Forward Declarations ใน C++ คืออะไร

    Forward declaration บอกคอมไพเลอร์ล่วงหน้าว่ามีคลาสชื่อนี้อยู่ (จะนิยามภายหลัง) ช่วยให้โค้ดหลัง declaration ใช้ชื่อคลาสได้ แม้ยังไม่เห็น definition ตัวอย่าง class Person; // Forward declaration void myFunc(Person p1) { // ใช้ Person ได้แล้ว แม้ยังไม่เห็น definition } class Person { // Defini

  7. ทำไมควรหลีกเลี่ยง global variables ใน C/C++

    ควรหลีกเลี่ยง global variables ในทุกภาษา ไม่ใช่แค่ C++ เหตุผล: เข้าถึง/แก้ไขได้จากทุกส่วนของโค้ด: ยากมากที่จะติดตามว่ามีใครบ้างที่อาจแก้ไขค่า ไม่มี access control: ไม่สามารถจำกัดให้เฉพาะบางส่วนเข้าถึงได้ Tight coupling: โค้ดต่างๆ ผูกมัดกันผ่าน global state Namespace pollution: ชื่อตัวแปรชนกัน นำไป

  8. ความแตกต่างระหว่าง struct และ typedef struct ใน C++

    ใน C++ ไม่มีความแตกต่าง ระหว่าง struct และ typedef struct เพราะ C++ ถือว่า struct/union/enum/class declarations ทั้งหมดถูก typedef แบบ implicit แล้ว (ตราบใดที่ชื่อไม่ถูกซ่อนโดย declaration อื่น) ข้อแตกต่างเล็กน้อย: typedef ไม่สามารถ forward declare ได้ ต้อง include ไฟล์ที่มี typedef ก่อนใช้งานเสมอ

  9. Segmentation Fault ใน C++ คือ Undefined Behavior จริงหรือไม่?

    Undefined Behavior (UB)** คือการที่มาตรฐานไม่กำหนด行為ของโปรแกรม ให้ compiler/OS ทำอะไรก็ได้ (อาจ crash, อาจทำงานได้, อาจผลลัพธ์ผิด) Segmentation fault เป็น หนึ่งในผลลัพธ์ที่เป็นไปได้ ของ UB แต่ไม่ใช่ทุก UB เกิด segfault ตัวอย่าง #include <iostream> int main() { int arr[2]; arr[0] = 0;

  10. ความแตกต่างระหว่าง std::endl และ \n ใน C++

    \n พิมพ์ newline (platform-specific: Windows = \r\n, Unix = \n) std::endl ทำ 2 สิ่ง: (1) พิมพ์ newline (2) flush stream (บังคับส่งข้อมูลออกทันที) แนวทางปฏิบัติ: ใช้ \n ในกรณีทั่วไป เพราะ flush บ่อยๆ ทำให้ช้าลง (performance) ใช้ std::endl เฉพาะต้องการให้ output แสดงทันที (เช่น logging, progress bar,

  11. วิธีแปลง int เป็น string ใน C++

    วิธีที่ 1: ใช้ itoa (C-style, ไม่มาตรฐาน) #include <iostream> #include <cstdlib> // สำหรับ itoa (non-standard) int main() { int a = 10; char *intStr = itoa(a); // หมายเหตุ: itoa ไม่อยู่ในมาตรฐาน C++ string str = string(intStr); cout << str; } วิธีที่ 2: ใช้ to_st

  12. คีย์เวิร์ด explicit ใน C++ หมายถึงอะไร

    explicit ใช้ติดหน้า constructor เพื่อ ป้องกัน implicit conversion (การแปลงแบบนัย) ตัวอย่างปัญหา class Foo { public: Foo(int n); // จัดสรร n ไบต์ Foo(const char *p); // initialize ด้วย string }; หากเขียน Foo mystring = x; ตัวอักษร x จะถูกแปลงเป็น int (ASCII 120) แล้วเรียก Foo(int

  13. วิธีเรียก virtual function ภายใน constructor ใน C++

    เตือน: การเรียก virtual function จาก constructor หรือ destructor เป็นอันตรายและควรหลีกเลี่ยง เพราะ virtual function ที่ถูกเรียกจะเป็นของ Base class ไม่ใช่ Derived class ใน C++ ทุกคลาสสร้าง virtual method table (vtable) ของตัวเอง ก่อน เข้า constructor ของตัวเอง ดังนั้นการเรียก virtual method ใน cons

  14. ทำไม C++ จึงมี header files (.h) และ .cpp files

    C++ แยก header (.h/.hpp) และ implementation (.cpp) เพื่อ แยก interface (อินเตอร์เฟซ) ออกจาก implementation (การติดตั้ง) Header files: ประกาศ ว่าจะทำอะไร - API ของคลาส คล้าย interface ใน Java .cpp files: นิยาม จะทำอย่างไร - การติดตั้งจริงของฟังก์ชันที่ประกาศไว้ ประโยชน์: ลด dependencies: โค้ดที่

  15. วิธีแปลง std::string เป็น const char* หรือ char* ใน C++

    ใช้เมธอด c_str() ของคลาส string เพื่อได้ const char* ตัวอย่าง: ได้ const char* #include <iostream> using namespace std; int main() { string x(hello); const char* ccx = x.c_str(); cout << ccx; } ผลลัพธ์ hello ตัวอย่าง: ได้ char* (ต้องจัดสรรหน่วยความจำเอง) #include <iostre

  16. วิธีพิมพ์ค่า double ด้วยความแม่นยำเต็มที่ (Full Precision) ใน C++

    ใช้ manipulators บน cout ตั้งค่าความแม่นยำและรูปแบบ fixed เพื่อแสดงค่า double ด้วย precision สูงสุด ตัวอย่างโค้ด #include <iostream> #include <limits> using namespace std; int main() { typedef std::numeric_limits<double> dbl; double PI = 3.14159265358979; cout.precisi

Total 6007 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:14/301  20-คอมพิวเตอร์/Page Goto:1 8 9 10 11 12 13 14 15 16 17 18 19 20