2015 part C/C ++ of internship preparation (to be continued), Part 3 to be continued

Source: Internet
Author: User
Tags constant definition

2015 part C/C ++ of internship preparation (to be continued), Part 3 to be continued

1. Virtual Functions

Definition: A member function that is declared as virtual in a base class and redefined in one or more Derived classes.

Purpose: Implement polymorphism. by returning the base class pointer of the derived class, access the member function with the same name in the derived class.

Summary: If there is no virtual function, no matter what the base class Pointer Points to, it will call the base class defined function and cannot implement polymorphism.

C ++ supports two types of polymorphism:

Compilation polymorphism: implemented through overload Functions

Run polymorphism: implemented through virtual functions

 

A virtual function table is created in the compiler. Each virtual function is organized into an array of virtual function entry addresses. The hidden Member of the object-virtual function pointer is initialized at runtime-that is, when the constructor is called, which is the key to implementing polymorphism.

 

2. Pure virtual functions

Definition: if the function of the parent class is not necessary or cannot be implemented, you can set this function to virtual function name = 0, such a function is called a pure virtual function. If a class contains pure virtual functions, it is called an abstract class.

 

3. abstract class (class modified with abstract)

(1) abstract classes cannot be directly instantiated.

(2) allow abstract classes to contain abstract members

(3) abstract classes cannot be sealed

(4) The derived class of the abstract class must override all the pure virtual functions inherited by it. Otherwise, the derived class must also become an abstract class.

 

C ++ virtual functions and pure virtual functions are summarized as follows:

1) C ++ introduces virtual functions to implement Polymorphism

2) The Destructor should be a virtual function.

3) C ++ introduces pure virtual functions to regulate the behavior of derived classes

4) classes that define more than one pure virtual function are abstract classes. abstract classes cannot define specific instance objects.

 

Calling a virtual function is slower than calling a non-virtual function because:

First, we must use * _ vptr to obtain the appropriate virtualtable, and then find the corresponding call function through the index. At the same time, the use of virtual functions also costs a certain amount of memory.

 

4. Differences between sizeof operators and strlen Functions

A: When sizeof operates on a string, it calculates the string Terminator "\ 0. "\ 0" is not calculated during the strlen operation ".

1) sizeof is an operator and strlen is a library function.

2) the sizeof parameter can be a data type or variable, and strlen can only be a string ending with '\ 0' as a parameter.

3) the compiler calculates the sizeof result during compilation. The strlen function can be computed only when it is running. Sizeof calculates the memory size of the Data Type, while strlen calculates the actual length of the string.

4) the sizeof parameter of the array is not degraded. If it is passed to strlen, It is degraded to a pointer.

 

5. Differences between typedef and # define

A:

(1) typedef defines a new type name with a type check. The definition of the define macro is a simple replacement, and there is no type check. Typedef ratio # define security.

(2) typedef is processed during compilation, and # define is preprocessing.

(3) It is different when multiple variables are defined at the same time.

 

6. const meaning and Implementation Mechanism

A: In const, the defined and read-only variables are completed during compilation. the compiler can replace the referenced variables with common ones.

(1) The const variable can be defined.

(2) Easy type check

(3) can protect modified things

(4) convenient parameter modification

(5) provides a reference for function overloading.

(6) Saving space and avoiding unnecessary Memory Allocation

(7) Improved efficiency

Difference between const in C/C ++

1) const in C ++ is more useful. One is const in non-class and the other is const in class.

2) const in C is an external connection by default. Therefore, if there is a const variable with the same name in different compilation units, a name conflict occurs and an error is reported during compilation. In C ++, the const variable is internally connected by default, so there can be const with the same name in different compilation units.

Static

(1) When multiple files are compiled, variables not declared in static mode have global visibility and can be accessed by other source files.

(2) Keep the variable content persistent and store it in the static zone. initialize the variable once it starts running.

(3) In general, the default Initialization is 0.

 

7. Memory leakage (dynamic storage is used to allocate the space dynamically opened by the function. The unreleased result occupies the memory unit until the program ends .)

Memory leakage classification: frequent, sporadic, one-time, and implicit;

The wild pointer is not a NULL pointer. There are three main causes of the wild pointer:

1) pointer variables are not initialized. Any pointer variables are not automatically NULL when they are created. The default value is random. All pointer variables should be initialized when they are created.

2) After the pointer p is free or deleted, It is not set to NULL, and it is easy to misunderstand as a valid pointer. Free and delete only release the memory indicated by the pointer, and do not release the pointer itself. After the pointer is released, it should be set to NULL.

