Define base classes and derived classes

Source: Internet
Author: User

Define base class

For the base class, we need to remember that the class of the root node in the inheritance relationship usually defines a virtual destructor.

A base class usually defines a virtual destructor, even if the function does not perform any actual operations.

 

Member functions and inheritance

A derived class can inherit the members of its base class or redefine the virtual functions in the base class. In other words, a derived class needs to provide its own new definition for these operations to overwrite the old definition inherited from the base class.

In C ++, the base class must distinguish its two member functions: one is the function that the base class wants its derived class to overwrite; the other is the function that the base class wants the derived class to inherit directly without changing. For the former, the base class defines it as a virtual function ). When we call a virtual function using a pointer or reference, the call will be dynamically bound. Depending on the type of the object to which the reference or pointer is bound, the call may run the version of the base class or the version of a derived class.

The base class dynamically binds the function execution by adding the keyword virtual before the declaration statement of its member functions. Non-static functions other than constructors can be virtual functions. The keyword virtual can only appear before the declaration statement inside the class, but cannot be used for Function Definition outside the class. If the base class calls a function as a virtual function, the function is implicitly a virtual function in the derived class.

If a member function is not declared as a virtual function, its parsing process occurs during compilation rather than runtime.

 

Access Control and inheritance

A derived class can inherit the Members defined in the base class, but the member functions of the derived class do not necessarily have the permission to access the members inherited from the base class. Like the code of other base classes, a derived class can access public members rather than private members. However, in some cases, the base class also has such a member. The base class wants its derived class to have the permission to access the Member and prohibit access by other users. We use protected (protected) Access operators to describe such members.

 

Define a derived class

Define base classes and 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.