Pure virtual Functions in C + + (pure virtual) and abstract base class (abstract base Class)

Source: Internet
Author: User
Tags abstract define final

A pure virtual function (pure virtual) is a method in a base class, just a declaration, not a definition, is a generalization concept (general concept);

is to put the corresponding virtual function, the end of the Add "= 0", the virtual function becomes a pure virtual function, you can not add a definition;

If it is another virtual function, it must be defined (define), even if it is not used;

A base class that contains pure virtual functions, an abstract base class (abstract base class) that cannot be defined (object) and can only be used as an inheritance define;

Code:

* * * CppPrimer.cpp * * Created on:2013.11.12 * author:caroline//*eclipse cdt*//* More highlights: http: www.bianceng.cnhttp://www.bianceng.cn/programming/cplus/*/#include <iostream> #include <string> #  
      
Include <cstddef> 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;  
    int main (void) {Quote Q ("Cppprimer", 99); Bulk_quote BQ ("Cppprimer", 99, 10, 0.5);  
    quote& RQ = BQ;  
    std::size_t n = 20;  
    Std::cout << "We want to buy" << N << "books." << Std::endl;  
    Std::cout << "Quote net_price:" << q.net_price (n) << Std::endl;  
    Std::cout << "Bulk_quote net_price:" << bq.net_price (n) << Std::endl;  
      
    Std::cout << "R Quote net_price:" << rq.net_price (n) << Std::endl;  
    Dynamic binding Quote Base ("Effectivecpp", 50);  
    Print_total (cout, base, 10);  
    Bulk_quote derived ("Effectivecpp", 50, 5,. 19);  
      
    Print_total (cout, derived, 10);  
    Copy-assignment operation, derived class-> base = derived;  
      
    Std::cout << "Net_price:" << base.net_price << Std::endl; Double undiscounted = derived. Quote::net_price (20);  
    Derived classes use the virtual functions of the base class Std::cout << "undiscounted:" << undiscounted << Std::endl;  
      
return 0; }

Author: csdn Blog spike_king

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.