1. What are the differences between pointers and references?
For:
- Non-null difference: You cannot use a reference to a null value under any circumstances . A reference must always point to some object. So if you use a variable and point it to an object, but that variable may not point to any object at some point, you should declare the variable as a pointer, because you can assign a null value to the variable. Conversely, if the variable is definitely pointing to an object, such as your design does not allow the variable to be empty, then you can declare the variable as a reference. The fact that there are no references to null values means that using the referenced code is more efficient than using pointers .
- Legitimacy difference: You do not need to test the legitimacy of a reference before you use it. Instead, the pointer should always be tested to prevent it from being empty
- Modifiable differences: Another important difference between pointers and references is that pointers can be re-assigned to point to another different object. But the reference always points to the object that was specified at the time it was initialized and cannot be changed later, but the contents of the specified object can be changed.
- Application differences: In general, pointers should be used in the following situations:
- Consider the possibility of not pointing to any object (in which case you can set the pointer to null)
- Need to be able to point to different objects at different times (in which case you can change the direction of the pointer)
If you always point to an object and do not change the point once it points to an object, you should use a reference.
2. Which member variable can be shared between instances of the same class?
A: You must use static member variables to share data across all instances of a class. If you want to restrict access to static member variables, you must declare them as either protected or private. Static member variables are not allowed to store data for an object. Static member data is shared among all objects in this class,
3. Constants must be initialized or set to static in the initialization list of the constructor.
4. Destructors can be virtual, constructors are not, so why can't constructors be virtual?
A: Virtual functions take a method of virtual invocation. A virtual call is a mechanism that works with only part of the information, specifically allowing us to invoke a function that knows only the interface and does not know its exact object type. But if you want to create an object, you're bound to know the exact type of the object, so the constructor cannot be virtual.
5. Can a destructor be an inline function?
A: You can construct a class first, so that its function is an inline function. Therefore, destructors can be inline functions.
6. What is polymorphic?
A: Polymorphism can be simply summed up as "one interface, multiple methods." The function that is called is determined during the program's run. Polymorphism is the core concept of object-oriented programming.
Polymorphic, literally means "multiple shapes", polymorphism is a technique that runs when you set the parent object to be equal to one or more of its sub-objects, and after assignment, the parent object can operate differently depending on the attributes of the child object that is currently assigned to it. In short, it is a sentence: A pointer to the parent class type is allowed to assign a pointer to the child class type. Polymorphism is implemented in Object Pascal and C + + through virtual functions.
7. What is a virtual function?
A: A virtual function is a member function that is allowed to be redefined by its child class. The subclass's practice of redefining the virtual elements of the parent class becomes "overwrite" or become "overridden".
8. What are the differences between overrides and overloads?
A: Overrides (Overrides) refer to subclasses redefining the virtual function of the parent class, that is, the virtual function is always overwritten in the derived class. Overloading, however, means that multiple functions with the same name are allowed, and the parameters of the functions are different (or the number of arguments, or different types of parameters, or both).
Overloaded and polymorphic Independent. The real and polymorphic correlation is the overlay. When a subclass has redefined the virtual function of the parent class, the parent class pointer dynamically calls the function that belongs to the subclass according to the different child class pointers assigned to it, so that the function call cannot be determined during compilation (the address of the virtual function of the called subclass cannot be given). Therefore, such a function address is bound at run time (late binding). So overloading is just a language feature, independent of polymorphism and irrelevant to object orientation.
9. The role of polymorphism
Encapsulation can hide implementation details and make the code modular;
Inheritance can extend existing code modules (classes), which are intended for code reuse.
And polymorphism is to implement interface reuse.
10. Macros, inline functions, templates can be parsed at compile time, except that virtual functions must be run to be determined.
11. Friend £ º
Friend function or friend class. is a common function defined outside the class, but it needs to be described in the class, and in order to distinguish it from the member functions of the class, the keyword friend is preceded by the description. A friend is not a member function, but it can access a private member in a class.
The role of friends is to improve the efficiency of the program, but it destroys the encapsulation and concealment of the class, allowing non-member functions to access the private members of the class.
12.3 ways to inherit from a derived class:
For:
- Public inheritance
- Base class members have the same visibility of their objects as the general class and its objects, public members are visible, and other members are not visible. Protected members are the same as private members here
- The visibility of a base class member to a derived class the public and protected members of the base class are visible to the derived class, and the public and protected members of the base class remain in their original state when they are members of the derived class; Private members of the base class are not visible, private members of the base class are still private, and derived classes cannot access private members in the base class
- The visibility of base class members to derived class objects for derived classes, the public members of the base class are visible and the other members are invisible.
- Therefore, when the public inherits, the objects of the derived class can access the public members in the base class, and the member functions of the derived class can access the public and protected members in the base class
13. What is virtual inheritance? How does it differ from the general inheritance? What's the use of it?
Answer: Virtual inheritance is a unique concept in multiple inheritance. Virtual base classes occur to resolve multiple inheritance.
14. What is a virtual pointer?
A: A virtual pointer or virtual function pointer is the implementation detail of a virtual function. Each object in a class with a virtual function has a virtual pointer to the virtual function table for that class.
How do I prevent a class from being instantiated in C + +?
A: Use an abstract class, or the constructor is declared private
16. Will the constructor be declared private to prevent the compiler from generating the default copy constructor?
A: The constructor is set to private, and it does not prevent the compiler from generating the default copy constructor, which is irrelevant for two things. If we do not define a copy constructor, the compiler will synthesize one for us. Unlike the default constructor, a copy constructor is synthesized even if we define other constructors.
- Private Inheritance (private)
- Protection Inheritance (protected)
3. Can a destructor be a virtual function? (I say, yes, and for the case of multiple inheritance, the destructor is suggested with a virtual function, and explained the next, Blablabla) 4, can you explain what happens if the destructor is not used by a virtual function? At that time, I said, "If the subclass object is returned to a parent class pointer, there may be a problem, when it got stuck, the interviewer heard a smile" is not a bit around (hahaha) ", (PS: Feeling the interviewer is very kind); in fact, the answer above, when the parent pointer is deleted, if not virtual function, Causes the subclass object to not be destroyed. This knowledge point suggests to look at "effective C + +" in article 07, there is an explanation. 5. Do you understand the design pattern? (the landlord combined with a simple single-case model of a project to say a bit.) 6, see your project useful to MFC, understand the message mechanism of MFC? (This is not a very good answer, the topic has been introduced, emphasizing that the project is more involved in the algorithm part, for its message mechanism is not too much involved.) If you have MFC experience, it is recommended to review its message mechanism. 7, a program code, say the results of its operation. The code is as follows: (this question is not to elaborate, the ox guest on a lot.) )
char * getmemory (void) { char p[] = "Hello world!"; return p; }void Test () { char *str = NULL; str = GetMemory (); printf ("%s", &STR); }
8, then 7 questions, asked the heap, stack, and asked the next global variables and static variables stored in which area? When they are initialized. (You can review these points of knowledge.) 9, asked some other projects or resumes on the things written, because it is two interviewers, may project, resume and technical basis will cross to ask. But it is still relatively basic, there is no deep problem; In addition I did not let the handwriting code, other men who;
C + + face question