C++ เป็นภาษาโปรแกรมมิ่งแบบทั่วไป รองรับ procedural, object-oriented และ generic programming C++ เป็น superset ของ C โปรแกรม C ที่ถูกต้องทั้งหมดจะรันใน C++ ได้
C++ รองรับ OOP ด้วยคุณสมบัติ: data hiding, encapsulation, inheritance, polymorphism ฯลฯ
โปรแกรม Hello World แรก
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl; // This prints Hello, World!
return 0;
}
ผลลัพธ์
Hello, World!
ส่วนประกอบของโปรแกรม
Headers (ไฟล์หัว)
#include <iostream> นำเข้าไลบรารีสำหรับ input/output พื้นฐาน
Namespaces (เนมสเปซ)
using namespace std; บอกคอมไพเลอร์ให้ใช้เนมสเปซ std (standard library)
main() ฟังก์ชันหลัก
int main() จุดเริ่มต้นการทำงานของโปรแกรม C++ ทุกโปรแกรม
คำสั่งแสดงผล
cout << "Hello, World!" << endl; แสดงข้อความ cout เป็นออบเจ็กต์ของคลาส ostream ผูกกับ stdout
Comments (ความคิดเห็น)
// This prints Hello, World! คอมเมนต์บรรทัดเดียว เริ่มต้นด้วย // คอมไพเลอร์จะละเว้น
return statement
return 0; จบการทำงานของ main() คืนค่า 0 แสดงว่าจบปกติ (success)