About the relationship between a base class and a derived class

Source: Internet
Author: User

As an object-oriented program design, inheritance is a very important feature, the core idea of object-oriented programming (object-oriented programming, OOP) is data abstraction, inheritance and dynamic binding. With the use of data abstraction, we can separate the interface of a class from implementation, use inheritance to define similar types and model their similarity, and use dynamic binding to somewhat ignore the differences of similar types and use their objects in a uniform manner. What follows is mainly about the relationship between the base class and the derived class in the inheritance.

There are two types of members in the base class that we need to be aware of, one that the base class wants derived classes to inherit from, and one that the base class wants derived classes to override. such as: base class quote and its derived class bulk_quote.

Class quote{public     :              std::string ISBN () const;              Virtual Double Net_price (std::size_t n) const;

Here the member function ISBN () is the base class that you want derived classes to inherit, and the derived class does not need to be redefined, and Net_price is expected to be overridden by a derived class, with a different implementation in the derived class. Here, the base class quote defines the member function that you want to derive overrides as a virtual function, preceded by the virtual keyword.

Then, those member functions need to be defined as virtual functions, that is, both the base class and the derived class have their own implementation, you need to define the member function in the base class as a virtual function, you want the derived class to overwrite it. Dynamic binding occurs when we call a virtual function using a reference (or pointer) to a base class. This allows the program to determine whether to call a member function of a base class, or to call a member function of a derived class, depending on the parameter. Of course, the derived class is not necessarily redefining the virtual function in the base class, and if the derived class does not overwrite the virtual function in the base class, the virtual function, like any other normal function, inherits directly from its version in the base class.

Type conversion between a derived class and a base class

In the example above, there are members in the base class quote: Bookno and price, derived classes Bulk_quote: Min_ Qty and Discount. When a derived class inherits a base class, there are two parts (four) members in the derived class: part of the Bookno, price, and part of the inheritance quote, Min_qty, discount. Because the derived class object contains parts corresponding to its base class, all of us can use the object of the derived class as a base class object, and we can bind a pointer or reference of the base class to the base class part in the derived class object. As follows:

Quote item;        Base class object Bulk_quote bulk;   Derived class object Quote *p = &item;  P point to quote object P = &bulk;           P points to the quote part of bulk quote &r = bulk; R binds to the quote part of the bulk

 Note: The fact that the derived class object contains the components corresponding to its base class is the key to inheritance.

About the relationship between a base class and a derived class

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.