C++ Primer Plus第六版 第十四章 编程练习答案

本章主要涉及C++ Primer Plus第六版的第十四章,讨论了编程练习,包括复杂题目如虚函数和模板,读者反馈存在一定的理解难度。
摘要由CSDN通过智能技术生成

这章有点难的= =。。。那个虚MI和模板把我看的有点晕QAQ

第一题

//main.cpp
#include <iostream>
#include "winec.h"

int main()
{
	using std::cin;
	using std::cout;
	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;
}

//winec.h
#ifndef WINEC_H_
#define WINEC_H_

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

class Wine
{
private:
	std::string name;
	std::pair<std::valarray<int>, std::valarray<int>> info;
public:
	Wine() {}
	Wine(const char *l, int y, const int yr[], const int bot[]);
	Wine(const char *l, int y);
	void GetBottles();
	const std::string &Label() const;
	int sum() const;
	void Show() const;
};

#endif

//winec.cpp
#include "winec.h"

Wine::Wine(const char * l, int y, const int yr[], const int bot[])
{
	name = l;
	std::valarray<int> a(y);
	std::valarray<int> b(y);
	for (int i = 0; i < y; ++i)
	{
		a[i] = yr[i];
		b[i] = bot[i];
	}
	info = std::make_pair(a, b);
}

Wine::Wine(const char * l, int y)
{
	name = l;
	std::valarray<int> a(y);
	std::valarray<int> b(y);
	info = std::make_pair(a, b);
}

void Wine::GetBottles()
{
	std::valarray<int> a(info.first.size());
	std::valarray<int> b(info.first.size());
	std::cout << "Enter " << name << " for " << info.first.size() << " year(s):" << std::endl;
	for (int i = 0; i < (int)info.first.size(); ++i)
	{
		std::cout << "Enter year: ";
		std::cin >> a[i];

		std::cout << "Enter bottles for that year: ";
		std::cin >> b[i];
	}
	info = std::make_pair(a, b);
}

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

int Wine::sum() const
{
	int ans = 0;
	for (int i = 0; i < (int)info.first.size(); ++i)
		ans += info.second[i];
	return ans;
}

void Wine::Show() const
{
	std::cout << "Wine: " << name << std::endl;
	std::cout << "\t\t" << "Year" << "\t" << "Bottles" << std::endl;
	for (int i = 0; i < (int)info.first.size(); ++i)
		std::cout << "\t\t" << info.first[i] << "\t" << info.second[i] << std::endl;
}



第二题

//第二题写的有点崩...主要是没有用typedef 或者 using 简化类型名,结果每次都好长一串类型....
//main.cpp
#include <iostream>
#include "winec.h"

int main()
{
	using std::cin;
	using std::cout;
	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;
}

//winec.h
#ifndef WINEC_H_
#define WINEC_H_

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

class Wine : private std::string, private std::pair<std::valarray<int>, std::valarray<int>>
{
public:
	Wine() {}
	Wine(const char *l, int y, const int yr[], const int bot[]);
	Wine(const char *l, int y);
	void GetBottles();
	const std::string &Label() const;
	int sum() const;
	void Show() const;
};

#endif

//winec.cpp
#include "winec.h"

Wine::Wine(const char * l, int y, const int yr[], const int bot[]) : std::string(l), std::pair<std::valarray<int>, std::valarray<int>>(std::valarray<int>(yr, y), std::valarray<int>(bot, y))
{
}

Wine::Wine(const char * l, int y) : std::string(l), std::pair<std::valarray<int>, std::valarray<int>>(
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值