Effective C + + clause 34 distinguishes between interface inheritance and implementation inheritance

Source: Internet
Author: User

1. There are three main types of inheritance for function members in C + +:

Inherit only the interface, do not inherit the implementation;

At the same time inherit the interface and implementation, while allowing the overwrite implementation;

Interfaces and implementations are inherited, and implementations are not allowed to overwrite.

2. For public inheritance, the interface of the member function should always be inherited (due to the existence of the is-a relationship), where:

The purpose of the Pure-virtual function is simply to make the derived class inherit the function interface;

The purpose of the impure-virtual (virtual but non-pure virtual) function is to allow both derived classes to inherit interfaces and implementations, and to override implementations.

The purpose of the Non-virtual function is to make the interface of the derived class inherit the function and a mandatory implementation. (If you want to override it, it should be set to the virtual function before)

In public inheritance, the type of the base class member function should follow the above principle definition.

3. Virtual functions allow both inheritance and overriding implementations to be risky, for example:

class base{public:    virtual Fun () {...}    ... Private :    ...} class Derived: public base{public    ... Private :    ...}
View Code

Derived forget to define your own fun function, so by default inherits the fun function of base, which is probably the result that the programmer does not want, in order to prevent the following result, you can set the fun function to the Protect function and named Defaultfun to indicate that it is the default implementation. To use the default implementation, the call Defaultfun function is displayed in fun, as follows:

 class   base{ public  :  virtual  void  Fun () =0      void   Defaultfun () {...} ... private  : ...} Derived:  public   base{ public  :  virtual  void   fun{Defaultfun (); }... private     : ...} 
View Code

Since the fun function is set to pure-virtual in base, derived must provide its own definition, Instead, it can proactively use the Defaultfun function provided by base. This allows the function to inherit only the interface through the pure-virtual fun function, and provides a choice using the default version through the Non-virtual defaultfun function. . (The Defaultfun is set to protect to improve the packaging of the highest possible)

In addition to using the above method of separating the interface from implementation, you can also use the Pure-virtual function fun for base to provide a way to define it, namely:

class base{public:    virtualvoid fun () =0;    ... Private :    ...} void Base::fun () {    ...}
View Code

Derived must implement the fun function, but if you want to use the default implementation, you can define it as follows:

class Derived: public base{public:    virtualvoid Fun () {        Base::fun ();    }    ... Private :    ...}
View Code

Effective C + + clause 34 distinguishes between 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.