基类和派生类中构造函数和拷贝控制

15.26 定义Quote和Bulk_quote的拷贝控制成员,令其与合成的版本行为一致。为这些成员以及其他构造函数添加打印状态的语句,使得我们能够知道正在运行哪个程序。使用这些类编写程序,预测程序将创建和销毁哪些对象。重复实验,不断比较你的预测和实际输出结果是否相同,直到预测完全准确再结束。

Quote.h

#ifndef QUOTE_H
#define QUOTE_H
#include<iostream>
#include<string>
using namespace std;
class Quote
{
public:
    Quote()=default;
    Quote(const string &book,double sales_price):bookNo(book),price(sales_price) {}
    string isbn() const  {return bookNo;}
    virtual double net_price(size_t n) const
    { return n*price;}
    virtual ~Quote()
    {
        cout<<"Quote destruct"<<endl;
    }
    virtual void debug(size_t ) const;
    Quote(const Quote &quote):bookNo(quote.bookNo),price(quote.price)
    {
        cout<<"Quote copy construct"<<endl;
    }
    Quote(Quote &&quote):bookNo(std::move(quote.bookNo)),price(std::move(quote.price))
    {
       cout<<"Quote move construce"<<endl;
    }
    Quote& operator=(const Quote &quote)
    {
        cout<<" Quote = copy construct"<<endl;
        bookNo=quote.bookNo;
        price=quote.price;
        return *this;
    }
    Quote& operator=(Quote&& quote)
    {
        cout<<"Quote  = move construct"<<endl;
        bookNo=std::move(quote.bookNo);
        price=std::move(quote.price);
        return *this;
    }
private:
    string bookNo;
protected:
    double price=0.0;
};
class Disc_quote:public Quote
{
public:
    Disc_quote()=default;
    Disc_quote(const string &book,double price,size_t qty,double disc):Quote(book,price),quantity(qty),discount(disc) {}
    Disc_quote(const Disc_quote &disc):Quote(disc),quantity(disc.quantity),discount(disc.discount)
    {
        cout<<"Disc_quote copy construct"<<endl;
    }
    Disc_quote(Disc_quote &&disc):Quote(std::move(disc)),quantity(std::move(disc.quantity)),discount(std::move(disc.discount))
    {
        cout<<"Disc_quote move construct"<<endl;
    }
    Disc_quote& operator=(const Disc_quote &disc)
    {
        cout<<" Disc_quote = copy construct"<<endl;
        Quote::operator=(disc);
        quantity=disc.quantity;
        discount=disc.discount;
        return *this;
    }
    Disc_quote& operator=(Disc_quote &&disc)
    {
        cout<<" Disc_quote = move construct"<<endl;
        Quote::operator=(std::move(disc));
        quantity=std::move(disc.quantity);
        discount=std::move(disc.discount);
        return *this;
    }
    ~Disc_quote()
    {
        cout<<"Disc_quote destruct"<<endl;
    }
    double net_price(size_t) const=0;
protected:
    size_t quantity=0;
    double discount=0.0;
};
class Bulk_quote:public Disc_quote
{
public:
    Bulk_quote()=default;
    Bulk_quote(const string &, double , size_t , double );
    Bulk_quote(const Bulk_quote &bulk):Disc_quote(bulk)
    {
        cout<<"Bulk_quote copy construct"<<endl;
    }
    Bulk_quote(Bulk_quote &&bulk):Disc_quote(std::move(bulk))
    {
        cout<<"Bulk_quote move construct"<<endl;
    }
    Bulk_quote& operator=(const Bulk_quote &bulk)
    {
        cout<<" Bulk_quote = copy construct"<<endl;
        Disc_quote::operator=(bulk);
        return *this;
    }
    Bulk_quote& operator=(Bulk_quote &&bulk)
    {
        cout<<"Bulk_quote = move construct"<<endl;
        Disc_quote::operator=(std::move(bulk));
        return *this;
    }
    ~Bulk_quote()
    {
        cout<<"Bulk_quote destruct"<<endl;
    }
    double net_price(size_t) const override;
    void debug(size_t) const override ;
};

#endif // QUOTE_H

Quote.cpp

#include"Quote.h"
Bulk_quote::Bulk_quote(const string &book, double p, size_t qty, double disc):Disc_quote(book,p,qty,disc) {}

inline double Bulk_quote::net_price(size_t cnt) const
{
    if(cnt<=quantity)
        return cnt*(1-discount)*price;
    else
        return (cnt-quantity)*price+quantity*(1-discount)*price;
}

void Quote::debug(size_t n) const
{
    cout<<isbn()<<" "<<net_price(n)<<endl;
}

void Bulk_quote::debug(size_t n) const
{
    cout<<isbn()<<" "<<net_price(n)<<endl;
}

main.cpp

#include <iostream>
#include"Quote.h"
using namespace std;

double print_total(ostream &os,const Quote &item,size_t n)
{
    double ret=item.net_price(n);
    os<<"ISBN: "<<item.isbn()<<" # sold: "<<n<<" total due: "<<ret<<endl;
    cout<<endl;
    item.debug(n);
    return ret;
}

int main()
{
    Quote quote("Base",2);
    Bulk_quote bulk("devired",2,5,0.5);
    print_total(cout,quote,6);
    print_total(cout,bulk,6);
    return 0;
}

 

上一篇:C++11写算法之二分查找


下一篇:sublime 自动升级 脚本