C + + interface inheritance and implementation inheritance

Source: Internet
Author: User

The so-called interface inheritance, that is, derived classes only inherit the interface of the function, that is, the Declaration, and the implementation of inheritance, is derived classes inherit the interface and implementation of the function. We're all clear about C .++there are several basic concepts, virtual functions, pure virtual functions, and non-virtual functions. Virtual function: A virtual function refers to a class of member functions you want to overload, when you use a base class pointer or reference to an inherited class object, you call a virtual function, the actual call is the version of the inherited class. The--MSDN virtual function is used to represent a relationship between a base class and a member function of a derived class. The definition of a virtual function is performed in a base class, and the declaration of a member function that needs to be defined as a virtual function is preceded by a keywordVirtualWhen a member function in a base class is declared as a virtual function, this virtual function can be redefined in one or more derived classes. When redefined in a derived class, its function prototypes, including return types, function names, number of arguments, parameter types, and the order of parameters, must be identical to the prototype in the base class. Virtual function is a form of overloading, and it is a dynamic overloaded way. Pure virtual functions: pure virtual functions are not defined in the base class, they are initialized to 0. Any class derived from a pure virtual function should provide the specific implementation of the function itself. Defining pure virtual FunctionsVirtual voidFunvoid) =0; Non-virtual function: General member function, no virtual keyword decoration. As for why these functions are defined, we can relate the functions of virtual functions, pure virtual functions and non-virtual functions to interface inheritance and implementation inheritance: Declaring a pure virtual function ( pureVirtual) is intended to allow derived classes to inherit only the function interface, which is the interface inheritance described above. Pure virtual functions are generally used in situations where it is not convenient to implement this function specifically. In other words, the base class cannot prescribe a uniform default operation for the inheriting class, but the inheriting class must also contain the function interface and implement it separately. However, in C++, we can provide a definition for pure virtual functions, except that this definition has no specific meaning for inheriting classes. Because inheriting classes still need to implement functions according to their needs. In layman's words, pure virtual function requires that its inheriting class must contain the function interface and implement it. is an interface implementation requirement for an inherited class, but does not provide the default operation, and each inheriting class must implement its own operation separately. Declares a non-pure virtual function (impureVirtual) is intended to allow an inheriting class to inherit the interface and default implementation of the function. The only difference from a pure virtual function is that it provides the default action for the inheriting class, and the inheriting class can take the default action provided by the base class without implementing its own operation. Declaring a non-virtual function (non-Virtual) is intended to enable inheriting classes to inherit function interfaces and a mandatory implementation. Compared to virtual function, non-virtual function is more strict to inheriting class, inheriting class not only inherits function interface, but also inherits function implementation. That is, a behavior is defined for an inherited class. Summary: pure virtual function: requires that the inheriting class must contain an interface and be implemented on an interface function. Virtual function: The inheriting class must contain an interface that can be implemented by itself or not, and the default implementation defined by the base class. Non-virtual functions: Inheriting classes must contain an interface, and the implementation of the base class must be used. I have heard this noun for a long time, so this is the case, our plug is the typical interface inheritancestructi_a{Virtual voidFun () =0;};classA: Publici_a { Public:        voidFun ();};voidA::fun () {} for virtual functions is the implementation of polymorphism, if you do not override virtual function, virtual function will become meaningless, rather than define a general function, but also save a pointer to the memory space

C + + interface inheritance and implementation inheritance

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.