c++ primer plus 第六版第十四章编程练习

14.1

//Wine.h
#include<iostream>
#include<string>
#include<valarray>
#include<utility>

typedef std::valarray<int> ArrayInt;
typedef std::pair<ArrayInt, ArrayInt> PairArry;
class wine
{

private:
	std::string name;
	int year;
	PairArry yr_bot;
public:
	wine(const char*l, int y, const int yr[], const int bot[]);
	wine(const char*l, int y);
	wine();
	~wine() {};
	void GetBottles();
	const std::string & Lable()const;//返回值是类型名,name不是类型,不应用&name
	int sum()const;
	void show()const;

};
//Wine.cpp
#include"Wine.h"
wine::wine(const char*l, int y, const int yr[], const int bot[])//yr[],bot[]是指针不能直接用于pairarry的参数传递,它的参数是两个数组
{
	name = l;
	year = y;
	ArrayInt a(y), b(y);//将指针转换为数组作为pairarry的参数
	for (int i = 0; i < y; i++)
	{
		a[i] = yr[i];
		b[i] = bot[i];
	}
	yr_bot=std::make_pair(a, b);//应用yr_bot而不是PairArry,后者只是一个类型名
}
wine::wine(const char*l, int y)
{
	name = l;
	year = y;
	ArrayInt a(y), b(y);//a,b数组的长度为y
	yr_bot = std::make_pair(a, b);
}
wine::wine()
{
	name = "none";
	year = 0;
	ArrayInt a, b;//a,b表示长度为0的int数组
	yr_bot = std::make_pair(a, b);
}
void wine::GetBottles()
{
	std::cout << "enter " << name << "data for " << year << "year(s):\n";
	for (int i = 0; i < year; i++)
	{
		std::cout << "enter year: ";
		std::cin >> yr_bot.first[i];
		std::cout << "enter bottles for that year: ";
		std::cin >> yr_bot.second[i];
	}
}
const std::string &wine::Lable()const//返回值是类型名,name不是类型,不应用&name
{
	return name;
}
int wine::sum()const
{
	return yr_bot.second.sum();
}
void wine::show()const
{
	std::cout << "wine: " << name << std::endl;
	std::cout << "\t" << "Year" << "\t" << "Bottles\n";
	for (int i = 0; i < year; i++)
		std::cout << "\t" << yr_bot.first[i]
		          << "\t" << yr_bot.second[i]
		          << "\n";

		   
}
//main.cpp
#include <iostream>
#include "wine.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.Lable()<< ": " << more.sum() << endl;	cout << "Bye\n";
return 0;
}

14.2

//Wine2.h
#include<iostream>
#include<string>
#include<valarray>
#include<utility>

typedef std::valarray<int> ArrayInt;
typedef std::pair<ArrayInt, ArrayInt> PairArry;
class wine :private std::string ,private PairArry
{

private:
	int year;
public:
	wine(const char*l, int y, const int yr[], const int bot[]);
	wine(const char*l, int y);
	wine();
	~wine() {};
	void GetBottles();
	const std::string & Lable()const;//返回值是类型名,name不是类型,不应用&name
	int sum()const;
	void show()const;

};
//Wine2.cpp
#include"Wine2.h"
wine::wine(const char*l, int y, const int yr[], const int bot[])
	:std::string(l),PairArry(ArrayInt(y), ArrayInt(y))//必须先使用string、pairArry的构造函数初始化
{
	year = y;
	
	for (int i = 0; i < y; i++)
	{
		PairArry::first[i] = yr[i];
		PairArry::second[i] = bot[i];
	}
	 
}
wine::wine(const char*l, int y)
	:std::string(l),PairArry(ArrayInt(y),ArrayInt(y) )
{
	year = y;
	
}
wine::wine()
	:std::string("none")
{
	year = 0;
	ArrayInt a, b;//a,b表示长度为0的int数组
	PairArry(a, b);
}
void wine::GetBottles()
{
	std::cout << "enter " << (const std::string )*this << " data for " << year << " year(s):\n";//使用强制类型转换访问基类对象
	for (int i = 0; i < year; i++)
	{
		std::cout << "enter year: ";
		std::cin >> PairArry::first[i];//使用类名加作用域解析运算符访问基类方法
		std::cout << "enter bottles for that year: ";
		std::cin >> PairArry::second[i];
	}
}
const std::string &wine::Lable()const//返回值是类型名,name不是类型,不应用&name
{
	return (const std::string&)*this;
}
int wine::sum()const
{
	return PairArry::second.sum();
}
void wine::show()const
{
	std::cout << "wine: " << (const std::string)*this << std::endl;
	std::cout << "\t" << "Year" << "\t" << "Bottles\n";
	for (int i = 0; i < year; i++)
		std::cout << "\t" << Pair
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值