C ++: "inheritance and Derivation"

Source: Internet
Author: User

C ++: "inheritance and Derivation"
Concepts of inheritance and DerivationI. Definition1. base class (parent class): original class;
2. derived class (subclass): new class;
3. Inheritance: The derived class has the features of the base class;
4. Derivation: the process of generating classes from the base class. Similar derivation is called generalization in UML.

Ii. Syntax1. Definition of a derived class:
Class derived class name: The base class name of the Inheritance Method ..
{
...
};

Iii. Inheritance and combination1. Contact: when multiple inheritance occurs, a derived class has multiple direct base classes. The derived class is actually a combination of all base class attributes and behaviors. A derived class is an extension of the base class. Some members of the derived class come from the base class, so the derived class combines the base class. 2. Differences:
Inheritance: 1. describes the relationship between general classes and special classes;
2. "is a kind of"; combination: 1. Relationship between the whole and part of the description;
2. "is a part ";

Inheritance MethodThe inheritance methods include mutual inheritance, private inheritance, and protection inheritance.


Type compatibilityI. Features of Type compatibility1. A derived class object can be copied to a base class object;
2. The object of the derived class can initialize the reference of the base class;
3. The address of the derived class object can be assigned to the pointer to the base class;

Ii. AdvantagesIt can easily realize type conversion between the base class and the derived class, greatly reducing the burden of programming code and improving the low rate of programming.


Virtual base classI. Definition1. Format: class derived class name: virtual Inheritance Method Base class Name
2. virtual is a keyword used to declare the base class as the virtual base class of the derived class;
3. The scope of the keyword only applies to the base class that follows it;


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.