Examples of inheritance and polymorphism for C + + classes

Source: Internet
Author: User
Tags access properties

Examples of inheritance of classes:

As an example of the production of the above dynamic Milky Way galaxy, suppose we define a class of stars as follows:

classStar{ Public: Star () {}~Star () {}      voidInit ();     voidMove ();  protected:     voidDraw ();     voidNewpos ();     voidRemove ();      Doublem_x =0;     intm_y;     DoubleM_step;     intM_color; };  voidStar::init () {     if(m_x = =0)     {m_x = rand ()%Screen_width;     }     Else     {m_x =0;     }m_y = rand ()%Screen_height;M_step = (rand ()% the) /1000.0+1;M_color = (int) (M_step *255/6.0+0.5);//the faster the speed, the brighter the colorM_color =RGB (M_color, M_color, m_color); }  voidStar::move () { Remove ();  Newpos ();  Draw (); }  voidStar::D Raw () {Putpixel ((int) m_x, m_y, m_color); }  voidStar::newpos () {M_x + =M_step;     if(M_x >screen_width)          This-Init (); }  voidStar::remove () {Putpixel ((int) m_x, M_y,0); }

Next we are asked to make a rectangular star what we should do, in fact, the rectangular star and the above difference is the draw () and Romove () These two functions, so we can use the method of class inheritance, and then use the same function name override method to write the class, there are three ways to inherit the following table is shown:

Derivation method Public members of the base class Protected members of the base class Private member of base class Summary of changes in access properties caused by derivation
Private derivation becomes Private member becomes Private member Not visible A non-private member in a base class becomes a private member in a derived class
Protected derivation Become a protected member becomes Private member Not visible Non-private members in a base class have a lower level of access properties in the derived class
Public derivation is still public member Still a member of protected Not visible The Access property of a non-private member in a base class in a derived class remains the same

So here we take the public inheritance, just change the class method in it (here we note that the data in the base class is not private type, because if the private class is not inherited in any way it is not callable, even if it is overloaded with a function with the same name):

 classRectstar: PublicStar {  Public: Rectstar () {}~Rectstar () {}      voidMove ()     { Remove (); Newpos (); Draw ();     }  protected:     voidDraw ();     voidRemove (); };   voidRectstar::D Raw () { Setfillcolor (m_color);FillRectangle (m_x, m_y, m_x +3, M_y +3); }  voidRectstar::remove () {Clearrectangle (m_x, m_y, m_x +4, M_y +3);}

Examples of inheritance and polymorphism for C + + classes

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.