Common programming languages like face questions and answers

1. Let the interviewer say something in C ++ some important points

  Friendship is the question of succession: Friendship is not inherited. Friend of the base class no special access to members of the derived class. If the base class is awarded a friend relationship, only the base class with special access rights to the base class of the derived class can not access granted class friends, relationships. (Removal: "c ++ primer fourth edition")

  Static member inheritance: If a base class defines static member, the entire inheritance hierarchy is only one such member. No matter how many base classes derived from a derived class, only one instance of each static member. (Removal: "c ++ primer fourth edition")

c ++ interview will ask: Different c ++ and c 8:00


2. The object-oriented thinking, encapsulation, inheritance, polymorphism, describe, respectively, for example about polymorphism runtime

2.1 object-oriented thinking:

  Is based on object-oriented in terms of process-oriented, that is the object-oriented functions implemented by objects encapsulate the functionality among objects, so that objects to achieve specific details; this idea is the data as the first, and a method or algorithm as the second, which is an optimized data, the operation is more convenient to simplify the process.

2.2 Package: hidden object attributes and implementation details, just outside the provision of public access method

  • The change in isolation, ease of use, improved reusability, improved safety

Inheritance 2.3: improved code reusability; inheritance is polymorphism premise

  Note: ① subclass all constructors are empty default constructor parameter access the parent class, the default Super the first line (); if not null constructor parameters, are required for the subclass; Further, subclass constructor this function can be used to specify their own other constructors.

Over 2.4 states: just implemented or inherited with an interface or class, then the corresponding method can be used in the parent class.

  • Benefits: Improved scalability program
  • Drawbacks: references to the parent class when the child class object, although improved scalability, but the method can have access to the parent class, subclass inaccessible method; i.e., access limitations.

2.5 run-time polymorphism: refers until the system is running, which was determined as a function of the same name needs to call based on the information generated by the operation of the program. In C ++, runtime polymorphism is achieved by inheritance and virtual functions.

What is object-oriented thinking


Function 3. strcpy function

  A: strcpy (character array, string2) function is copied into the string 2 to an array of characters.

expand:

  1. strncpy (str1, str2,2); str2 copying action is in the top two characters str1, a substituted str1 any original top two characters.
  2. memory copy function c and c ++ use, function memcpy function is the memory address from the start position of the start of the copy source indicated src n bytes to the start address of the target memory location referred to in dest.
    usage:void *memcpy(void *dest, const void *src, size_t n);
  3. the difference between strcpy and memcpy:
    1) a different copy of the contents. strcpy only copy the string, and any content can be copied memcpy, e.g. character array, integers, structures, and the like.
    2). Different methods of replication. strcpy do not need to specify the length, it encounters a character string to be copied terminator '\ 0' until the end, it is easy to overflow. memcpy copy length is determined in accordance with its three parameters.
    3) different purposes. Normally used strcpy when copying a string, while other types of data need to be copied is generally used memcpy

4. Run Results The following C program, plus the number of outputs is 14

int main() {
    for (int i = 0; i < 3; i++) {
        fork();
        printf("+");
        fflush(stdout);
    }
    wait(NULL);  // 等待子进程退出
    wait(NULL);
    wait(NULL);
    return 0;
}
Published 378 original articles · won praise 43 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_43283397/article/details/104932552