Bantu Technical Interview Summary

Bantu Network Technology Interview Summary:

1. What is polymorphism and what function does it have?

Answer: Polymorphism means that after a property or service defined in a general class is inherited by a special class, it can have different data types or show different behaviors, so it is polymorphism to be able to derive another class from one class. Basic conditions.
From an implementation perspective, polymorphism can be divided into two categories: compile-time polymorphism and execution-time polymorphism. Compile-time polymorphism means that the compiler determines the specific operation object of the same-name operation at compile time. In C++, it is mainly realized through function overloading and operator overloading; while execution-time polymorphism means that the program determines the specific operation at runtime. Object, C++ is mainly implemented through virtual functions. Compile-time polymorphism is also called static binding, and runtime polymorphism is also called dynamic binding.
There are two ways to achieve polymorphism: overloading and overriding.

2. Overloading and Overriding:

  • Overloading:
    Overloading is the simplest form of polymorphism and is divided into operator overloading and function overloading. Overloading means that multiple functions with the same name are allowed, and these functions have different parameter lists (or different number of parameters, or different parameter types, or both). The implementation of overloading is: the compiler modifies the names of the functions with the
    same name functions, and then these functions with the same name become different functions (at least for the compiler). Calls to functions are determined during compilation and are static. That is, their addresses are bound at compile time.

  • Overriding
    Overriding refers to the practice of a derived class redefining a virtual function of a base class. When the derived class redefines the virtual function of the base class, the base class pointer dynamically calls the function of the same name belonging to the derived class according to the different derived class pointers assigned to it. Such a function call cannot be determined during compilation (calling The virtual function address of the derived class cannot be given). Therefore, such function addresses are bound at runtime.

3. Virtual and pure virtual functions

  • Virtual function: A
    virtual function is a member function declared with virtual in the class. The virtual function of the base class is still a virtual function in the derived class. Once a function is declared virtual, it remains virtual in every derived class, no matter how many levels it is inherited. A virtual function is chosen at runtime based on the type of the object pointed to by the pointer, not the type of the pointer. If the definition of the corresponding virtual function is missing in the derived class, the virtual function of the base class will be used by default.
    Function: You can redefine virtual functions in a derived class, so that the pointer to the base class object can access the redefined virtual function in the derived class after pointing to the derived class object.
    Note: When a derived class is redefined, its function name, number of parameters, order of parameter types and return value must be exactly the same as those in the base class.
    Access mode: access by base class pointer or by object name

  • Pure virtual function:
    It is a special virtual function whose declaration format is:

    virtual type function name (parameter list) = 0;

A pure virtual function cannot be called directly, it only provides an interface consistent with the derived class for storing functions with the same name in the derived class. Pure virtual functions cannot be inherited.

4. Multiple inheritance:
1. Construction order of multiple inheritance: The base class constructor is called according to the order in which the base class constructor appears in the class derivation list, and the constructor calling order is not affected by the base class appearing in the constructor initialization list. , and is not affected by the order in which the base classes appear in the constructor initialization list.

2. In the case of a single base class, the pointer or reference of the derived class can be automatically converted to the pointer or reference of the base class, and the same is true for multiple inheritance, the pointer or reference of the derived class can be converted to the pointer or reference of any of its base classes.

3. Multiple inheritance, like single inheritance, can only access the members defined (or inherited) in the base class with the pointer or reference of the base class, and cannot access the members introduced in the derived class.
When a class inherits from multiple base classes, there is no implicit relationship between those base classes, and it is not allowed to use a base class pointer to access members of other base classes.

4. If the user defines the derived class's own copy constructor or assignment operator, the user is responsible for copying (assigning) all the base class subsections. For the copy constructor or assignment operator of the derived class synthesized by default by the compiler, the base class part will be automatically copied or assigned.

5. Under multiple inheritance, the name used in the member function is usually searched first in the function itself. If the name cannot be found locally, it continues to search in the member's class, and then searches for each base class in turn. Note that name lookup always follows two steps: (1) First the compiler finds a matching declaration (which has nothing to do with parameters, return values, public or private, only the name) (2) Then, the compiler determines Whether the found statement is legitimate.

5. When applying for memory, the difference between heap memory and stack memory

  • The stack is
    the . The variables inside are usually local variables, function parameters, etc.
  • The heap is the memory block allocated by new. The compiler does not care about their release and is controlled
    by application. Generally, a new corresponds to a delete. If the programmer does not release
    it, the operating system will automatically recycle it after the program ends.

6. The difference between structure and class The
struct in C++ expands the struct in C. It is no longer just a data structure containing different data types, it has acquired too many functions.
Can a struct contain member functions? can!
Can struct inherit? can! !
Can struct achieve polymorphism? can! ! !

Since it can achieve these, what is the difference between it and class?

The most essential difference is the default access control, which is reflected in two aspects:

1) Default inherited access rights. struct is public, class is private.
You can write code like this:
struct A
{
char a;
};
struct B : A
{
char b;
};

At this time, B is public inheriting A.

If the above struct is changed to class, then B inherits A privately. This is the default inherited access.

So when we usually write class inheritance, we usually write like this:
struct B : public A

It is to indicate that it is public inheritance, rather than the default private inheritance.

Of course, whether the default is public inheritance or private inheritance depends on the subclass rather than the base class.

I mean, a struct can inherit a class, and a class can also inherit a struct, so the default inheritance access authority depends on whether the subclass uses a struct or a class. as follows:

