Differentiate interface inheritance and implementation inheritance

Source: Internet
Author: User

Using the public inheritance base class, you can divide the inherited results into two types:Function interface inheritanceAndFunction implementation inheritance.

The purpose of declaring a pure function is to make the derived class inherit only the function interface. Pure virtual function has two outstanding features:

They must be re-declared by any specific class that "inherits them", and they are generally not defined in abstract classes.

The purpose of declaring a non-pure virtual function is to let the derived class inherit the interface and default Implementation of the function.

The purpose of declaring a non-virtual function is to make the derived class inherit the function interface and a mandatory implementation.

When designing a class, declare a member function and select it with caution. Declarations of different types mean things with different fundamental meanings.

The following are common mistakes made by inexperienced designers:

1. Declare all functions as non-virtual functions. This is equivalent to a derived class that inherits the function interface and mandatory implementation, so that the derived class does not have the space for special work.

This is especially true for non-virtual destructor.

However, when designing a class that is not intended as the base class, all its member functions should be declared as non-virtual.

2. Declare all member functions as virtual. Sometimes this is correct, but sometimes some functions should not be redefined in the derived class,

These functions represent immutability in the inheritance system. Therefore, if the immutability of the class body prevails over the specicity, the function sound must be non-virtual.

 

Inspiration: when carrying out Object-oriented Inheritance, the design of the class body is crucial. A good design of the class body and its inheritance system facilitates the implementation, protection, and expansion of the system.

Therefore, when declaring a member function, pay attention to the role and purpose of the class body, so as to select a reasonable declaration method.

References: Objective C ++

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.