Frequently asked questions in C + + interview

This article explains several questions often asked in C + + interview. This article explains the frequently asked questions such as initialization list, inheritance and string through demo. Look at the example below

Initialization list

//Base class
class Base
{
public:
    Base() : m_nbase(0), m_nbase2(m_nbase + 100) {}
    Base(int n) : m_nbase(n), m_nbase2(m_nbase + 100)
    {
        cout << "this is Base construct " << endl;
    }
    ~Base()
    {
        cout << "this is Base destruct " << endl;
    }

   virtual void printData()
    {
        cout << "this is Base printData " << endl;
        cout << "data is " << m_nbase << endl;
        // cout << "base2 data is " << m_nbase2 << endl;
    }

    void printData2()
    {
        cout << "base2 data is " << m_nbase2 << endl;
    }

    int SizeOf(char p[])
    {
        return sizeof(p);
    }

    int SizeOf2(char *p)
    {
        return sizeof(p);
    }

private:
    int m_nbase;
    int m_nbase2;
};

A class Base is implemented. The constructor of the class adopts the initialization list, which initializes the initial class members in order.
Next, call the following in the main function

Base b1(1);
b1.printData();
b1.printData2();

1 ask whether the initialization list of Base will report an error?
answer:
There will be no problem because the initialization list initializes class members in order. Therefore, m will be output respectively_ The value of nbase is 1, M_ The value of nbase2 is 101

Inheritance problem

Inheritance questions often ask about the relationship between base classes and subclasses. Inheritance constructs base classes first and then subclasses. When destructing, first destruct subclasses, and then destruct base classes.
We implement a Derive class that inherits from Base

class Derive : public Base
{
public:
    Derive(int n) : Base(n), m_nderive(n) 
    { cout << "this is Derive construct " << endl; }

     ~Derive()
    {
        cout << "this is Derive destruct " << endl;
    }

    void printData()
    {
        cout << "this is Derive printData" << endl;
    }

private:
    int m_nderive;
};

The Derive class inherits the Base class. Next we call the following in the main function.

Derive d1(2);
d1.printData();
cout << " ................." << endl;

1 ask what the above program outputs
Answer: the output is as follows

this is Base construct 
this is Derive construct 
this is Derive printData
this is Derive destruct 
this is Base destruct 

Because when constructing, first construct the base class and then construct the subclass. When destructing, first destruct the subclass and then destruct the base class
If the main function is called as follows

Base *b1 = new Derive(2);
b1->printData();
delete b1;

2 ask what the above program outputs?
Answer: the output is as follows

this is Base construct 
this is Derive construct 
this is Derive printData
this is Base destruct 

Because when constructing, the base class is constructed first, and then the subclass is constructed.
Since the creation of a subclass object returns the base class pointer, calling the virtual function printData will trigger the polymorphic mechanism and call the printData of the subclass.
When destructing, because the destructor is not a virtual function, the destructor of the subclass will not be called!!!
Therefore, only the destructor of Base will be output.
3. How to solve the problem that subclasses cannot be destructed in polymorphism?
Answer: set the destructors of the base class and subclass as virtual destructors. You can't do without one!!!
Amend as follows

class Base
{
public:
    Base() : m_nbase(0), m_nbase2(m_nbase + 100) {}
    Base(int n) : m_nbase(n), m_nbase2(m_nbase + 100)
    {
        cout << "this is Base construct " << endl;
    }
    ~Base()
    {
        cout << "this is Base destruct " << endl;
    }

   virtual void printData()
    {
        cout << "this is Base printData " << endl;
        cout << "data is " << m_nbase << endl;
        // cout << "base2 data is " << m_nbase2 << endl;
    }
    //.... ellipsis
};

class Derive : public Base
{
public:
    Derive(int n) : Base(n), m_nderive(n) 
    { cout << "this is Derive construct " << endl; }

    virtual  ~Derive()
    {
        cout << "this is Derive destruct " << endl;
    }

    void printData()
    {
        cout << "this is Derive printData" << endl;
    }

private:
    int m_nderive;
};

Call again at this time

Base *b1 = new Derive(2);
b1->printData();
delete b1;

Program output

this is Base construct 
this is Derive construct 
this is Derive printData
this is Derive destruct 
this is Base destruct 

String sizeof

sizeof is also a common question

char carry[100] = "Hello World";
cout << "sizeof(carry)  " << sizeof(carry) << endl;
char *pstr = "Hello World";
cout << "sizeof(pstr)   " << sizeof(pstr) << endl;
cout << "sizeof(*pstr)  " << sizeof(*pstr) << endl;
Base b1;
cout << "b1.SizeOf(carry)  " << b1.SizeOf(carry) << endl;
cout << "b1.SizeOf2(carry) " << b1.SizeOf2(carry) << endl;

1 ask what the above program outputs
Answer: the output is as follows

sizeof(carry)  100
sizeof(pstr)  4
sizeof(*pstr)  1
b1.SizeOf(carry) 4
b1.SizeOf2(carry) 4

Carry is an array, so sizeof(carry) is an array with a size of 100
pstr is a char pointer, so sizeof(pstr) is a pointer with a size of 4 bytes. Of course, it is 8 bytes on 64 bit machines
The output of my machine is 8, which varies according to the machine. Just remember the pointer size.
pstr points to the first address of the string, which is also the address of the first character, * pstr is the first character
sizeof(*pstr) is the size of one character, so it is 1
b1.SizeOf and B1 Sizeof2 internally calls sizeof() a char pointer, so they are all 4,
The output on my machine is 8

bool, float and 0 comparison

bool and 0 comparison

bool bval = true;
if(!bval){
    //...
    //Logic with bval of 0
}else{
    //...
}

float and 0 comparison

    float f1 = 0.1;
    if (f1 <= FLT_EPSILON && f1 >= -FLT_EPSILON)
    {
        cout << "this is float 0" << endl;
    }
    else
    {
        cout << "this is not float 0" << endl;
    }

    double d1 = 0.0;
    if (d1 <= DBL_EPSILON && d1 >= -DBL_EPSILON)
    {
        cout << "this is double 0" << endl;
    }
    else
    {
        cout << "this is not double 0" << endl;
    }

summary

This article introduces some common interview questions
Source code link
https://gitee.com/secondtonone1/cpplearn
To learn more about C + + systematically, click the link below.
C + + Basics

Keywords: C++ Back-end

Added by itsgood on Thu, 03 Mar 2022 06:04:06 +0200