Basic concepts of inheritance and Derivation

Source: Internet
Author: User

Object-Oriented Programming has four main features: abstraction, encapsulation, inheritance and polymorphism. Inheritance is one of the most important features I think. It can be said that inheritance is the essence of object-oriented. For example, if we already have a "horse" class, the member variables include length, height, and weight, there are also member functions such as "eat" and "run". Now we want to create new classes such as "male" and "female". Obviously, male and female have all the characteristics of horses, we don't need to write it again. We just need to add the new "gender" feature on the basis of the original class to create two new classes. Likewise, the white horse and the black horse can be derived from the category of the horse, that is, it can inherit all the features of the horse and add a new color feature. The horse is the base class of the horse, and the horse is the base class of the white horse, so we can see that the base class and the derived class are relatively speaking. 1. The declaration method of the derived class is described in the following example.

Class horse {public: void eat (); void run (); private: float height; float weight ;}; class malehorse: public horse {private: char sex; // write only the newly added members}; class wh_mhorse: public malehorse {private: char color;}; void horse: eat () {cout <"horse eats grass" <endl ;}

 

The declaration method of a derived class is class derived class name: inheritance method base class name 2. Access attribute of a derived class first, there are three inheritance Methods: public, protected, and private. When I first learned C ++, everyone had such doubts. I feel that the protected and private attributes are the same and cannot be accessed from outside the class. Here there is a difference: private is "private", regardless of the Inheritance Method, the private Members of the base class are private to the base class, and the derived class is not accessible; the base-class protected members assume different identities in the derived class according to the Inheritance Method. The following describes in detail (1) shared inheritance of the common members of the base class and Protection of Members in the derived class to maintain the original access attribute, Private Members are thrown to the base class. (2) Protect the co-members that inherit the base class and protect the members to become protected members in the derived class. (3) The Private inherited members and protected members of the base class become private members in the derived class.

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.