派生類的拷貝控制成員
測試代碼
#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的題,主要就是為了測試派生類拷貝控制操作的行為,實驗結(jié)果與預(yù)期相同
1 派生類的默認構(gòu)造函數(shù)會先運行基類的默認構(gòu)造函數(shù),將基類中的數(shù)據(jù)成員初始化,然后執(zhí)行自己的默認構(gòu)造函數(shù),將數(shù)據(jù)成員初始化
2 派生類使用拷貝構(gòu)造函數(shù)時會先運行基類的拷貝構(gòu)造函數(shù),然后再運行自己的拷貝構(gòu)造函數(shù),對于拷貝賦值運算符、移動構(gòu)造函數(shù)、移動賦值運算符也都是類似的操作
3 派生類銷毀時先執(zhí)行自己的析構(gòu)函數(shù),再執(zhí)行基類的析構(gòu)函數(shù)

浙公網(wǎng)安備 33010602011771號