C + + face question (4-6)

Source: Internet
Author: User

Title: (4)

What is the difference between overloading, overriding, and hiding a class member function?

Answer:

A. Features that are overloaded with member functions:

    • The same range (in the same class);
    • The function has the same name;
    • different parameters;
    • The virtual keyword is optional.

B. Overwrite refers to a derived class function overriding a base class function, characterized by:

    • Different ranges (in the derived class and the base class, respectively);
    • The function has the same name;
    • the same parameters;
    • The base class function must have the virtual keyword.

C. " Hide refers to a function of a derived class that masks a base class function with the same name as the following rule:

    • If a function of a derived class has the same name as a function of a base class, the parameters are different. At this point, the function of the base class is hidden, regardless of the virtual keyword (Note that it is not confused with overloading).
    • If a function of a derived class has the same name as a 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 (be careful not to confuse the overlay)

Title (5)

Please describe the polymorphism in C + + and explain the virtual function mechanism

Answer:

Polymorphism can be simply summed up as "one interface, many methods", the program at runtime to determine the function of the call, it is the core concept of object-oriented programming domain.
C + + polymorphism is implemented by virtual functions, which allow subclasses to redefine member functions.
The real difference between polymorphic and non-polymorphic is whether the function address is early binding or late binding. If a function is called, the call address of the function can be determined during compiler compilation, and the production code is static, which means that the address is early bound. If the address of a function call cannot be determined during the compiler, it needs to be determined at run time, which is a late binding.
The role of polymorphism is for interface reuse.

Virtual functions are implemented by a virtual function table (virtual table).
A virtual function table is actually an array of function pointers, each of which occupies one position in the array. A class has only one virtual function table, regardless of how many instances it has.
The derived class has its own virtual function table, and the virtual function table of the derived class has the same function order as the virtual function table of the base class (that is, it contains the base class virtual function table), and the virtual function with the same name is placed in the same position on the two array (that is, the virtual function of the derived class overrides the virtual function
When you create an instance of a class, the compiler also adds a vptr field to the memory layout of each instance that points to the virtual function table of this class, which exists in the first place in the object instance. By these means, the compiler will rewrite this call when it sees a virtual function call!
More detailed analysis

Title (6)

Describe the format, benefits, and rules that you need to follow to return a value type of reference as a function

Answer:

Format:

<!-- lang: cpp -->类型标识符 &函数名(形参列表及类型说明){   //函数体   }

Benefit: No copy of the returned value is generated in memory.

Rules:

    • You cannot return a reference to a local variable. The main reason is that the local variable is destroyed after the function returns, so the returned reference becomes a "no" reference, and the program goes into an unknown state.
    • You cannot return a reference to the memory that is allocated inside the function. For example, a reference returned by a function is only present as a temporary variable, not given an actual variable, and the space pointed to by the reference (allocated by new) cannot be freed, causing a memory leak.
    • You can return a reference to a class member, but preferably a const. If other objects can get a very good reference (or pointer) to the property, the simple assignment of that property will break the integrity of the business rules (that is, break encapsulation).
    • The stream operator and assignment operator overloads must return a reference. To support continuous use, the return value should be a reference that still supports both operators.
    • +-*/arithmetic character overload cannot return a reference.

C + + face question (4-6)

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.