C + + public inheritance

Source: Internet
Author: User

Is-a, Has-a, and like-a, grouping, aggregation, and inheritance the difference between the two sets of concepts-cbk861110 column-Blog channel-Csdn.net http://blog.csdn.net/cbk861110/article/details/9028189

Public succession emphasizes the relationship of is-a, emphasizing one kind of inheritance relation, and has-a emphasizes a kind of subordinate relation.

The form of inheritance is: Class derived:public based

Public inheritance is characterized by the public and protected members of the base class as members of derived classes, which remain in their original state, while private members of the base class are still private and accessible only through the public or protected members of the base class.

Derived classes inherit the constructor of the base class, so derived classes must define their own constructors.

When using a derived class constructor:

1. You must first call the base class constructor to create the base class Derived::D erived (...): Based () {...}

2. The derived class constructor can pass the information of the base class to the base class constructor through the member initialization list.

3. Derived class constructors should initialize new data members of derived classes

when a derived class expires, the destructor for the derived class must be called before the accumulated destructor is called Derived::~derived () {... ~baesd ();}

The relationship between a derived class and a base class

1. Derived classes can only invoke the public members of the base class, and for private members only through the public or protected members of the base class

2. Base-class pointers and references can point to derived class objects without a display conversion, namely:

The ① parameter is a function of the base class reference (or pointer) that can use the base class object (the address) or the derived class object (the address) as the argument

② can initialize base class objects with derived class objects

3. A base class reference or pointer can only be used to call a base class method and cannot be used to invoke a method of a derived class

C + + public inheritance

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.