C++ Primer Plus(第六版) 第十四章课后编程答案

本文提供了C++ Primer Plus第六版第十四章的课后编程题目答案,包括1到5题的详细解答,参考了CSDN上的相关文章资源。
摘要由CSDN通过智能技术生成

参考链接:https://blog.csdn.net/weixin_41882882/article/details/82118401

https://blog.csdn.net/u012175089/article/details/53914092

1.

//头文件,类的声明
#ifndef WINE_H_
#define WINE_H_

#include<iostream>
#include<string>
#include<valarray>

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

class Wine
{
private:
	std::string label;
	PairArray number;
	int yearnumber;
public:
//Wine(){}
	Wine(const char * l, int y, const int yr[], const int bot[]);
	Wine(const char * l, int y);
//~Wine(){}

	void GetBottles();
	void Show();
	const std::string & Label();
	int sum();

};

#endif
#include "Wine.h"
#include<iostream>
#include<string>

using std::cin;
using std::cout;
using std::string;
using std::endl;

Wine::Wine(const char * l, int y, const int yr[], const int bot[])
{
	label = l;
	yearnumber = y;
	ArrayInt a(y);
	ArrayInt b(y);
	for (int i = 0; i < y; i++)
	{
		a[i] = yr[i];
		b[i] = bot[i];
	}
	number = std::make_pair(a, b);
}

Wine::Wine(const char * l, int y)
{
	label = l;
	yearnumber = y;
	ArrayInt a(y);
	ArrayInt b(y);
	number = std::make_pair(a, b);
}

void Wine::GetBottles()
{
	cout << "Enter" << label << "data for" << yearnumber << "year(s):\n";
	number.first.resize(yearnumber);
	number.second.resize(yearnumber);
	for (int i = 0; i < yearnumber; i++)
	{
		cout << "Enter year:";
		cin >> number.first[i];
		cout << "Enter bottles for that year:";
		cin >> number.second[i];
	}

}

void Wine::Show()
{
	cout << "Wine:" << label << endl;
	cout << " Year Bottles" << endl;
	for (int i = 0; i < yearnumber; i++)
	{
		cout << " " << number.first[i]
			<< " " << number.second[i] << endl;

	}

}

const std::string & Wine::Label()
{
	return label;
}

int Wine::sum()
{
	int count = 0;
	for (int i = 0; i < yearnumber; i++)
		count += number.second[i];
	return count;
}
//书中的测试程序
#include<iostream>
#include"Wine.h"

int main(void)
{
	using std::cout;
	using std::cin;
	using std::endl;

	cout << "Enter name of Wine:";
	char lab[50];
	cin.getline(lab, 50);
	cout << "Enter number of years:";
	int yrs;
	cin >> yrs;

	Wine holding(lab, yrs);
	holding.GetBottles();
	holding.Show();

	const int YRS = 3;
	int y[YRS] = { 1993,1995,1998 };
	int b[YRS] = { 48,60,72 };

	Wine more("Gushing Grape Red", YRS, y, b);
	more.Show();
	cout << "Total bottles for" << more.Label()
		<< ":" << more.sum() << endl;
	cout << "Bye\n";
	return 0;
}

2.

//头文件,类的声明
#ifndef WINE_H_
#define WINE_H_

#include<iostream>
#include<string>
#include<valarray>

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

class Wine:private std::string, private PairArray
{
private:
	int yearnumber;
public:
//Wine(){}
	Wine(const char * l, int y, const int yr[], const int bot[]);
	Wine(const char * l, int y);
//~Wine(){}

	void GetBottles();
	void Show();
	const std::string & Label();
	int sum();

};

#endif
#include "Wine.h"
#include<iostream>
#include<string>
#include<cstdlib>

using std::cin;
using std::cout;
using std::string;
using std::endl;

Wine::Wine(const char * l, int y, const int yr[], const int bot[])
{
	(string &)(*this) = l;
	yearnumber = y;
	ArrayInt a(y);
	ArrayInt b(y);
	for (int i = 0; i < y; i++)
	{
		a[i] = yr[i];
		b[i] = bot[i];
	}
	(PairArray &)(*this) = std::make_pair(a, b);
}

Wine::Wine(const char * l, int y)
{
	(string &)(*this) = l;
	yearnumber = y;
	
}

void Wine::GetBottles()
{
	cout << "Enter " << (string &)(*this) << " data for " << yearnumber << " year(s):\n";
	PairArray& number = (PairArray&)(*this);
	number.first.resize(yearnumber);
	number.second.resize(yearnumber);
	for (int i = 0; i < yearnumber; i++)
	{
		cout << "Enter year:";
		cin >> number.first[i];
		cout << "Enter bottles for that year:";
		cin >> number.second[i];
	}

}

void Wine::Show()
{
	cout << "Wine:" << (string&)(*this) << endl;
	cout << " Year Bottles" << endl;
	PairArray & number = (PairArray&)(*this);
	for (int i = 0; i < yearnumber; i+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值