派生类的拷贝控制成员

测试代码

#include "../head.h"

class Quote {
public:
    Quote() {cout << "default constructing Quote" << endl;};
    Quote(const string &book, double sales_price) : 
        bookNo(book), price(sales_price) {
            cout << "Quote:cosntructor taking 2 parameters" << endl;
        }

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

    string isbn() const {return bookNo;}
    virtual double net_price(size_t n) const {return n * price;}
    virtual ~Quote() {cout << "destructing Quote" << endl;};
protected:
    double price = 0.0;
private:
    string bookNo;
};

class Disc_quote : public Quote {
public:
    Disc_quote() {cout << "default constructing Disc_quote" << endl;}
    Disc_quote(const string &book, double price, size_t qty, double disc) : 
        Quote(book, price), quantity(qty), discount(disc) {
            cout << "Disc_quote:constructor taking 4 parameters" << endl;
        }

    Disc_quote(const Disc_quote &q) : Quote(q) {
        cout << "Disc_quote:copy constructing Disc_quote" << endl;
    }
    Disc_quote& operator=(const Disc_quote &rhs) {
        Quote::operator=(rhs);
        cout << "Disc_quote:copy=()" << endl;
        return *this;
    }

    Disc_quote(const Disc_quote &&q) noexcept :
        Quote(move(q)) {
            cout << "Disc_quote:move constructing" << endl;
        }
    Disc_quote& operator=(const Disc_quote &&rhs) {
        Quote::operator=(move(rhs));
        cout << "Disc_quote:move=()" << endl;
        return *this;
    }

    double net_price(size_t n) const override {
        if (n >= quantity) return n * (1 - discount) * price;
        else return n * price;
    }
    ~Disc_quote() {cout << "destructing Disc_quote" << endl;}
protected: 
    size_t quantity = 0;
    double discount = 0.0;
};

void 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;
}

int main(int argc, char** argv) {
    Quote item1("hello", 10);
    Disc_quote item2("hello", 10, 5, 0.1);
    cout << "<==copy constructor test ==>" << endl;
    Disc_quote item3(item2);
    Disc_quote item4;
    item4 = item3;
    cout << "<==move constructor test ==>" << endl;
    Disc_quote item5(move(item2));
    Disc_quote item6;
    item6 = move(item5);
    cout << "<==destructing test ==>" << endl;
    return 0;
}

输出

Quote:cosntructor taking 2 parameters
Quote:cosntructor taking 2 parameters
Disc_quote:constructor taking 4 parameters
<==copy constructor test ==>
Quote:copy constructing Quote
Disc_quote:copy constructing Disc_quote
default constructing Quote
default constructing Disc_quote
Quote:copy=()
Disc_quote:copy=()
<==move constructor test ==>
Quote:move constructing
Disc_quote:move constructing
default constructing Quote
default constructing Disc_quote
Quote:move=()
Disc_quote:move=()
<==destructing test ==>
destructing Disc_quote
destructing Quote
destructing Disc_quote
destructing Quote
destructing Disc_quote
destructing Quote
destructing Disc_quote
destructing Quote
destructing Disc_quote
destructing Quote
destructing Quote

这是《C++ primer》15-26的题,主要就是为了测试派生类拷贝控制操作的行为,实验结果与预期相同

1 派生类的默认构造函数会先运行基类的默认构造函数,将基类中的数据成员初始化,然后执行自己的默认构造函数,将数据成员初始化
2 派生类使用拷贝构造函数时会先运行基类的拷贝构造函数,然后再运行自己的拷贝构造函数,对于拷贝赋值运算符、移动构造函数、移动赋值运算符也都是类似的操作
3 派生类销毁时先执行自己的析构函数,再执行基类的析构函数

转载于:https://www.cnblogs.com/Anthony-ling/p/10749980.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值