The road to coding--re-learning C + + (7): Write a good class with inheritance

Source: Internet
Author: User

1. Where to take note when inheriting classes

(1) When a class is a base class, the class must be defined.

(2) in a derived class, the default constructor for the base class can be called implicitly, but if the constructor for the base class has parameters, the derived class needs to call one directly. A constructor for a derived class can only describe the derived class's own member variables and the direct initialization of its own base class, which cannot directly initialize the members of the base class.

Manager::manager (conststringintint  LVL)    : Family_name (n),            // Error: No family_name statement    in Manager Department (d),             // wrong me: There is no department statement in Mangager level     (LVL)    {  //... }// Error: The constructor for the employee was not called

(3) The construction of a class object is bottom-up: The first is the base class, then the member, and finally the derived class itself. The destruction of a class object is top-down: The first is the derived class itself, then the member, and finally the base class.

(4) When a virtual function is inline, you can use an online substitution with the "::" Call for special instructions. Allows us to handle situations where a virtual function invokes another virtual function on the same object.

(5) Abstract class. The most important use is to provide an interface without exposing any implementation details. However, a pure virtual function that is not defined in a derived class is still a pure virtual function, so that the derived class is still an abstract class.

2. Realize the inheritance of the class from the instance.

(1) Let's take a look at our traditional way of inheriting classes:

//Ival_box can get an integer from a user interfaceclassival_box{protected:    intVal; intLow,high; BOOLchanged; Public: Ival_box (intllinthh) {Changed=false; Val= Low =ll; High=hh; }    Virtual intGet_value (){changed=false; returnVal; }    Virtual voidSet_value (inti) {Changed=true; Val=i; }    Virtual voidReset_value (inti) {Changed=false; Val=i; }    Virtual voidprompt{}Virtual BOOLWas_changed () const{returnchanged;}};
class Ival_slider: public ival_box{    // defining visual graphic elements for sliders, etc.
//...
Public : Ival_slider (intint); int get_value (); void prompt ();};

(2) Assuming that we need a class called "Bbwindow" to implement the graphical features, we have to rewrite the Ival_box class so that the Ival_box class inherits the Bbwindow class. But if we still need Bjwindow class, Zxwindow class and so on, we can't let the Ival_box class inherit all over again. So, the father of C + + gives me a more revealing way to inherit, and the class hierarchy is quite clear:

//The user interface should be an implementation detail that should be hidden from the user who wants to know about it//class Ival_box should not contain data//after the user interface is modified, you do not need to recompile the code of a race that uses Ival_box//Ival_box for different interfaces can coexist in our programs. classival_box{ Public:    Virtual intGet_value () =0; Virtual voidSet_value (inti) =0; Virtual voidReset_value (inti) =0; Virtual voidPrompt () =0; Virtual BOOLWas_changed ()Const=0; Virtual~Ival_box ();};

The complete hierarchy consists of our application-oriented conceptual hierarchies as interfaces, represented by derived classes:

classival_box{/*...*/}classIval_slider: Publicival_box{/*...*/}classIval_dial: Publicival_box{/*...*/}classFlashing_ival_slider: Publicival_slider{/*...*/}classPopup_ival_dial: Publicival_dial{/*...*/}

The next hierarchy is for the implementation of various graphical user interfaces, as well as derived classes:

classBbslider: Publicbbwindow{/*...*/};classCwslider: Publiccwwindow{/*...*/};classBb_ival_slider: PublicIval_slider,protectedbbsilder{/*...*/};classBb_flashing_ival_slider: PublicFlashing_ival_slider,protectedbbwindow_with_bells_and_whistles{/*...*/};classBb_popup_ival_slider: PublicPopup_ival_slider,protectedbbslider{/*...*/};classCw_ival_slider: PublicIval_slider,protectedCwslider{/*...*/};

The hierarchical structure of this class is this:

Finally, when inheriting classes, we can apply the following recommendations: Data members are best used for private use, which makes it impossible for derived classes to interfere with them. Further, the data should be placed in the derived class, in the derived class, we can define the exact data according to the specific needs, do not confuse the unrelated derived classes, for almost all cases, the protection interface should contain only functions, types and constants.

The road to coding--re-learning C + + (7): Write a good class with inheritance

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.