Derived classes are constructed and refactored in a certain order because they want to invoke the base class;
The order of the constructs is: base (base)-> derivation (derived), that is, the base class is constructed first, and then the derived class is constructed;
Because the base class is independent of the derived class, the object in the derived class is not invoked, so it should be generated first;
If a derived class is built before the base class, it is possible that the build fails because the base class resource cannot be invoked;
The order of the destructor is: derivation (derived)-> base (base); The derived class is released first, then the base class is released;
Because the derived class needs to release the invoked base class resource first, it should be released preferentially;
If the base class is first destructor, it is possible that some of the resources are occupied by the derived class, which may cause the destructor to fail;
The structure and the destructor order of the derived class are just the opposite;
Code:
* * * CppPrimer.cpp * * Created on:2013.11.12 * author:caroline//*eclipse cdt*/#includ E <iostream> #include <string> #include <vector> #include <memory> #include <cstddef>
;
using namespace Std;
Class Quote {public://quote () = default;
Quote () {std::cout << "This is Quote constructor" << Std::endl; } Quote (const std::string& book, double sales_price): Bookno (book), Price (Sales_price) {} St
D::string ISBN () const {return bookno} Virtual Double Net_price (std::size_t N) const {return n-price;}//virtual function//virtual ~quote () = default;
Dynamic binding destructor Virtual ~quote () {std::cout << "This is Quote destructor" << Std::endl;
} private:std::string Bookno;
Protected://protected type Double price = 0.0;
}; * More Highlights: http://www.bianceng.cnhttp://www.bianceng.cn/programming/cplus/*/
Class Disc_quote:public Quote {//abstract base class public://disc_quote () = default;
Disc_quote () {std::cout << "This is Disc_quote constructor" << Std::endl; } disc_quote (const std::string& book, double Price, std::size_t qty, double Disc): Quote (book, Price ), Quantity (qty), Discount (disc) {} double Net_price (std::size_t) const = 0; Pure virtual function Virtual ~disc_quote () override{std::cout << "This is Disc_quote destructor" << std::en
dl
} protected:std::size_t quantity = 0;
Double discount = 0.0;
};
Class Bulk_quote Final:public disc_quote {//final qualifier, cannot be inherited public://bulk_quote () = default;
Bulk_quote () {std::cout << "This is Bulk_quote constructor" << Std::endl; } bulk_quote (const std::string& book, double p, std::size_t qty, double disc): Disc_quote (Book, p, QT Y, disc) {}//using the constructor of the base class DoubLe Net_price (std::size_t cnt) const override;
Virtual ~bulk_quote () override{std::cout << "This is Bulk_quote destructor" << Std::endl;
}
}; Double Bulk_quote::net_price (std::size_t cnt) const {if (CNT >= quantity) return CNT * (1-discount) *
Price
else return CNT * price; Double Print_total (std::ostream &os, const quote& Item, std::size_t N) {double ret = item.net_
Price (n); Os << "ISBN:" << item.isbn () << "# Sold:" << n << "total due:" << ret << St
D::endl;
return ret;
int main (void) {bulk_quote* BQ = new Bulk_quote;
Delete Bq;
return 0; }
Output:
This is Quote constructor this are
Disc_quote constructor This is
Bulk_quote constructor This is
bulk_quote des Tructor this are
disc_quote destructor This is
quote destructor
Author: csdn Blog spike_king