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