面向对象程序毕业设计中英文对照文档内容摘要:

s derived from this class. In addition, protected has another important property: A derived object may access the protected members of its base class only through a derived object. The derived class has no special access to the protected members of base type objects. As an example, let39。 s assume that Bulk_item defines a member function that takes a reference to a Bulk_item object and a reference to an Item_base object. This function may access the protected members of its own object as well as those of its Bulk_item parameter. However, it has no special access to the protected members in its Item_base parameter: void Bulk_item::memf(const Bulk_item amp。 d, const Item_base amp。 b) { // attempt to use protected member double ret = price。 // ok: uses thisprice ret =。 // ok: uses price from a Bulk_item object ret =。 // error: no access to price from an Item_base } The use of is okay, because the reference to price is through an object of type Bulk_item. The use of is illegal because Bulk_item has no special access to objects of type Item_base. Derived Classes To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form class classname: accesslabel baseclass where accesslabel is one of public, protected, or private, and baseclass is the name of a previously defined class. As we39。 ll see, a derivation list might name more than one base class. Inheritance from a single base class is most mon and is the topic of this chapter. We39。 ll have more to say about the access label used in a derivation list in Section (p. 570). For now, what39。 s useful to know is that the access label determines the access to the inherited members. When we want to inherit the interface of a base class, then the derivation should be public. A derived class inherits the members of its base class and may define additional members of its own. Each derived object contains two parts: those members that it inherits from its base and those it defines itself. Typically, a derived class (re)defines only those aspects that differ from or extend the behavior of the base. Defining a Derived Class In our bookstore application, we will derive Bulk_item from Item_base, so Bulk_item will inherit the book, isbn, and price members. Bulk_item must redefine its _price function and define the data members needed for that operation: // discount kicks in when a specified number of copies of same book are sold // the discount is expressed as a fraction used to reduce the normal price class Bulk_item : public Item_base { public: // redefines base version so as to implement bulk purchase discount policy double _price(std::size_t) const。 private: std::size_t min_qty。 // minimum purchase for discount to apply double discount。 // fractional discount to apply }。 Each Bulk_item object contains four data elements: It inherits isbn and price from Item_base and defines min_qty and discount. These latter two members specify the minimum quantity and the discount to apply once that number of copies are purchased. Derived Classes and virtual Functions Ordinarily, derived classes redefine the virtual functions that they inherit, although they are not requried to do so. If a derived class does not redefine a virtual, then the version it uses is the one defined in its base class. A derived type must include a declaration for each inherited member it intends to redefine. Our Bulk_item class says that it will redefine the _price function but will use the inherited version of book. Public, Private, and Protected Inheritance Access to members defined within a derived class is controlled in exactly the same way as access is handled for any other class (Section , p. 432). A derived class may define zero or more access labels that specify the access level of the members following that label. Access to the members the class inherits is controlled by a bination of the access level of the member in the base class and the access label used in the derived class39。 derivation list. The base class itself specifies the minimal access control for its own members. If a member is private in the base class, then only the base class and its friends may access that member. The derived class has no access to the private members of its base class, nor can it make those members accessible to its own users. If a base class member is public or protected, then the access label used in the derivation list determines the access level of that member in the derived class:  In public inheritance, the members of the base retain their access levels: The public members of the base are public members of the derived and the protected members of the base are protected in the derived.  In protected inheritance, the public and protected members of the base class are protected members in the derived class.  In private inheritance, all the members of the base class are private in the derived class. As an example, consider the following hierarchy: class Base { public: void basemem()。 // public member protected: int i。 // protected member // ... }。 struct Public_derived : public Base { int use_base() { return i。 } // ok: derived classes can access i // ... }。 struct Private_derived : private Base { int use_base() { return i。 } // ok: derived classes can access i }。 All classes that inherit from Base have the same access to the members in Base, regardless of the access label in their derivation lists. The derivation access label c。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。