The Debug C Runtime Library of Visual C ++ provides a number of functions to help diagnose code and track memory leaks.

 

8. Differences between pointers and references

1) A pointer is a variable and an address is stored. Reference is an alias of a variable;

2) There is a const pointer and there is no const reference;

3) sizeof references the size of the variable to which it points, and sizeof pointer obtains the size of the pointer;

4) the pointer can be empty, and the reference cannot be empty;

5) the pointer value can be changed after initialization, but the reference will not change after initialization.

6) pointer and reference auto-increment operations have different meanings.

7) the pointer has multiple levels, while the reference has only one level.

 

9. copy constructors

Shallow copy: When copying an object, only simple values are assigned to the data members of the object. By default, the copy constructor executes the shortest copy function. In most cases, "Shallow copy" can work very well, but once there are dynamic members in the object, there will be problems with the shallow copy. (The two pointers point to the same space in the heap. When the destructor releases the same memory twice, an error will occur)

Deep copy: for dynamic members in an object, instead of simply assigning values, the space is allocated dynamically.

P = new int; // dynamically allocate space for the new object

At this point, the two pointers point to a memory space, but the space they point to has the same content.

A duplicate constructor can exist in a class.

X (const X &); // const copy structure X (X &); // non-const copy Structure

 

10. What is the difference between stack and stack?

(1) The stack space is automatically allocated/released by the operating system, and the heap space needs to be manually allocated/released.

(2) The stack space is limited, and the stack is a large free storage zone.

(3) During the compilation period, the program allocates both variables and Function Memory on the stack, and transmits function call parameters during the program running.

(4) accessing data in the stack is faster than accessing data in the stack. The stack data is assigned a value and directly put the data to the target address. In the heap, the data is first put into the Register, and then the value is put into the address pointed to by the register.

 

11. Differences between malloc free and new delete

(1). malloc free is a function, and new delete is an operator.

(2). malloc free is not applicable to dynamic objects and non-Internal data types.

(3) the malloc pointer is void, and the new pointer has a data type.

 

12. Role of extern

1) Declaration of the extern modifier variable. For example, if the file a. c needs to reference the variable v in B. c, you can declare the extern int v in a. c, and then you can reference the variable v. The link property of the referenced variable v must be external.

2) extern modifier function declaration. Extern references are more concise than header files.

3) the extern modifier can be used to indicate the call specifications of C or C ++ functions. The main reason is that C ++ and C programs have different naming rules in the target code after compilation.

 

13. Internal and external connections in C ++

Internal Connection: If a name is partial for its compilation unit and does not conflict with the same name in other compilation units during the connection, the name has an internal connection. Example: Declaration/namespace/enum/inline function/class definition/const constant definition/Union definition

External Connection: In a multi-document program, if a name can interact with other compilation units during the connection, this name has an external connection. For example: inline function/static member variable of the class/namespace

 

14. When will the C ++ copy constructor be called?

Note: The default copying constructor and the value assignment operator are all for shortest replication. Therefore, if the object contains dynamically allocated memory, you need to rewrite the function for deep replication, ensure data integrity and security.

1) use other objects for initialization during object Creation

2) The object is passed as a function parameter.

 

15. Design and Implementation of C ++ smart pointers

Shared_ptr uses reference counting. Multiple pointers can point to the same object. auto_ptr cannot. Only one pointer can be run to point to one object. If you want to assign a value to a pointer, then the original pointer should discard the ownership of the object. Shared_ptr has been included in the standard pointer in the latest C ++ 11. Use RALL technology to prevent memory leakage.

 

16. A * Heuristic Search Algorithm

Heuristic Search is to evaluate the position of each search in the state space to obtain the best position, and then search until the target. In this way, a large number of search paths can be omitted. In heuristic search, location valuation is very important. Evaluation function f (n) = g (n) + h (n), f (n) is the evaluation function of node n, g (n) it is the actual cost from the initial node to the n node in the state space, and h (n) is the estimated cost of the optimal path from the n to the target node. H (n) represents the information inspired by search.

 

17. Differences between arrays and linked lists

(1) Static Array Memory Allocation and dynamic memory allocation through linked lists;

(2) arrays are continuous in the memory and the linked list is not continuous;

(3) array elements are in the stack area, and linked list elements are in the heap area;

(4) array access time complexity: 0 (1); chain table access time complexity: O (n );

(5) the time complexity of array insertion or deletion is O (n), and the time complexity of the linked list is O (1)

Note: differences between pointers and Arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.