C + +: Interview Basics __c++

Source: Internet
Author: User
1: Please tell me what is the advantage of the const compared with #define.

Answer:

Const action: Define constants, modifier function parameters, cosmetic function return value three functions. The things that are modified by the const are protected by coercion, which can prevent accidental changes and improve the robustness of the program.

1 Const constants have data types, and macro constants do not have data types. The compiler can perform type security checks on the former. The latter only characters are replaced, there is no type security check, and the replacement of characters may produce unexpected errors.

2 Some integrated debugging tools can debug const constants, but you cannot debug macro constants.

2: Describe how memory is allocated and how they differ?

1) from the static storage area allocation. The memory is allocated when the program is compiled, and exists throughout the running of the program. such as global variables, static variables.
2) created on the stack. When the function is executed, the storage units of local variables within the function can be created on the stack, and the storage units are automatically freed when the function is finished. The stack memory allocation operation is placed within the processor's instruction set.

3 allocation from the heap, also known as dynamic memory allocation. The program uses malloc or new to request any amount of memory at run time, and the programmer is responsible for releasing the memory with free or delete. The lifetime of dynamic memory is determined by the programmer and is very flexible to use, but the problem is the most.

the difference between 3.struct and class

Answer: The members of the struct are publicly owned by default, and the members of the class are private by default. struct and class are functionally equivalent in other ways. Emotionally, most developers feel that there is a big difference between the class and the structure. The sensory structure is just like a heap of open memory bits lacking in encapsulation and functionality, and the class is like a living and reliable social member with intelligent services, a strong encapsulation barrier and a well-defined interface. Since most people think so, you should probably use the struct keyword if your class has few methods and public data (something that exists in a well-designed system!), or you should use the Class keyword.

4. Overload, overwrite, and hide differences for class member functions.

Answer: A. member functions are overloaded features:
(1) The same range (in the same class);
(2) The function name is the same;
(3) different parameters;
(4) virtual keyword is optional.
B. Overlay refers to a derived class function that overrides a base class function, characterized by:
(1) different ranges (in derived classes and base classes, respectively);
(2) The function name is the same;
(3) the same parameter;
(4) The base class function must have the virtual keyword.
C. " Shadowing refers to a function of a derived class that masks a base class function with the same name as the following rule:
(1) If the function of the derived class has the same name as the function of the base class, but the parameters are different. At this point, the function of the base class is hidden, regardless of the virtual keyword (Note that you are not confused with overloading).

(2) If the function of the derived class has the same name as the function of the base class, and the parameters are the same, the base class function does not have the virtual keyword. At this point, the function of the base class is hidden (note that it is confused with overrides)

the concrete implementation of 5.String

The string class is known to be defined as follows:

Class String
{
Public
String (const char *STR = NULL); Common constructors
String (const string &another); Copy Constructors
~ String (); destructor
String & operater = (const string &RHS); Assignment function
Private
Char *m_data; Used to save strings
};
Attempt to write a member function implementation of a class.
Answer:
string::string (const char *STR)
{
if (str = null)//strlen throws an exception when the argument is null this step is judged
{
m_data = new Char[1];
M_data[0] = ' the ';
}
Else
{
m_data = new Char[strlen (str) + 1];
strcpy (M_DATA,STR);
}
}
string::string (const String &another)

{
m_data = new Char[strlen (another.m_data) + 1];
strcpy (M_data,other.m_data);
}

string& String::operator = (const String &RHS)
{
if (this = = &RHS)
return *this;
delete []m_data; Delete original data, open a new piece of memory
m_data = new Char[strlen (rhs.m_data) + 1];
strcpy (M_data,rhs.m_data);
return *this;
}

String::~string ()
{
delete []m_data;

}

6. What is the difference between a global variable and a local variable? How it is achieved. How does the operating system and compiler know.

The life cycle of a global variable is the time that the entire program runs, and the life cycle of the local variable is the time period of a local function or procedure call. The implementation is performed by the compiler using different memory allocation methods at compile time. The global variable begins to be allocated after the main function call, and if it is a static variable, it is initialized before the main function. The local variables are dynamically allocated in the user stack (or suggest that you look at the activity record in the compilation principle)

7.static What is the purpose. (Please specify at least two types)

1. Limit the scope of the variable (file-level).

2. Set the storage domain of the variable (global data area)

8.static Global variables, local variables, functions and ordinary global variables, local variables, functions

What is the difference between a static global variable and a normal global variable. What is the difference between a static local variable and a normal local variable. What is the difference between a static function and a normal function.

A: The global variable (external variable) before the description of the static is composed of the global variable. The global variable itself is the static storage mode, static global variables are of course also static storage mode. The two are not different in the way they are stored. The difference between the two is that the scope of the Non-static global variable is the entire source program, and when a source program consists of multiple source files, non-static Global variables are valid in each source file. A static global variable restricts its scope, that is, it is only valid within the source file that defines the variable, and it cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can only be common to functions within that source file, so it is possible to avoid causing errors in other source files.

From the above analysis, we can see that the change of the local variable to the static variable is the change of its storage mode that changes its lifetime. Changing the global variable to a static variable changes its scope and limits its use.

The static function differs from the normal function scope. In this file only, functions that are used only in the current source file should be described as internal functions (static), and internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, you should indicate in a header file that the source file to use these functions contains this header file

What is the difference between a static global variable and a normal global variable: Static global variables are only first made once, preventing them from being referenced in other file cells;

What is the difference between a static local variable and a normal local variable: the static local variable is only initialized once, and the next time it is based on the previous result value;

What is the difference between a static function and a normal function: There is only one copy of the static function in memory, and the normal function maintains a copy of each call

The program's local variables exist in (the stack), and the global variables exist in the (static area), and the dynamic request data exists in the (heap)

The difference between 9.vector and list.
Vector internal use array, access speed, but delete data compare performance
List internal Use linked list, access speed is slow, but delete data relatively fast


10. How the pure virtual function is implemented. Let's talk about the principles of compiling.
Inside the class, add a virtual function table pointer to a virtual function table that contains the entry address of all virtual functions, and the virtual function table for each class does not

, you can find your function portal in this vein during the run phase.

A pure virtual function is equivalent to a placeholder, first in the virtual function table to occupy a position by the derived class implementation and then the real function of the pointer fill in. In addition, there is no difference from ordinary virtual functions.


11. Why cannot an abstract class be instantiated.
Pure virtual functions in abstract classes do not have specific implementations, so there is no way to instantiate them.


12. Add a const to the back of the function to understand.

Adding a const after a function is generally used in a member function of a class to indicate that the function does not modify data members;

Related Article

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.