The inheritance of VC + +

Source: Internet
Author: User

I. Derivation of inheritance

Many things in nature have many similarities, such as Between man and ape, between the train and the car, between cannon and machine gun, there are obviously many similarities, but there are many differences between them. C + + solves the "similar but different" problem by allowing a class to inherit its properties and behavior from one or more other classes (here called the base class), see the following example:

// --------------------------------------------------- class printeddocument{// member list }; // the book class derives from the Printeddocument class. class  Public printeddocument{// member list }; // ---------------------------------------------------



We call classes inherited from other classes as derived classes, and a derived class itself can be inherited by other classes. The inherited members must be accessed in an appropriate manner. In order to guarantee the security of the base class data encapsulation, derived classes, regardless of how they are inherited, cannot directly access the private zone members of the base class. The syntax format for a derived class is:
Class name:< access rights > base class List {class definition Body};
The base classes in the list of base class names are separated by commas, and access permissions can be public or private, which represent two different ways of inheriting: the member properties inherited from public are the same as the properties in their base class; The member properties that are inherited by private will all become private properties. The protected zone members mentioned earlier are exactly the same as the private zone members, the only difference being that when derived, the members of the protected zone can be accessed directly by the derived class, which is visible to the derived class. For example:

//---------------------------------------------------classdocument{ Public:    Char*Name; voidprintnameof ();};//---------------------------------------------------voidDocument::P rintnameof () {cout<<Name<<"\ n";}//---------------------------------------------------classBook: Publicdocument{ Public: Book (Char*name,LongPageCount); Private:    LongPageCount;};//---------------------------------------------------//the constructor of the book classBook::book (Char*name,LongPageCount) {Name=New Char[Strlen (name) +1];    strcpy (name, name); PageCount=PageCount;}//---------------------------------------------------

Two. member override issues derived from class derivation

In inheritance, a derived class contains all the base class members and joins its own new members, so derived classes can access any member of the base class based on the member access mechanism at the time of derivation (unless those members are redefined in the derived class). When a member of a base class is redefined in a derived class, it is possible to force a call to a base class member by using the scope operator "a". In the above example, if the printnameof function is redefined in book, and the Printnameof function of the base class is called, it can only be forced by the scope operator "", as follows:

//---------------------------------------------------classBook: Publicdocument{ Public: Book (Char*name,LongPageCount); Private:    LongPageCount;};//---------------------------------------------------voidBook :: Printnameof () {cout<<"Name of book:"; Document:: Printnameof ();}

The inheritance of VC + +

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.