part-03 knowledge summary

Note: For a summary, please refer to Tuoba Axiu ’s interview website

1. The difference between ++i and i++

++i and i++ are both increment operators in C++, they differ in their behavior and return value.

Behavior:
  • ++i is the prefixed increment operator , which adds 1 to the value of variable i and then returns the value of +1. That is to say, first self-increment, and then use.
  • i++  is a post-increment operator , which will first return the current value of variable i, and then +1 the value of variable i. That is to say, use it first and increase it later.

return value:

  • The prefix returns a reference and the postfix returns an object.
     
//++i实现代码:
int& operator++(){
    *this+=1;
    return *this;
}
//i++实现代码
int operator(int){
    int temp=*this;
    ++*this;
    return temp;
}
  • Pre-positioning does not generate temporary objects, post-positioning must generate temporary objects, and temporary objects lead to reduced efficiency.

2. The difference between overloading, rewriting (overwriting) and hiding of functions

1. Overload

Overloading means that only functions with the same name defined in the same scope have an overloading relationship. The main feature is that the function names are the same, but the parameter types and numbers are different . There cannot be functions that have the same number and type of parameters and only rely on different return values ​​to distinguish them. Overloading has nothing to do with whether the function is virtual or not.

class A{
    virtual int fun();
    void fun();
    void fun(double val);
    static int fun(char val);
    ...
}

2. Override (override)

Overriding refers to overriding the function of the same name in the base class in the derived class. Overriding means rewriting the function body. It is required that the base class function must be a virtual function and:

  • Have the same number of parameters as the virtual function of the base class
  • have the same parameter types as the virtual function of the base class
  • Have the same return value type as the virtual function of the base class
//父类
class A{
public:
    virtual int fun(int val){};
}

//子类
class B{
public:
    //重写,一般加override可以确保是重写父类的函数
    virtual int fun(int val) override{};

The difference between  overloading and overriding :

  • Overriding is the vertical relationship between parent classes and subclasses, and overloading is the horizontal relationship between different functions.
  • Overriding requires the parameter list to be the same, overloading requires the parameter list to be different, and the return value is not required.
  • In the overwriting relationship, the calling method is determined based on the object type, and the overloading selects the function body based on the corresponding relationship between the actual parameter list and the formal parameter list when calling.

3. hide

Hiding means that in some cases, functions in the derived class block functions of the same name in the base class, including the following situations:

  • The two function parameters are the same, but the base class function is not a virtual function. The difference between hiding and overriding lies in whether the base class function is a virtual function.
#include<iostream>
using namespace std;

class A {
public:
    void fun(int a) {
        cout << "A" << endl;
    }
};
class B :public A {
public:
    void fun(int a) {
        cout << "B" << endl;
    }
};

int main() {
    B* b=new B();
    b->fun(0);
    b->A::fun(0);
    return 0;
}

  • If the two function parameters are different, no matter whether the base class function is a virtual function or not, it will be hidden. The difference with overloading is that the two functions are not in the same class.
#include<iostream>
using namespace std;

class A {
public:
    void fun(int a) {
        cout << "A" << endl;
    }
};
class B :public A {
public:
    void fun(char a) {
        cout << "B" << endl;
    }
};

int main() {
    B* b=new B();
    b->fun(0);
    b->A::fun(0);
    return 0;
}

Guess you like

Origin blog.csdn.net/Ricardo_XIAOHAO/article/details/132683609