您的位置:首页 > 编程语言 > C语言/C++

C++primer plus第六版课后编程题答案14.2

2014-04-24 19:15 483 查看
Pair.cpp不变

Wine2.cpp

//说实话,我之前用的不是包含,所以不太懂这题的意思
//难道说是要定义一个Wine的父类,然后私有派生一个具体的Wine派生类?
//好吧,我姑且就这么理解了
//难道说是继承Pair?实在是有点不可思议啊,好吧,我把它当成是继承Pair了,估计是这个意思
//要你继承那个Pair<valarray<>,valarray<> >这个属性了
#include <iostream>
//#include <vector>
#include <valarray>
#include <string>
#include "Pair.cpp"
using namespace std;
typedef valarray<int> ArrayInt;
typedef Pair<ArrayInt,ArrayInt> PairArray;
class Wine:private PairArray
{
private:
string label;
//PairArray pa;
int yearNum;//永远存储年数,即一共有几种年份的
public:
Wine(const string l,int y,const int yr[],int bot[])
{
label=l;
yearNum=y;//已经指明了长度
ArrayInt f(yr,y);//构造valarray<int>数组
ArrayInt b(bot,y);
PairArray::setFirst(f);	//这里改为域运算符
PairArray::setSecond(b);

}
Wine(const string l,int y)
{
label=l;
PairArray::setFirst(ArrayInt(y));
PairArray::setSecond(ArrayInt(y));
//pa.setFirst(ArrayInt(y));
//pa.setSecond(ArrayInt(y));
yearNum=y;
}
void GetBottles()
{
int len=yearNum;
int i=0;
while(i<len)
{
cout<<"Enter year:";
cin>>Pair::first()[i];
//cin>>pa.first()[i];//这个表达式是不是很奇怪?
//pa.first()返回的是a,而a的类型是valarry,这个就不奇怪了吧,哈哈
cin.get();//记得吃掉这个回车;
cout<<"Enter bottles for that year:";
//cin>>pa.second()[i];
cin>>Pair::second()[i];
cin.get();
i++;
}
}

string &Label()
{
return label;
}
int sum()
{
//return pa.second().sum();
return PairArray::second().sum();
}
void show()
{
int len=yearNum;
int i=0;
cout<<"Wine:"<<label<<endl;
cout<<"     year        bottols"<<endl;
for(;i<len;i++)
{
//cout<<"     "<<pa.first()[i]<<"        "<<pa.second()[i]<<endl;
cout<<"     "<<PairArray::first()[i]<<"        "<<PairArray::second()[i]<<endl;
}
}

};


测试不变
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息