C + + derived classes

Source: Internet
Author: User

Now suppose that a B inherits from a , sob* can be used as a *. Similarly, a b& can be used as a a&. However, a A is not necessarily a B, so a* cannot be used as a b*. In general, if a class derived has a public base class base, then we can assign a derived* to a variable of type base* without explicit type conversions. The opposite conversion, that is, from base* to derived*, must be explicit. For example:

 void   g (Manager Mm,employee ee) {Employee      * PE = &mm; //     correct: Each manager is an employee       manager* pm = &ee; //  error: Not every employee is a manager            pm ->level = 2 ; //  disaster: EE does not include level       = static_cast<manager*> (PE); //  violent conversion; it works because PE points to manager mm            pm ->level = 2 ; //  No problem: PM points to manager mm with level } 

In other words, a derived class object can be treated as its base class object if manipulated by pointers and references, and vice versa.

Using a class as a base class is equivalent to defining an (nameless) object of that class, so the class must be determined later to be used as a base class.

Virtual functions

C + + derived classes

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.