C++Primer Plus 第十四章代码重用:编程练习,第2题

C++Primer Plus 第十四章代码重用:编程练习,第一题

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如: 编程练习,第一题


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

练习2.

采用私有继承而不是包含来完成编程练习1。同样,一些typedef可能会有所帮助,另外,您可能还需要考虑诸如下面这样的语句的含义:

PairArray::operator=(PairArray(ArrayInt(),ArrayInt()));
cout <<(const string &)(*this);

您设计的类应该可以使用编程练习1中的测试程序进行测试。


头文件

#pragma once

#include <iostream>
#include <valarray>

namespace n_wine
{
	using std::string;
	using std::pair;
	using std::valarray;

	typedef valarray<unsigned> UnsignedArray;

	class Wine : private string, private pair<UnsignedArray, UnsignedArray>
	{
		public:
			Wine (const string& name, unsigned years_total, unsigned vintage_years[], unsigned bottles_total[]);
			Wine (const string& name, unsigned years_total);
			const string& getLabel (void) const;
			void getBottles (void);
			void show (void) const;
			unsigned sum (void) const;

		private:
			//const string	name_;
			const unsigned	years_total_;	// 年份数量
			//	vintage_year_and_bottles_total_;	// 酿造年份与对应瓶数
	};
}

方法

#include "Wine.h"


namespace n_wine
{
	using std::cin;
	using std::cout;
	using std::endl;
	using std::cerr;

	// ====================================
	// 公有成员函数
	// ====================================

	Wine::Wine (const string& name, unsigned years_total, unsigned vintage_years[], unsigned bottles_total[])
		:	string(name),
			pair<UnsignedArray, UnsignedArray>(UnsignedArray(vintage_years, years_total), UnsignedArray(bottles_total, years_total)),
			years_total_(years_total) 
	{
		;
	}
	
	Wine::Wine (const string& name, unsigned years_total)
		:	string(name),
			pair<UnsignedArray, UnsignedArray>(UnsignedArray(years_total), UnsignedArray(years_total)),
			years_total_(years_total) 
	{
		;
	}

	const string&
	Wine::getLabel (void) const
	{
		return ((const string&)*this);
	}

	void
	Wine::getBottles (void)
	{
		cout << "Enter "<< getLabel() << " data for " << years_total_ << " year(s) ---- \n";

		auto&	vintage_year_and_bottles_total = (pair<UnsignedArray, UnsignedArray>&)*this;
		for (unsigned i = 0; i < years_total_; ++i) {
			unsigned	vintage_year;
			cout << "Enter year: ";
			cin >> vintage_year;
			if (!cin) {
				cerr << "ERREOR! " << __FILE__ << ", " << __LINE__ << endl;
				exit(EXIT_FAILURE);
			}
			vintage_year_and_bottles_total.first[i] = vintage_year;

			unsigned	bottles_total;
			cout << "Enter bottles for that year: ";
			cin >> bottles_total;
			if (!cin) {
				cerr << "ERREOR! " << __FILE__ << ", " << __LINE__ << endl;
				exit(EXIT_FAILURE);
			}
			vintage_year_and_bottles_total.second[i] = bottles_total;
		}
	}

	void
	Wine::show (void) const
	{
		cout << "Wine: " << getLabel() << '\n';

		cout << '\t' << "Year" << '\t' << "Bottles" << '\n';
		const auto&	vintage_year_and_bottles_total = (pair<UnsignedArray, UnsignedArray>)*this;
		for (unsigned i = 0; i < years_total_; ++i) {
			cout << '\t' << vintage_year_and_bottles_total.first[i] << '\t' << vintage_year_and_bottles_total.second[i] << endl;
		}
	}

	unsigned
	Wine::sum (void) const
	{
		const auto&	vintage_year_and_bottles_total = (pair<UnsignedArray, UnsignedArray>)*this;
		return (vintage_year_and_bottles_total.second.sum());
	}
}

测试方法:

参考第一题的测试方法


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值