"C + + Primer Plus" 13th class succession notes

Source: Internet
Author: User

Class inheritance defines a new (derived class) by using an existing class (the base class), making it possible to modify the programming code as needed. A common inheritance establishes a is-a relationship, which means that the derived class object should also be a base class object. As part of the IS-A model, derived classes inherit the base class's data source and most methods, but do not inherit the base class's constructors, destructors, and assignment operators. Derived classes can access the public and protected members of the base class directly, and can access the private members of the base class through the common methods and protection methods of the base class. You can add data members and methods to a derived class, and you can use derived classes as base classes for further development. Each derived class must have its own constructor. When a program creates a derived class object, the constructor of the base class is called first, and then the constructor of the derived class is called, and when the program deletes the object, the destructor for the derived class is called first and then the accumulated destructor is called.
If you want to use a class as a base class, you can declare members to be protected, not private, so that derived classes will have direct access to those members. However, using private members can often reduce the likelihood of programming problems. If you want a derived class to redefine a method of a base class, use the keyword virtual to declare it as virtual. This allows objects accessed through pointers or references to be handled according to the type of the image, rather than by the type of reference or pointer. In particular, the cumulative destructor should usually be virtual.
Consider defining an ABC: only the interface is defined, not the implementation. For example, you can define an abstract class shape, and then use it to derive in addition to specific shape classes, such as circle and square. ABC must contain a pure virtual method, which can be preceded by a good portion of the declaration with the = Zero Declaration pure virtual method.
Virtual double area () const = 0;
You do not necessarily have to define pure virtual methods. For a class that contains a pure virtual member, you cannot use it to create an object. A pure virtual method is used to define a generic interface for a derived class.

"C + + Primer Plus" 13th class succession notes

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.