Exercise 15.11
1#include <iostream>2#include <string>3#include <utility>4#include <memory>5#include <vector>6 7 using namespacestd;8 9 classQuote {Ten Public: OneQuote () =default; AQuote (Const string&book,Doublesales_price): Bookno (book), Price (Sales_price) {} - Const string& ISBN ()Const{returnBookno;} - Virtual DoubleNet_price (size_t N)Const{returnn *Price ;} the Virtual voidDebug () {cout <<"Bookno:"<< Bookno <<"Price :"<< Price <<Endl;} - Virtual~quote () =default; - Private: - stringBookno; + protected: - DoublePrice =0.0; + }; A at classBulk_quote: PublicQuote { - Public: -Bulk_quote () =default; -Bulk_quote (Const string& Book,DoubleP, size_t qty,Doubledisc): Quote (Book, p), Min_qty (qty), Discount (disc) {} - DoubleNet_price (size_t N)Const; - voidDebug ()Override{cout <<"Min_qty:"<< Min_qty <<"Discount:"<< Discount <<Endl;} in Private: -size_t Min_qty =0; to DoubleDiscount =0.0; + }; - the classLim_discount: PublicQuote { * Public: $Lim_discount () =default;Panax NotoginsengLim_discount (Const string& Book,DoubleP, size_t lcount,DoubleLdis): Quote (Book, p), Lim_count (Lcount), Lim_dis (Ldis) {} - DoubleNet_price (size_t N)Const Override; the voidDebug ()Override{cout <<"Lim_count:"<< Lim_count <<"Lim_dis:"<< Lim_dis <<Endl;} + Private: Asize_t Lim_count =0; the DoubleLim_dis =0.0; + }; - DoublePrint_total (ostream& OS,Constquote&item, size_t t); $ $ intMain () - { - Quote Q1; theQuote Q2 ("C + + primer",128.00); - Bulk_quote B1;WuyiBulk_quote B2 ("C + + primer",128.00,5,0.4); thePrint_total (cout, Q1,6); -Print_total (cout, Q2,6); WuPrint_total (cout, B1,6); -Print_total (cout, B2,6); AboutLim_discount L1 ("C + + primer",128.00,Ten,0.5); $Print_total (cout, L1,9); -Print_total (cout, L1, One); - Q2.debug (); -System"Pause"); A return 0; + } the - DoublePrint_total (Ostream & OS,ConstQuote &item, size_t t) $ { the DoubleRET =Item.net_price (t); theOS <<"ISBN:"<< ITEM.ISBN () <<"# Sold:"<< T <<"Total Due:"<< ret <<Endl; the returnret; the } - in DoubleBulk_quote::net_price (size_t N)Const the { the if(N >=Min_qty) About returnn * (1-Discount) *Price ; the Else the returnn *Price ; the } + - DoubleLim_discount::net_price (size_t N)Const the {Bayi if(N <=lim_count) the returnn * (1-Lim_dis) *Price ; the Else - returnQuote::net_price (n); -}
Exercise 15.12
It is necessary, override: In the new c+=11 standard we can use the Override keyword to illustrate virtual functions in derived classes. The advantage of doing this is to make our intentions clearer and more explicit to the compiler that we want to overwrite existing virtual functions. If you define a virtual function that is the same as the name in the base class, but the parameter list is different, the function definition is legal when the override keyword is not used and the behavior is illegal after the override keyword is used, and the compiler prompts an error. Final: If we define a function as final, subsequent derived classes are not allowed to overwrite the function, or an error will be made. So declaring a member function at the same time as override and final can make our intentions clearer.
Exercise 15.13
The function of the print function is to print out all data members (including the base class), so there is a problem when the derived class overrides the virtual function of the base class, where print (OS) is parsed at run time as a call to the derived class version itself, resulting in an infinite loop;
1 void Print (ostream& os)2{3 base::p rint (OS); 4 " " << i; 5 }
Exercise 15.14
A) the first one
b) A second
c) Call the name function of the base class, BP1 is a pointer to the base class
D) A non-virtual function that causes BP2 to have only the data members of the base class, so the name function of the base class is called
e) The first print
f) The second print (which involves dynamic binding) is a virtual function, which is the one that matches the dynamic type of the object bound to the pointer or reference. Post a piece of code
1#include <iostream>2#include <string>3 using namespacestd;4 5 class Base {6 Public:7 Base() =default;8 Base(Const string&s): basename (s) {}9 Virtual voidPrint (Ostream &os) {OS <<basename;}Ten Private: One stringbasename; A }; - - classDerived: Public Base { the Public: -Derived () =default; -DerivedConst string& S,intJ):Base(s), I (j) {} - voidPrint (Ostream &os) {Base::p rint (OS); OS <<" "<<i;} + Private: - inti; + }; A at intMain () - { - BaseBobj ("Hello"); -Derived Dobj (" World",3); - Base&BR2 =dobj; - Base*BP2 = &dobj; inBp2->print (cout); - //Br2.print (cout); toSystem"Pause"); + return 0; -}
C++primer 15.3-day exercise