How to use inheritance and virtual functions in a container in C + +

Source: Internet
Author: User
Tags final inheritance

The container does not support mixed types, and derived-base conversions (Derived-base conversion) cannot be used if the derived class object is stored directly into the base class container;

Because the conversion can occur only in pointers and references, it cannot occur directly to the object, and if it is directly converted, truncation will occur (sliced down);

That is, the derived class is partially excised, leaving only the base class part; Therefore, the derived class stored in the container is exported as a virtual function of the base class part;

If you want to inherit in a container, you need to use a pointer, including a smart pointer, such as:shared_ptr<>, to output a virtual function of the overridden (override) version of the derived class;

Code:

* * * CppPrimer.cpp * * Created on:2013.11.12 * author:caroline//*eclipse cdt*/#includ E <iostream> #include <string> #include <vector> #include <memory> #include <cstddef&gt  
      
;  
      
using namespace Std;  
    Class Quote {public:quote () = default; Quote (const std::string& book, double sales_price): Bookno (book), Price (Sales_price) {} Std::strin  
    G ISBN () const {return bookno;} Virtual Double Net_price (std::size_t N) const {return n price;}//virtual function virtual ~quote () = default;  
Dynamic binding destructor private:std::string Bookno;  
Protected://protected type Double price = 0.0;  
      
};  
    Class Disc_quote:public Quote {//abstract base class public:disc_quote () = default; Disc_quote (const std::string&, double, std::size_t qty, double Disc): Quote (book, price), quant ity (qty), Discount (disc) {} double Net_price (std::size_t) const= 0;  
        Pure virtual function 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 (const std::string& book, double p, std::size_t qty, double disc): Disc_quote (Book, p, qty, disc)  
{}///Using the constructor of the base class double Net_price (std::size_t cnt) const override;  
      
}; 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;  
    the int main (void) {//Normal store cannot invoke the virtual function of the derived class std::vector<quote> basket; Basket.pusH_back (Quote ("Cppprimer", 50));  
    Basket.push_back (Bulk_quote ("Cppprimer", 50, 10,. 25));  
      
    Std::cout << "bulk_quote:" << basket.back () net_price << Std::endl;  
    Use the pointer to store, can correctly call std::vector<std::shared_ptr<quote> > sbasket;  
    Sbasket.push_back (std::make_shared<quote> ("Cppprimer", 50));  
    Sbasket.push_back (std::make_shared<bulk_quote> ("Cppprimer", 50, 10,. 25));  
      
      
    Std::cout << "bulk_quote:" << sbasket.back ()->net_price << Std::endl;  
      
return 0; }

Output:

bulk_quote:1000  
bulk_quote:750

Author: csdn Blog spike_king

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.