struct A{}; class B : A{}; //private inheritance
struct C : B{}; //public inheritance

2) struct as the implementation of the data structure, its default data access control is public, and class as the implementation of the object, its default member variable access control is private.

Note my wording above, I still emphasize that struct is an implementation of a data structure, although it can be used like a class. I still call variables in struct data and variables in class members, although there is no difference between them.

In fact, whether to use struct or class depends entirely on personal preference. You can replace all classes in your program with struct, and it can still run normally. But the best advice I can give is: when you think what you're going to do is more of a data structure, use a struct, and if what you're trying to do is more of an object, use a class.

Of course, what I want to emphasize here is that for access control, it should be clearly pointed out in the program instead of relying on the default. This is a good habit and makes your code more readable.

At this point, many people who know may think that this topic can be closed, because they know that the "only" difference between struct and class is access control. It is true that only this difference is mentioned in many literatures.

But I didn't use "the only" above, but said "the most essential", that's because they do have another difference, although that difference we may rarely touch on. That is: the "class" keyword is also used to define template parameters, just like "typename". But the keyword "struct" is not used to define template parameters. This is explained in Inside the C++ Object Model by Stanley B.Lippman.

The discussion of the problem is here, and it should basically be over. But someone has said that he has found other "differences", so let's see if this is another difference. As mentioned above, struct in C++ is an extension of struct in C. Since it is an extension, it must be compatible with all the features that struct in C in the past should have. For example you can write:

struct A //Define a struct
{
char c1;
int n2;
double db3;
};
A a={'p',7,3.1415926}; //Assign directly when defining

That is to say, struct can be initialized with {} when it is defined. So the question is, does class work? Change the above struct to class and try it out. Error! Oh~ So the man jumped out and said, he found another difference. Let's take a closer look, is this really another difference?

You try to add a constructor (or virtual function) to the above struct, what do you find?
Yes, struct can't use {} to assign initial value.

Indeed, assigning the initial value in the way of {} just uses an initialization list to initialize the data sequentially, as above, if it is written as A a={'p',7}; then c1 and n2 are initialized, and db3 no. Such a simple copy operation can only occur on simple data structures, not on objects. Adding a constructor or a virtual function will make the struct more reflect the characteristics of an object, so that the {} operation is no longer valid.

In fact, adding such a function changes the internal structure of the class. What about adding a normal member function? You will find that {} is still available. In fact, you can understand an ordinary function as an algorithm for a data structure, which does not break the characteristics of its data structure.

Then, seeing this, we found that even if struct wants to use {} to assign initial value, it must meet many constraints, these conditions actually make struct more reflect the characteristics of a data organization rather than a class.

So why do we just change struct to class above, and {} can't be used?

In fact, the problem happens to be what we talked about before - access control! Look, what have we forgotten? Yes, when the struct is changed to a class, the access control is changed from public to private, so of course, {} cannot be used to assign the initial value. Add a public, and you will find that class can also use {}, which is no different from struct! ! !

To sum up, from the above differences, we can see that struct is more suitable to be regarded as the realization of a data structure, and class is more suitable to be regarded as the realization of an object.

7. The usefulness of static and static before the function
In the C language, the keyword static has three obvious functions:
1) In the function body, a variable declared as static maintains its value unchanged during the calling process of the function (The variable is stored in the static variable area).
2) Inside a module (but outside of a function), a variable declared static can be accessed by functions used within the module, but not by other functions outside the module. It is a local global variable. Note that it can only be used after the variable has been defined. If the variable is defined after use, use extern declaration. Therefore, generally global variables are defined at the very beginning of the file.
3) Within a module, a function declared static can only be called by other functions within this module. That is, this function is restricted to use in the local scope of the module in which it is declared.

In embedded systems, as programmers, we must understand the importance of transplantation. The program may be completed by many programmers at the same time. When defining variables and functions, they may have the same name, which brings trouble to the integrated development of the system. , so the way to ensure that there is no conflict is to indicate that this variable or function is local, and you can use static.
In Linux module programming, this one is very obvious. All functions and global variables must be declared with the static keyword to limit their scope to the inside of this module, and functions or variables shared with other modules must be exported to the kernel.
The static keyword has the following functions:
1) Set the storage domain of the variable. The scope of the static variable in the function body is the function body. Unlike the auto variable, the memory of the variable is only allocated once, so its value will be called the next time. The last value is still maintained;
2) Restrict the scope of the variable, static global variables in the module can be accessed by functions used in the module, but cannot be accessed by other functions outside the module;
3) Restrict the scope of the function, in the module The static function can only be called by other functions in this module, and the scope of use of this function is limited to the module in which it is declared;
the following is the extension of C++ to static
4) The statically modified member variables in a class belong to the class and can Shared, equivalent to a global variable, does not belong to a specific instance or object, that is to say, when an instance of a class modifies the static member variable, its modified value is seen by all other instances of the class, and must be Out-of-class initialization, static member variable access form: 1° can be accessed by the object 2° can be accessed by the class 3° can be accessed by the object pointer
5) Ordinary functions have this pointer, and the pointer can access all members, but the static members in the class The function is owned by the entire class. This function does not receive this pointer, so it can only access the static member variables of the class. static can only be added when the function is declared, not when it is defined (declared in the class, initialized outside the class)

For details, see: https://www.cnblogs.com/songdanzju/p/7422380.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325164652&siteId=291194637