1. What are the three basic features of object-oriented architecture?
1. encapsulation: Abstract Objective objects into classes. Each class implements protection (private, protected, public) on its own data and methods)
2. inheritance: generalized inheritance has three implementation forms: Implementation inheritance (the ability to use the attributes and methods of the base class without additional encoding) visual inheritance (child forms use the appearance and Implementation of the parent formCode), Interface inheritance (only use properties and methods to achieve implementation lags behind subclass implementation ). The first two methods (class inheritance) and the last one (Object combination => interface inheritance and pure virtual functions) constitute two methods of function reuse.
3. polymorphism: a technology that sets a parent object to be equal to one or more other sub-objects, the parent object can operate in different ways based on the features of the sub-objects assigned to it. Simply put, it is a sentence:Assign a pointer of the subclass type to a pointer of the parent class.
2. What is the difference between overload and overried?
Common Questions. In terms of definition:
Heavy Load: Multiple allowedFunctions with the same nameAndDifferent parameter tables(Maybe the number of parameters is different, maybe the parameter type is different, or both are different ).
Rewrite: It refers to the method used by the subclass to redefine the complex class virtual function.
In terms of implementation principle:
Heavy Load : The Compiler modifies the names of functions with the same name based on different parameter tables of the function, and then these functions with the same name become different functions (at least for the compiler ). For example, there are two functions with the same name:Function func (P: integer): integer; And Function func (P: string): integer ;. The modified function name of the compiler may be as follows: Int_func , Str_func . The call to the two functions is determined between the compilers.Static. That is to say,Their addresses are bound during compilation (early binding ),Therefore,Overload is not related to Polymorphism!
Rewriting: truly related to Polymorphism. After the subclass re-defines the virtual function of the parent class, the parent class pointer is assigned to it according to different subclass pointers,Dynamic callThis function belongs to the subclass.The number of calls cannot be determined during compilation.(The address of the virtual function of the subclass called cannot be provided ). Therefore,Such function addresses are bound at runtime (late binding ).
3. What is the role of polymorphism?
There are two main reasons: 1. Hiding implementation details to modularize the Code; extending the code module to achieve code reuse; 2. Reusing interfaces: for classes to inherit and deriveTo ensure that a property of any type of instance in the family is called correctly..
4. What are the relationships and differences between New Delete and malloc free?
Answer: all are dynamic memory operations on heap. To use the malloc function, you must specify the number of bytes allocated by the memory and Cannot initialize the object. New automatically calls the object constructor. Delete calls the destructor of the object, but free does not call the destructor of the object.
5. What code will be executed before the main function is executed?
Answer: The constructor of the global object is executed before the main function.
6. Is C ++ type-safe?
Answer: No. Two different types of pointers can be forcibly converted (reinterpret cast ). C # is type-safe.
7. in which situations can I use the intialization list instead of the assignment?
Answer: when the class contains const and reference member variables, the base class constructors need to initialize the table.
8. describes the memory allocation methods and their differences?
1)Distribute from static storage area. InnerProgramThe program has been allocated during compilation, and the program exists throughout the runtime. For exampleGlobal variable, static variable.
2)Create on Stack. When executing a function,The storage units of local variables in the function can be created on the stack.When function execution ends, these storage units are automatically released. Stack memory allocation operations are embedded in the processor's instruction set.
3)Allocate from the stack,Also known as dynamic memory allocation. When the program runs, it uses malloc or new to apply for any amount of memory. The programmer is responsible for releasing the memory with free or delete. The lifetime of the dynamic memory is determined by the programmer. It is very flexible to use, but the problem is also the most.
9. Differences between struct and class
Answer: struct members are public by default, while Class Members are private by default. Struct and class are quite functional in other aspects.
Emotionally, most developers feel that classes and structures are very different. It seems that the structure is just like a bunch of open memory spaces that lack encapsulation and functionality, and the class is like a living and reliable social member. It has smart services, it has a strong encapsulation barrier and a well-defined interface. Since most people think so, there are only a few methods in your class and public data (this kind of thing exists in a well-designed system !) You should use the struct keyword. Otherwise, you should use the class keyword.
10. If a Class A does not have any member variables and member functions, what is the value of sizeof (a)? If it is not zero, explain why the compiler didn't make it zero. (Autodesk)
Answer: it must not be zero. For example, if it is zero, declare an array of Class A [10] objects, and each object occupies zero space. In this case, a [0] cannot be distinguished. A [1]… .
11. What are the advantages of const compared with # define?
Answer: 1)Const constants have data types, while macro constants do not.. The compiler can type the former.Security check. However, only character replacement is performed for the latter, and there is no type security check, and unexpected errors may occur during character replacement.
2)Some integrated debugging tools can debug const constants.But macro constants cannot be debugged.
12. What are the differences between arrays and pointers?
An array is either created in a static storage area (such as a global array) or on a stack. Pointers can point to any type of memory block at any time.
(1) Differences in modification content
Char A [] = "hello ";
A [0] = 'X ';
Char * P = "world"; // note that P points to a constant string
P [0] = 'X'; // This error cannot be found by the compiler. It is a runtime error.
(2) the sizeof operator can be used to calculate the array capacity (number of bytes ).Sizeof (P), P is the pointer to get the number of bytes of a pointer variable, rather than the memory capacity referred to by P. C ++/C language cannot know the memory capacity referred to by the pointer unless you remember it when applying for memory.Note: When an array is passed as a function parameter, the array will automatically degrade to a pointer of the same type.
Char A [] = "Hello World ";
Char * P =;
Cout <sizeof (a) <Endl; // 12 bytes
Cout <sizeof (p) <Endl; // 4 bytes
Calculate the memory size of arrays and pointers
Void func (char a [1, 100])
{
Cout <sizeof (a) <Endl; // 4 bytes instead of 100 bytes
}
13. What are the differences between Overloading, overwriting, and hiding of class member functions?
Answer:
A. Features of member functions being overloaded:
(1) the same range (in the same class );
(2) The function name is the same;
(3) parameters are different;
(4) virtual keywords are optional.
B. Override refers to the function of the derived class that overwrites the base class function. The features are as follows:
(1) different scopes (located in the derived class and the base class respectively );
(2) The function name is the same;
(3) The parameters are the same;
(4) basic functions must have virtual keywords.
C."Hide"Indicates that the function of the derived class shields the base class functions with the same name. The rules are as follows:
(1) If the function of the derived class has the same name as the function of the base class, but the parameter is different. In this case, functions of the base class will be hidden regardless of whether there is any virtual keyword (Be sure not to confuse them with overload ).
(2) If the function of the derived class has the same name and parameter as the function of the base class, but the base class function does not have the virtual keyword. At this time, the base class functions are hidden (Be sure not to confuse with overwrite)
14. There are two int variables: A and B, don't use "if", "? : "," Switch "or other judgement statements, find out the biggest one of the two numbers.
Answer: (a + B) + ABS (a-B)/2
15. How do I print the file name of the current source file and the current row number of the source file?
Answer:
Cout <_ file __;
Cout <__line __;
_ File _ and _ line _ are pre-defined macros of the system, which are not defined in a file but defined by the compiler.