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

คำถามสัมภาษณ์ C++ ที่พบบ่อย (Commonly Asked C++ Interview Questions)

คำอธิบาย

ที่นี่เราจะดูคำถามสัมภาษณ์ C++ ที่สำคัญบางข้อ

  • ความแตกต่างระหว่าง C และ C++ คืออะไร?

หัวข้อCC++
บทนำC ถูกพัฒนาโดย Dennis Ritchie ราวปี 1969 ที่ AT&T Bell LabsC++ ถูกพัฒนาโดย Bjarne Stroustrup ในปี 1979
ประเภทภาษาC เป็น procedural programmingC++ รองรับทั้ง procedural และ object-oriented programming paradigms
รองรับคุณสมบัติ OOPsC ไม่รองรับ OOPs จึงไม่มี polymorphism, encapsulation, inheritanceC++ รองรับ polymorphism, encapsulation, inheritance เนื่องจากเป็น OOP language
ความปลอดภัยข้อมูลC ไม่มี encapsulation ข้อมูลจึงเป็น free entity ถูกจัดการจากภายนอกได้C++ มี encapsulation ซ่อนข้อมูล ให้มั่นใจว่า data structures และ operators ถูกใช้ตามที่ตั้งใจ
ชนิดการขับเคลื่อนC เป็น function-driven languageC++ เป็น object-driven language
ฟีเจอร์ที่รองรับC ไม่รองรับ function/operator overloading ไม่มี namespace และ reference variablesC++ รองรับ 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