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

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

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
C++Primer Plus 第十四章代码重用:编程练习,第一题


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


14.7 编程练习

题.

Wine 类有一个 sting类对象成员(参见第4章)和一个Pair 对象(参见本章);其中前者用于存储葡萄酒的名称,而后者有2个valamray<inp对象(参见本章),这两个valanay对象分别保存了葡萄酒的酿造年份和该年生产的瓶数。例如,Pair的第1个valarray对象可能为1988、1992和1996年,第2个valarray对象可能为 24、48和144瓶。Wine 最好有1个imnt 成员用于存储年数。另外,一些typedef可能有助于简化编程工作:

typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt,ArrayInt> PairArray;

这样,PairAmray 表示的是类型 Pair<std::valarray,std:valarray>。使用包含来实现 Wine 类,并用一个简单的程序对其进行测试。Wine 类应该有一个默认构造函数以及如下构造函数:

//initialize label to l,number of years to y,
//vintage years to yrl],bottles to bot[]
wine(const char*l,int y,const int yr[],const int bot[]);
//initialize label to l,number of years to y.
//create array objects of length y
Wine(const char*l int y);

Wimne 类应该有一个 GetBotles()方法,它根据 Wine 对象能够存储儿种年份(y),提示用户输入年份和瓶数。方法 Label()返回一个指向葡萄酒名称的引用。sum()方法返回 Pair 对象中第二个 valamay对象中的瓶数总和。测试程序应提示用户输入葡萄酒名称、元素个数以及每个元素存储的年份和瓶数等信息。程序将使用这些数据来构造一个 Wine 对象,然后显示对象中保存的信息。
下面是一个简单的测试程序:

// pe14-1.cpp--using Wine class with containment
#include <iostream>
#include "winec.h"
int main (void )
{
	using std::cin;
	using std::cout;
	using std::endl;
	cout <<"Enter name of wine:";
	char lab[50];
	cin.getline(lab,50);
	cout <<"Enter numberofyears:";
	int yrs;
	cin >>yrs;
	
	Wine holding(lab,yrs);//store label,years,give arrays yrs elements
	holding.GetBottles();//solicit input for year,bottle count
	holding.show();//display object contents
	const int YRs =3;
	int y[YRs]={199319951998};
	int b[YRs]={486072};
//create new object,initialize using data in arrays y and b
Wine more("Gushing Grape Red",YRS,y,b);
more.Show();
cout << "Total bottles for "<< more.Label()//use Label()method
	<<":"<< more.sum()<< endl;//use sum()method
cout << "Bye\n";return 0;
}

仅供参考

头文件定义

#pragma once

#include <iostream>
#include <valarray>

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

	class Wine
	{
		private:
			typedef valarray<unsigned> 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_;	// 年份数量
			pair<UnsignedArray, UnsignedArray>	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[])
		:	name_(name),
			years_total_(years_total), 
			vintage_year_and_bottles_total_(UnsignedArray(vintage_years, years_total), UnsignedArray(bottles_total, years_total))
	{
		;
	}
	
	Wine::Wine (const string& name, unsigned years_total)
		:	name_(name),
			years_total_(years_total), 
			vintage_year_and_bottles_total_(UnsignedArray(years_total), UnsignedArray(years_total))
	{
		;
	}

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

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

		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: " << name_ << '\n';

		cout << '\t' << "Year" << '\t' << "Bottles" << '\n';
		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
	{
		return (vintage_year_and_bottles_total_.second.sum());
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值