导航

C++primer 练习15.26

Posted on 2016-04-17 18:18  CSU蛋李  阅读(192)  评论(0编辑  收藏  举报

    定义Quote和Bulk_Quote的拷贝控制成员,令其与合成的版本行为一致。为这些成员以及其他构造函数添加打印状态的

语句,使得我们能够知道正在运行哪个程序。使用这些类编写程序,预测程序将创建和销毁哪些对象。重复实验,不断比较你

的预测和实际输出结果是否相同,直到预测完全准确再结束。

 


// 15_15.cpp : 定义控制台应用程序的入口点。
//

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

class Quote
{
public:
    Quote() = default;
    Quote(const string &book, double p) :bookNo(book), price(p) { cout << "Quote(const string&,double)" << endl; }
    Quote(const Quote &);
    Quote& operator=(const Quote &);
    Quote(Quote &&)noexcept;
    Quote& operator=(Quote &&)noexcept;
    virtual ~Quote() { cout << "~Quote()" << endl; }
    const string& isbn()const { return bookNo; }
    virtual double net_price(size_t n)const
    {
        return n*price;
    }
private:
    string bookNo;
protected:
    double price = 0.0;
};

Quote::Quote(const Quote &rhs)
{
    cout << "Quote::Quote(const Quote &)" << endl;
    bookNo = rhs.bookNo;
    price = rhs.price;
}

Quote& Quote::operator=(const Quote &rhs)
{
    cout << "Quote::operator=(const Quote &)" << endl;
    bookNo = rhs.bookNo;
    price = rhs.price;
    return *this;
}

Quote::Quote(Quote &&rhs)noexcept
{
    cout << "Quote::Quote(Quote &&)" << endl;
    bookNo = move(rhs.bookNo);
    price = move(rhs.price);
}
Quote& Quote::operator=(Quote &&rhs)noexcept
{
    cout << "Quote::operator=(Quote &&)" << endl;
    bookNo = move(rhs.bookNo);
    price = move(rhs.price);
    return *this;
}

class Dis_Quote :public Quote
{
public:
    Dis_Quote() = default;
    Dis_Quote(const string &book,double p,size_t n,double d):Quote(book,p),quantity(n),discount(d){
        cout << "Dis_Quote(const string&,double,size_t,double)" << endl;
    }
    Dis_Quote(const Dis_Quote &);
    Dis_Quote& operator=(const Dis_Quote &);
    Dis_Quote(Dis_Quote &&)noexcept;
    Dis_Quote& operator= (Dis_Quote &&)noexcept;
    virtual ~Dis_Quote()override
    {
        cout << "~Dis_Quote()" << endl;
    }
    virtual double net_price(size_t n)const = 0;
protected:
    size_t quantity = 0;
    double discount = 0.0;
};

Dis_Quote::Dis_Quote(const Dis_Quote &rhs):Quote(rhs)
{
    cout << "Dis_Quote::Dis_Quote(const Dis_Quote &)" << endl;
    quantity = rhs.quantity;
    discount = rhs.discount;
}

Dis_Quote& Dis_Quote::operator=(const Dis_Quote &rhs)
{
    Quote::operator=(rhs);
    cout << "Dis_Quote::operator=(const Dis_Quote &)" << endl;
    quantity = rhs.quantity;
    discount = rhs.discount;
    return *this;
}

Dis_Quote::Dis_Quote(Dis_Quote &&rhs)noexcept :Quote(move(rhs))
{
    cout << "Dis_Quote::Dis_quote(Dis_Quote &&)" << endl;
    quantity = move(rhs.quantity);
    discount = move(rhs.discount);
}

Dis_Quote& Dis_Quote::operator=(Dis_Quote &&rhs)noexcept
{
    Quote::operator=(move(rhs));
    cout << "Dis_Quote::operator=(Dis_Quote &&)" << endl;
    quantity = move(rhs.quantity);
    discount = move(rhs.discount);
    return *this;
}

class Bulk_Quote :public Dis_Quote
{
public:
    Bulk_Quote() = default;
    Bulk_Quote(const string &book,double p,size_t n,double d):Dis_Quote(book,p,n,d){
        cout << "Bulk_Quote(const string &,double,size_t,double)" << endl;
    }
    Bulk_Quote(const Bulk_Quote &);
    Bulk_Quote& operator=(const Bulk_Quote &);
    Bulk_Quote(Bulk_Quote &&)noexcept;
    Bulk_Quote& operator=(Bulk_Quote &&)noexcept;
    virtual ~Bulk_Quote()override
    {
        cout << "~Bulk_Quote()" << endl;
    }
    virtual double net_price(size_t n)const override
    {
        if (n > quantity)
            return n*(1 - discount)*price;
        else
            return n*price;
    }
};

Bulk_Quote::Bulk_Quote(const Bulk_Quote &rhs):Dis_Quote(rhs)
{
    cout << "Bulk_Quote::Bulk_Quote(const Bulk_Quote&)" << endl;

}

Bulk_Quote& Bulk_Quote::operator=(const Bulk_Quote &rhs)
{
    Dis_Quote::operator=(rhs);
    cout << "Dis_Quote& operator=(const Dis_Quote &);" << endl;
    return *this;
}

Bulk_Quote::Bulk_Quote(Bulk_Quote &&rhs)noexcept :Dis_Quote(move(rhs))
{
    cout << "Bulk_Quote::Bulk_Quote(Bulk_Quote &&)" << endl;
}

Bulk_Quote& Bulk_Quote::operator=(Bulk_Quote &&rhs)noexcept
{
    Dis_Quote::operator=(move(rhs));
    cout << "Bulk_Quote::operator=(Bulk_Quote &&)" << endl;
    return *this;
}

class Exercise_Quote :public Dis_Quote
{
public:
    Exercise_Quote() = default;
    Exercise_Quote(const string &book,double p,size_t n,double d):Dis_Quote(book,p,n,d){}
    virtual double net_price(size_t n)const override
    {
        if (n <= quantity)
            return n*(1 - discount)*price;
        else
            return quantity*(1 - discount)*price + (n - quantity)*price;
    }
};

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;
    return ret;
}

int main()
{

    //Quote q1("lidandan", 5);
    //Bulk_Quote q2("lidandan", 5, 9, 0.1);
    //Exercise_Quote q3("lidandan", 5, 9, 0.1);
    ////Dis_Quote q4;
    //print_total(cout, q1, 10);
    //print_total(cout, q2, 10);
    //print_total(cout, q3, 10);

    Bulk_Quote q1("test", 5, 9, 0.1);
    Bulk_Quote q2=move(q1);
    q2 = { "test",5,9,0.1 };
    return 0;
}