freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

面向?qū)ο蟪绦虍厴I(yè)設(shè)計中英文對照文檔-文庫吧

2025-09-28 19:54 本頁面


【正文】 lying any discounts void print_total(ostream amp。os, const Item_base amp。item, size_t n) { os ISBN: () // calls Item_base::book \tnumber sold: n \ttotal price: // virtual call: which version of _price to call is resolved at run time (n) endl。 } The function39。s work is trivial: It prints the results of calling book and _price on its item parameter. There are two interesting things about this function. First, even though its second parameter is a reference to Item_base, we can pass either an Item_base object or a Bulk_item object to this function. Second, because the parameter is a reference and the _price function is virtual, the call to _price will be resolved at run time. The version of _price that is called will depend on the type of the argument passed to print_total. When the argument to print_total is a Bulk_item, the version of _price that is run will be the one defined in Bulk_item that applies a discount. If the argument is an Item_base object, then the call will be to the version defined by Item_base. C++, dynamic binding happens when a virtual function is called through a reference (or a pointer) to a base class. The fact that a reference (or pointer) might refer to either a base or a derivedclass object is the key to dynamic binding. Calls to virtual functions made through a reference (or pointer) are resolved at run time: The function that is called is the one defined by the actual type of the object to which the reference (or pointer) refers. Defining Base and Derived Classes In many ways, base and derived classes are defined like other classes we have already seen. However, there are some additional features that are required when defining classes in an inheritance hierarchy. This section will present those features. Subsequent sections will see how use of these features impacts classes and the programs we write using inherited classes. Defining a Base Class Like any other class, a base class has data and function members that define its interface and implementation. In the case of our (very simplified) bookstore pricing application, our Item_base class defines the book and _price functions and needs to store an ISBN and the standard price for the book: // Item sold at an undiscounted price // derived classes will define various discount strategies class Item_base { public: Item_base(const std::string amp。book = , double sales_price = ): isbn(book), price(sales_price) { } std::string book() const { return isbn。 } // returns total sales price for a specified number of items // derived classes will override and apply different discount algorithms virtual double _price(std::size_t n) const { return n * price。 } virtual ~Item_base() { } private: std::string isbn。 // identifier for the item protected: double price。 // normal, undiscounted price }。 For the most part, this class looks like others we have seen. It defines a constructor along with the functions we have already described. That constructor uses default arguments (Section , p. 253), which allows it to be called with zero, one, or two arguments. It initializes the data members from these arguments. The new parts are the protected access label and the use of the virtual keyword on the destructor and the _price function. We39。ll explain virtual destructors in Section (p. 587), but for now it is worth noting that classes used as the root class of an inheritance hierarchy generally define a virtual destructor. BaseClass Member Functions The Item_base class defines two functions, one of which is preceded by the keyword virtual. The purpose of the virtual keyword is to enable dynamic binding. By default, member functions are nonvirtual. Calls to nonvirtual functions are resolved at pile time. To specify that a function is virtual, we precede its return type by the keyword virtual. Any nonstatic member function, other than a constructor, may be virtual. The virtual keyword appears only on the memberfunction declaration inside the class. The virtual keyword may not be used on a function definition that appears outside the class body. Access Control and Inheritance In a base class, the public and private labels have their ordinary meanings: User code may access the public members and may not access the private members of the class. The private members are accessible only to the members and friends of the base class. A derived class has the same access as any other part of the program to the public and private members of its base class: It may access the public members and has no access to the private members. Sometimes a class used as a base class has members that it wants to allow its derived classes to access, while still prohibiting access to those same members by other users. The protected access label is used for such members. A protected member may be accessed by a derived object but may not be accessed by general users of the type. Our Item_base class expects its derived classes to redefine the _price function. To do so, those classes will need access to the price member. Derived classes are expected to access isbn in the same way as ordinary users: through the book access function. Hence, the isbn member is private and is inaccessible to classes that inherit from Item_base. protected Members The protected access label can be thought of as a blend of private and public: ? Like private members, protected members are inaccessible to users of the class. ? Like public members, the protected members are accessible to classe
點(diǎn)擊復(fù)制文檔內(nèi)容
公司管理相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖鄂ICP備17016276號-1