C + + face question

Source: Internet
Author: User
Tags access properties

1. What is the object-oriented program design idea?

A: The data structure and the data structure of the method of operation to encapsulate the formation of a set of objects.



2. What is a class?

A: Classify some objects that have commonalities to form a set, the so-called class.



3. What are the two aspects of the object? What is the meaning of the distinction?

A: objects are characterized by static and dynamic characteristics.

Static characteristics refer to some attributes (member variables) that describe the object, and the dynamic feature is the behavior (member function) that the object manifests.



4. What is the meaning of the class declaration in the header file and the definition of the classes in the corresponding implementation file?

A: This can improve the efficiency of the compilation, because separate words only need to compile once the corresponding. obj file, the class is again applied to the place, this class will not be compiled again, thus greatly improving the efficiency of the compilation.



5. Define the function body of the member function within the class, which will have that property?

A: This function is automatically an inline function, where the function is replaced in the compile phase at the point where the function is called.



6. What does the member function do to differentiate the member data of different objects? Why is it able to differentiate?

A: This pointer points to the first address of the object to differentiate.



7. What are the four default functions that the C + + compiler automatically produces for a class?

Answer: default constructor, copy constructor, destructor, assignment function.



8. What are the different scenarios in which the copy constructor is called?

For:

1. When an object of the class goes to initialize another object of the class;

2. If the function's formal parameter is the object of the class, the calling function is combined with the formal parameter and the argument;

3. If the return value of the function is a class object, the function call completes when it returns.



9. What is the difference between a constructor and a normal function in form? (the function of the constructor, its declaration form to analyze)

A: A constructor is a special member function of a class that, in general, is specifically used to initialize an object member variable.

The constructor must have the same name as the class name, does not have any type, and does not return any values.



10. When do I have to rewrite the copy constructor?

A: When the constructor involves dynamic storage allocation space, you write the copy constructor yourself, and you want to make a deep copy.



11. What is the order in which constructors are called?

Answer: 1. Call the base class constructor first

2. Initializing data members in the Order of declaration

3. Finally call your own constructor.



12. What kinds of situations must be used to initialize the member list?

A: A member of a class is a constant member initialization;

A member of a class is an object member initialization, and the object does not have a parameterless constructor.

When a member of a class is a reference.



13. What is a regular object?

A: A constant object is an object that cannot modify the value of its members on any occasion.



14. What is the meaning of static functions?

A: Static private members cannot be accessed outside the class, and can be accessed through static member functions of the class;

When a class's constructor is private, it does not instantiate itself like a normal class, and the constructor can only be called by a static member function.



15. Is there any way outside the class to access the non-public members of the class?

A: Friend, inheritance, public member function.



16. What is abstract class?

A: You do not need to define an object and use it as an inherited class only as a base type.



17. What is the meaning of operator overloading?

A: The operation of data of a user-defined data type is consistent with the operation of data of the data type defined within.



18. What are the 5 operators that do not allow overloading?

For:

1.. * (Member pointer access operation symbol)

2.:: Domain operator

3. Sizeof length operation symbol

4.: Conditional operation Symbol

5.. (Member accessor)



19. Three ways of operator overloading?

A: common function, friend function, class member function.



20. Why can't a stream operator be overloaded by a member function of a class?

A: Because a member function overload through a class must be the first of the operators itself, the overload of the convection operation requires that the first argument be a stream object. So generally through friends to solve.



21. What are the differences and linkages between assignment operators and copy constructors?

A: The same point: Copy one object to another.

Different points: The copy constructor involves creating a new object.



22. In which case do you want to invoke the destructor for the class?

Answer: At the end of the object life cycle.



23. How do data sharing be achieved between objects?

A: The use of static member variables of the class to achieve data sharing between objects. Static member variables occupy their own independent space and are not private to an object.



24. What are the characteristics of a friend relationship?

Answer: One-way, non-transitive, cannot inherit.



25. What is the order in which the object members are initialized?

A: Its order is completely unaffected by their order in the initialization table, only the order in which the member objects are declared in the class.



26. What is the relationship between classes and objects?

A: A class is an abstraction of an object that is an instance of a class.



27. What are the access properties for members of a class?

Answer: Public,protected,private.



28. const char *P and char * const p; The difference

For:

If the const is on the left side of the asterisk, the const is used to decorate the variable pointed to by the pointer, that is, the pointer is constant;

If the const is on the right side of the asterisk, the const is the modifier pointer itself, which means the pointer itself is a constant.



29. Is it a parent class that writes a virtual function that can be polymorphic if the subclass overrides its function without virtual?

For:

The virtual modifier is inherited by stealth.

Virtual can be added without, the sub-class covering its function does not add virtual, but also to achieve polymorphism.



30. What does function overloading mean? How does it differ from the concept of virtual functions?

