派生类的拷贝控制成员

测试代码

#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 派生类销毁时先执行自己的析构函数,再执行基类的析构函数

posted @ 2019-04-22 14:36  start-from-ling  阅读(165)  评论(0)    收藏  举报