คำอธิบาย
ที่นี่เราจะดูคำถามสัมภาษณ์ C++ ที่สำคัญบางข้อ
ความแตกต่างระหว่าง C และ C++ คืออะไร?
| หัวข้อ | C | C++ |
|---|---|---|
| บทนำ | C ถูกพัฒนาโดย Dennis Ritchie ราวปี 1969 ที่ AT&T Bell Labs | C++ ถูกพัฒนาโดย Bjarne Stroustrup ในปี 1979 |
| ประเภทภาษา | C เป็น procedural programming | C++ รองรับทั้ง procedural และ object-oriented programming paradigms |
| รองรับคุณสมบัติ OOPs | C ไม่รองรับ OOPs จึงไม่มี polymorphism, encapsulation, inheritance | C++ รองรับ polymorphism, encapsulation, inheritance เนื่องจากเป็น OOP language |
| ความปลอดภัยข้อมูล | C ไม่มี encapsulation ข้อมูลจึงเป็น free entity ถูกจัดการจากภายนอกได้ | C++ มี encapsulation ซ่อนข้อมูล ให้มั่นใจว่า data structures และ operators ถูกใช้ตามที่ตั้งใจ |
| ชนิดการขับเคลื่อน | C เป็น function-driven language | C++ เป็น object-driven language |
| ฟีเจอร์ที่รองรับ | C ไม่รองรับ function/operator overloading ไม่มี namespace และ reference variables | C++ รองรับ function/operator overloading มี namespace และ reference variables |
ความแตกต่างระหว่าง pointers และ references คืออะไร?
ความแตกต่างหลักระหว่าง pointers และ references คือ:
References ใช้เพื่ออ้างอิงตัวแปรที่มีอยู่แล้วในอีกชื่อหนึ่ง ในขณะที่ pointers ใช้เก็บที่อยู่ของตัวแปร
References ไม่สามารถกำหนดค่า null ได้ แต่ pointer ทำได้
Reference variable สามารถอ้างอิงได้โดย pass by value ในขณะที่ pointer อ้างอิงได้โดย pass by reference
Reference ต้อง initialize ตอนประกาศ แต่ pointer ไม่จำเป็น
Reference แบ่งปันที่อยู่หน่วยความจำกับตัวแปรต้นฉบับ แต่ก็ยังใช้พื้นที่บน stack ขณะที่ pointer มีที่อยู่และขนาดของตัวเองบน stack
Virtual function ใน C++ คืออะไร?
Virtual functions ใน C++ ใช้เพื่อสร้างรายการ base class pointers และเรียกเมธอดของ derived classes ใดๆ ได้โดยไม่ต้องรู้ชนิดของ derived class object Virtual functions ถูก resolve แบบ late (runtime)
ถ้า virtual function ใน base class ประกาศเป็น member function ครั้งเดียว มันจะกลายเป็น virtual ในทุก class ที่สืบทอดจาก base class นั้น ดังนั้น การใช้คีย์เวิร์ด virtual ใน derived class ขณะประกาศฟังก์ชันที่ override ใหม่ของ virtual base class function จึงไม่จำเป็น
ตัวอย่าง
#include<iostream> using namespace std; class B { public: virtual void s() { cout<<" In Base \n"; } }; class D: public B { public: void s() { cout<<"In Derived \n"; } }; int main(void) { D d; // An object of class D B *b= &d; // A pointer of type B* pointing to d b->s(); // prints "D::s() called" return 0; }ผลลัพธ์
In Derived
This pointer ใน C++ คืออะไร?
ทุก object ใน C++ เข้าถึงที่อยู่ของตัวเองได้ผ่าน pointer สำคัญที่เรียกว่า this pointer this pointer เป็น implicit parameter ของทุก member functions ดังนั้น ภายใน member function this สามารถใช้เพื่ออ้างอิงถึง object ที่เรียกใช้ได้
Friend functions ไม่มี this pointer เพราะ friends ไม่ใช่สมาชิกของ class เฉพาะ member functions เท่านั้นที่มี this pointer
ลองดูตัวอย่างต่อไปนี้เพื่อเข้าใจแนวคิดของ this pointer:
ตัวอย่าง
#include <iostream> using namespace std; class Box { public: // Constructor definition Box(double l = 2.0, double b = 2.0, double h = 2.0) { cout <<"Constructor called." << endl; length = l; breadth = b; height = h; } double Volume() { return length * breadth * height; } int compare(Box box) { return this->Volume() > box.Volume(); } private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; int main(void) { Box Box1(3.3, 1.2, 1.5); // Declare box1 Box Box2(8.5, 6.0, 2.0); // Declare box2 if(Box1.compare(Box2)) { cout << "Box2 is smaller than Box1" <<endl; } else { cout << "Box2 is equal to or larger than Box1" <<endl; } return 0; }ผลลัพธ์
Constructor called. Constructor called. Box2 is equal to or larger than Box1