A: function overloading is a function with the same name, the compiler system in the compilation phase through the number of function parameters, parameter types, function return value to distinguish which function, that is, the implementation of static polymorphism. But remember: you can't implement function overloading just by using function return values. The virtual function is implemented in the base class by using the keyword virtual to declare that a function is a virtual function, meaning that the function may be defined in a future derived class or extended on the basis of the base class, the system can only be in the run phase to dynamically determine which function to call, So the dynamic polymorphism is realized. It embodies a portrait concept, which is implemented between the base class and the derived class.



31. Whether constructors and destructors can be overloaded, why?

A: constructors can be overloaded and destructors cannot be overloaded. Because the constructor can have more than one and can take parameters, the destructor can only have a single, and cannot take parameters.



32. How do I define and implement a member function of a class as a callback function?

For:

The so-called callback function is to pre-register the function in the system, let the system know the existence of this function, later, when an event occurs, then call this function to respond to the event.

When defining a member function of a class, add callback to the function before it is defined as a callback function, and the implementation of the function is not different from the ordinary member function.



33. How is virtual function implemented?

A: Simply put, a virtual function table is used.



34. Abstract classes do not produce instances, so there is no need for constructors. Wrong



35. You can derive a new template class from a template class, or you can derive a non-template class. Right



What code is executed before the main function is executed?

Answer: The constructor for the global object is executed before the main function.



37. When there is no life member variable and member function in a Class A, then what is the value of sizeof (a), if not 0, explain why the compiler did not let it be zero. (Autodesk)

Answer: Definitely not zero. For example, if it is 0, declare a class A[10] object array, and each object occupies a zero space, then there is no way to differentiate a[0],a[1] ... The



The difference between delete and delete []:

A: Delete will only call a destructor, and delete[] will call the destructor for each member.



39. Is the destructor of the parent class to be called when the subclass is destructor?

A: It will be called. The order of destructor calls is the destructor of the derived class after the destructor of the base class, that is, when the destructor of the base class is called, the information of the derived class is destroyed.



40. Advantages and disadvantages of inheritance.

1, class inheritance is statically defined at compile time, and can be used directly,

2, class inheritance can easily change the implementation of the parent class.

Disadvantages:

1. Because inheritance is defined at compile time, it is not possible to change the implementation inherited from the parent class at run time

2. The parent class usually defines at least part of the behavior of the subclass, and any changes to the parent class may affect the behavior of the subclass

3. If the inherited implementation is not suitable for solving the new problem, the parent class must be overridden or replaced by a more appropriate class. This dependency limits flexibility and ultimately limits reusability.



41. Explain the differences between heaps and stacks.

A: Stack (stack)-Automatically allocated by the compiler to release, store the function's parameter values, local variable values, and so on.

The heap is typically released by the programmer, and if the programmer does not release it, it may be reclaimed by the OS at the end of the program.



42. When a class's constructors and destructors are called, do they need to be called manually?

A: The constructor is automatically called when the class object is created, and the destructor is automatically called by the system at the end of the class object's lifetime.



43. When to Precompile:

A: Always use large code bodies that don't change very often.

The program consists of multiple modules, all of which use a standard set of include files and the same compilation options. In this case, you can precompile all the include files into a precompiled header.



44. What is the role of polymorphism?

A: The main two:

1. Hide the implementation details, so that the code can be modular, extension code module, implementation of code reuse;

2. Interface reuse: For classes to be inherited and derived, the correct invocation is guaranteed when using a property of an instance of any class in the family



45. What is the difference between a virtual function and an ordinary member function? can inline functions and constructors be virtual functions?

Answer: The difference: the virtual function has the virtual keyword, there is a dummy pointer and dummy function table, virtual pointer is the interface of the virtual function, and the ordinary member function is not. Inline functions and constructors cannot be virtual functions.



46. What is the order in which constructors and destructors are called? Why do destructors need to be virtual?

Answer: Constructor invocation Order: base class constructor-object member constructor-derived class constructor, the destructor is called in the reverse order of the constructor function. destructor virtual is to prevent the destruction of the incomplete, resulting in memory leaks.



What functions can be accessed by a member variable of type Private in C + +?

A: can only be accessed by member functions and friend functions in this class



48. Please state the difference between private,protect,public three types of access restrictions in a class

A: Private is a privately-owned type, accessible only to member functions in this class, protect is protected, this class and inheriting classes are accessible, public is publicly available, and any class is accessible.



49. How are member variables initialized in a class?

A: You can do this through the constructor's initialization list or the function body of the constructor.



50. When do I need to use "regular reference"?

A: If you need to use reference to improve the efficiency of the program, but also to protect the data passed to the function is not changed in the function, you should use a constant reference.

C + + face question

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.