1. In the case of inheritance, the scope of the derived class is nested in the scope of the base class. It is precisely the hierarchical nesting of the scope that enables us to directly access the members of the base class.
2. A derived class member with the same name as a base class member will be blocked from accessing the base class member. You can use the scope operator to access the blocked base class members.
3. Use a member function with the same name in the base class and derived class. The behavior is the same as that of the data member. In the scope of the derived class, the member of the derived class will be blocked. The base class members are blocked even if the function prototype is different.
4. When a base class object is called through a derived class object, the real parameter must match the version defined in the derived class. The base class function is considered only when the derived class does not define the function.
If a derived class wants to use all the overloaded versions of its own type, the derived class must either redefine all versions or one of them.
You can use using to declare the name of the base-class member function and add all the overloaded instances of the function to the scope of the derived class.
5. Name Search and inheritance
1) First, determine the static type of the object, reference, or pointer for the function call.
2) Search for a function in this class. If the function cannot be found, search for the function in the base class directly. In this way, search for the function based on the inheritance chain of the class, wait until you find the function or find the last class.
If the name cannot be found in the class or its related base class, the call is incorrect.
3) once the name is found, check the regular type and check whether the function call is legal by checking the definition.
4) assuming that the function call is valid, the compiler generates code. If a function is a virtual function and is called by reference or pointer, the compiler generates code to determine which function version to run based on the dynamic type of the object. Otherwise, the compiler generates code to directly call the function.
6. classes that contain (or inherit) one or more pure virtual functions are abstract base classes. Except for being part of the object of the derived class of the abstract base class, the object of the abstract type cannot be created.
7. Container and inheritance
Objects with inheritance relationships are saved in the container. If it is defined as a base class object, the derived class is cut. If it is defined as an object for saving the derived class, the base class object is saved as a problem (the base class object is forcibly converted to a derived class object, and the member defined in the derived class is not initialized ).
The only feasible option is to save the object pointer in the container. However, you need to manage objects and pointers. A common technology in C ++ is the cover or handle ). Use handle class storage and management class pointer.
8. There are two important design considerations for packaging the inheritance level handle:
1) Just like for any class that saves pointers, you must determine what to do with the replication control. A handle that wraps an inheritance level usually looks like a smart pointer or a value.
2) the name handle Class determines whether the handle interface is shielded or not. If the inheritance hierarchy is not blocked, you must understand and use the objects in the basic hierarchy (objects in the underlying hierarchy ).
When you do not know the type of an object, you must assign a copy of the known object to the handle class. The common method to solve this problem is to define a virtual operation for copying, which is called a clone operation, as shown in the example.
Handle Class Example
# Ifndef BASKET_H # define BASKET_H # include <iostream> # include <string> # include <set> # include <map> # include <utility> # include <cstddef> // Item sold at an undiscounted price // derived classes will define various discount strategiesclass Item_base {friend std:: istream & operator> (std: istream &, Item_base &); friend std: ostream & operator <(std: ostream &, const Item_base &); public: virtual Item_base * clone () Const {return new Item_base (* this);} public: Item_base (const std: string & book = "", double sales_price = 0.0): 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 algorithmsvirtual double net_price (std: size_t n) const {return n * price ;} // no work, bu T virtual destructor needed // if base pointer that points to a derived object is ever deletedvirtual ~ Item_base () {} private: std: string isbn; // identifier for the itemprotected: double price; // normal, undiscounted price }; // class to hold discount rate and quantity // derived classes will implement pricing strategies using these dataclass Disc_item: public Item_base {public: std: pair <size_t, double> discount_policy () const {return std: make_pair (quantity, discount);} // other members as beforedo Uble net_price (std: size_t) const = 0; // declare it as a pure virtual function Disc_item (const std: string & book = "", double sales_price = 0.0, std :: size_t qty = 0, double disc_rate = 0.0): Item_base (book, sales_price), quantity (qty), discount (disc_rate) {} protected: std: size_t quantity; // purchase size for discount to applydouble discount; // fractional discount to apply}; // discount kicks in when a specified number of copie S of same book are sold // the discount is expressed as a fraction to use to reduce the normal priceclass Bulk_item: public Disc_item {public: std: pair <size_t, double> discount_policy () const {return std: make_pair (quantity, discount);} // other members as beforeBulk_item * clone () const {return new Bulk_item (* this );} bulk_item (const std: string & book = "", double sales_price = 0.0, std: size_t qt Y = 0, double disc_rate = 0.0): Disc_item (book, sales_price, qty, disc_rate) {}// redefines base version so as to implement bulk purchase discount policydouble net_price (std :: size_t) const;}; // discount (a fraction off list) for only a specified number of copies, // additional copies sold at standard priceclass Lim_item: public Disc_item {public: lim_item (const std: string & book = "", double sales _ Price = 0.0, std: size_t qty = 0, double disc_rate = 0.0): Disc_item (book, sales_price, qty, disc_rate) {}// redefines base version so as to implement limited discount policydouble net_price (std: size_t) const; Lim_item * clone () const {return new Lim_item (* this );} std: pair <size_t, double> discount_policy () const {return std: make_pair (quantity, discount) ;}; // use counted handle class for the I Tem_base hierarchy, handle class Sales_item {// let compare use the Item_base pointer to use Item_base compare function // this friend is needed for the hidden call to print_total, friend class Basket; public: // default constructor: unbound handleSales_item (): p (0), use (new std: size_t (1 )) {}// attaches a handle to a copy of the Item_base objectSales_item (const Item_base &); // copy control members to m Anage the use count and pointersSales_item (const Sales_item & I): p (I. p), use (I. use) {++ * use ;}~ Sales_item () {decr_use () ;}sales_item & operator = (const Sales_item &); // member access operatorsconst Item_base * operator-> () const {return p ;} const Item_base & operator * () const {return * p;} private: Item_base * p; // pointer to shared itemstd: size_t * use; // pointer to shared use count // called by both destructor and assignment operator to free pointersvoid decr_use () {if (-- * use = 0) {delete p; Delete use ;}}; bool compare (const Sales_item &, const Sales_item &); // holds items being purchasedclass Basket {// type of the comparison function used to order the multisettypedef bool (* Comp) (const Sales_item &, const Sales_item &); std :: multiset <Sales_item, Comp> items; public: // useful typedefs modeled after corresponding container typestypedef std: multiset <Sales_item, Comp >:: size_type size_t Ype; typedef std: multiset <Sales_item, Comp >:: const_iterator const_iter; // workaround MS compiler bug: must explicitly pass function addressBasket (): items (& compare) {}// initialze the comparatorvoid display (std: ostream &) const; void add_item (const Sales_item & item) {items. insert (item);} size_type size (const Sales_item & I) const {return items. count (I);} double total () const; // sum of net prices For all items in the basket}; inlineSales_item: Sales_item (const Item_base & item): p (item. clone (), use (new std: size_t (1) {}// compare defines item ordering for the multiset in Basketinline bool compare (const Sales_item & lhs, const Sales_item & rhs) {return lhs-> book () <rhs-> book () ;}# endif # include <algorithm> using std: multiset; using std :: map; using std: pair; using std: size_t; using std: stri Ng; using std: ostream; using std: endl; using std: min; using std: cout; // debugging routine to check contents in a Basketvoid Basket :: display (ostream & OS) const {OS <"Basket size:" <items. size () <endl; // print each distinct isbn in the Basket along with // count of how many copies are ordered and what their price will be // upper_bound returns an iterator to the next item in the setfor (const _ Iter next_item = items. begin (); next_item! = Items. end (); next_item = items. upper_bound (* next_item) {// we know there's at least one element with this key in the Basketos <(* next_item)-> book () <"occurs" <items. count (* next_item) <"times" <"for a price of" <(* next_item)-> net_price (items. count (* next_item) <endl ;}} void print_total (ostream &, const Item_base &, size_t); // calculate and print price for given number of copies, ap Plying any discounts void print_total (ostream & OS, const Item_base & item, size_t n) {OS <"ISBN:" <item. book () // callitem_base: book <"\ tnumber sold:" <n <"\ ttotal price:" // virtual call: which version of net_price to call is resolved at run time <item.net _ price (n) <endl;} double Basket: total () const {double sum = 0.0; // holds the running total for (const_iter iter = items. begin (); Iter! = Items. end (); iter = items. upper_bound (* iter) {// we know there's at least one element with this key in the Basketprint_total (cout, * (iter-> p), items. count (* iter); // virtual call to net_price applies appropriate discounts, if anysum + = (* iter)-> net_price (items. count (* iter);} return sum;} // use-counted assignment operator; use is a pointer to a shared use countSales_item & Sales_item :: operator = (const Sales_item & rhs) {++ * rhs. use; decr_use (); p = rhs. p; use = rhs. use; return * this;} // if specified number of items are purchased, use discounted price double Bulk_item: net_price (size_t cnt) const {if (cnt> = quantity) return cnt * (1-discount) * price; elsereturn cnt * price;} // use discounted price for up to a specified number of items // additional items priced at normal, undiscounted pricedouble Lim_item: net_price (size_t cnt) const {size_t discounted = min (cnt, quantity); size_t undiscounted = cnt-discounted; return discounted * (1-discount) * price + undiscounted * price;} int main (){//... return 1 ;}
Handle Class Example
9. In the rewrite of the virtual function, the return type of the derived class must exactly match the return type of the base class instance, but there is an exception: if the base class instance of the virtual function returns a reference or pointer to the class type, then, the instance of the derived class of the virtual function can return the object of the derived class (or a pointer or reference of the class type) returned by the base class instance ).
10. When defining Relational operators, for example, when defining =, <=, the logic must be consistent.
Reference
[1] multiset usage
Http://blog.163.com/zhoumhan_0351/blog/static/399542272010396030501/