Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++
C++
  1. การจับ Ctrl+C Event ใน C++

    Ctrl+C ส่งสัญญาณ SIGINT ให้ process ใช้ signal() จาก <signal.h> จับและจัดการได้ โค้ดตัวอย่าง #include <iostream> #include <signal.h> #include <unistd.h> #include <cstdlib> using namespace std; void signal_handler(int signum) { cout << Caught signal <<

  2. การ Initialize Const Member Variable ใน C++ Class

    Const member variable ต้อง initialize ผ่าน member initializer list ใน constructor ไม่สามารถ assign ใน body ได้ Syntax class MyClass { const int x; // ต้อง initialize ใน initializer list public: MyClass(int a) : x(a) {} // OK: initializer list void show() { cout << x; } }; int main

  3. ป้องกันการสืบทอดคลาสใน C++ (Final Class)

    เนื้อหาเหมือนบทความ ID 1004082251 (Simulating final class in C++) - จำลอง final class ด้วย virtual base class ที่มี private constructor เทคนิค MakeFinal (ซ้ำ) class MakeFinal { private: MakeFinal() {} friend class FinalClass; }; class FinalClass : virtual MakeFinal { public: FinalClass(

  4. คอมไพล์โปรแกรม C++ ด้วย GCC

    GCC (gcc) คอมไพล์ C++ ได้ แต่ต้อง link libstdc++ เองด้วย flag -lstdc++ ปัญหา: Linker Error gcc test.cpp # undefined reference to std::cout, std::endl, etc. แก้ไข: เพิ่ม -lstdc++ gcc test.cpp -lstdc++ ./a.out # Hello World. This is C++ program คำแนะนำ สำหรับโค้ด C++ ควรใช้ g++ โดยตรง จะจัดการ linking

  5. ความแตกต่างระหว่าง GCC และ G++ Compilers

    gcc = GNU C Compiler, g++ = GNU C++ Compiler ความแตกต่างหลัก พฤติกรรมgccg++ .c filesCompile as CCompile as C++ .cpp filesCompile as C++Compile as C++ Linkingไม่ link libstdc++ อัตโนมัติLink libstdc++ อัตโนมัติ Predefined macrosน้อยกว่า (C)มากกว่า (C++ macros) Macros เพิ่มเติมของ C++ (g++ และ gcc ก

  6. การวนลูป Enum ใน C++

    Enum (Enumeration) คือ user-defined type ที่กำหนดชื่อให้ค่าคงที่จำนวนเต็ม Syntax enum Color { RED, GREEN, BLUE }; // เริ่มจาก 0 เพิ่มทีละ 1 enum Status { OK=1, ERROR=5, PENDING }; // OK=1, ERROR=5, PENDING=6 การ Iterate Enum (C++11 scoped enum แนะนำ) Enum แบบดั้งเดิม (unscoped) สามารถวนลูปได้เพราะแป

  7. ความแตกต่างระหว่าง Destructor และ free() ใน C++

    Destructor เป็น member function מיוחד ที่เรียกอัตโนมัติเมื่อ object ถูกทำลาย (out of scope, delete, ทำลาย container) ทำ cleanup logic free() เป็น C library function (<cstdlib>) ใช้ปล่อยหน่วยความจำที่ allocate ด้วย malloc() ไม่เรียก destructor จุดสำคัญ C++ ใช้ new/delete แทน malloc/free delete

  8. การประกาศ Global Variable ใน C++

    Global variable ประกาศนอกฟังก์ชัน/บล็อก ใส่ได้ทุกที่ในไฟล์ (ปกติวางบนสุด) ในไฟล์เดียว int g_x = 10; // global int g_y = 20; int main() { cout << g_x << g_y; } ข้ามไฟล์ (Multiple Files) ไฟล์ globals.cpp: int g_x = 10; int g_y = 20; ไฟล์ main.cpp: #include <iostream> extern int g_x;

  9. คอมไพล์หลายไฟล์ .cpp ใน C++

    คอมไพล์หลายไฟล์ .cpp พร้อมกันด้วย g++ โดยระบุชื่อไฟล์ต่อกัน คำสั่ง g++ file1.cpp file2.cpp file3.cpp -o myprogram ./myprogram ตัวอย่าง: แยกฟังก์ชัน area() ไปไฟล์ area.cpp // area.cpp float area(float r) { return 3.1415 * r * r; } float area(float l, float w) { return l * w; } // find_area.cpp #incl

  10. C++ Equivalent ของ sprintf คืออะไร?

    ใน C++ ใช้ std::ostringstream (จาก <sstream>) แทน sprintf() เพื่อจัดรูปแบบสตริงอย่างปลอดภัยและยืดหยุ่น โค้ดตัวอย่าง #include <iostream> #include <sstream> #include <string> using namespace std; int main() { ostringstream os; os << Store int: << 50 <&l

  11. Inheritance ใน C++ เทียบกับ Java

    ความแตกต่างหลัก หัวข้อC++Java Root classไม่มี common baseทุกคลาส extends Object โดยนัย Multiple inheritanceรองรับโดยตรงไม่รองรับ (ใช้ interface) Virtual methodsต้องระบุ virtualทุก method เป็น virtual โดยนัย Access specifier ในการสืบทอดpublic/protected/private inheritanceเฉพาะ public (extends) Prote

  12. Static Keyword ใน C++ เทียบกับ Java

    static ใน C++ และ Java คล้ายกันแตกต่างในรายละเอียด Static Data Members (คล้ายกัน) เป็นสมาชิกของคลาส แบ่งปันให้ทุกอ็อบเจกต์ // C++ class Test { public: static int count; Test() { count++; } }; int Test::count = 0; // Java class Test { static int count = 0; Test() { count++; } } Static Member Functio

  13. การจำลอง Final Class ใน C++

    C++ ไม่มีคีย์เวิร์ด final สำหรับคลาส (ก่อน C++11) แต่สามารถจำลองได้ด้วย idiom ใช้ virtual base class ที่มี private constructor เทคนิค MakeFinal class MakeFinal { private: MakeFinal() { cout << MakeFinal ctor\n; } friend class FinalClass; // เฉพาะ FinalClass เข้าถึงได้ }; class FinalCl

  14. การใช้ explicit Keyword ใน C++

    explicit ป้องกัน implicit conversion จาก constructor ที่รับ argument เดียว (converting constructor) โดยไม่มี explicit (Implicit Conversion) class Point { double x, y; public: Point(double a=0, double b=0) : x(a), y(b) {} bool operator==(Point p) { return x==p.x && y==p.y; } }; int main(

  15. Private Destructor ใน C++

    Destructor ที่เป็น private ส่งผลต่อการสร้างและทำลาย object: กรณีที่ OK (ไม่สร้าง object) class A { private: ~A() {} }; int main() {} // OK: ไม่มี object ถูกสร้าง กรณี Error: Stack Object class A { private: ~A() {} }; int main() { A obj; } // Error: destructor inaccessible กรณี OK: Pointer เท่านั้น (

  16. Function Overloading และ const Keyword ใน C++

    C++ อนุญาต overload ระหว่าง const และ non-const member functions ตาม cv-qualifier ของ object Overload ตาม const-ness ของ Object class MyClass { public: void func() const { cout << const func\n; } void func() { cout << non-const func\n; } }; int main() { MyClass obj; cons

  17. main() สามารถ overload ได้หรือไม่ใน C++?

    C++ ไม่อนุญาต ให้ overload ฟังก์ชัน main() ที่ระดับ global จะเกิด compile error (conflicting declarations) ตัวอย่างที่ Error int main(int x) { ... } int main(char* y) { ... } int main(int x, int y) { ... } int main() { main(10); main(Hello); main(15,25); } // Error: multiple definitions of main วิธี

  18. การขยาย Namespace และ Unnamed Namespace ใน C++

    1. Extending Namespace สามารถเปิด namespace เดียวกันหลายครั้งได้ สมาชิกจะรวมกัน namespace MyNS { int a = 10; } namespace MyNS { int b = 20; } // ขยาย namespace เดียวกัน // ใช้งาน cout << MyNS::a << MyNS::b; // 10 20 2. Unnamed (Anonymous) Namespace Namespace ไม่มีชื่อ ใช้สำหรับ: จำกัด

Total 6007 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:61/301  20-คอมพิวเตอร์/Page Goto:1 55 56 57 58 59 60 61 62 63 64 65 66 67