#ifndefZY2_H_
#defineZY2_H_
#include//使用string类
#include//使用valarray类
#include
typedefstd::valarrayArrayInt;
template//有两个类型参数
classPair
{
private:
T1a;
T2b;
public:
Pair(constT1&aval,constT2&bval):a(aval),b(bval){}
Pair(){}
T1&first(){returna;}//返回类型为T1&,使得可以修改数据,用于常规对象
T2&second(){returnb;}//返回类型为T2&,使得可以修改数据,用于常规对象
T1first()const{returna;}//返回类型为T1,后加const,使得不可以修改数据,用于常量对象
T2second()const{returnb;}//返回类型为T2,后加const,使得不可以修改数据,用于常量对象
Pair&operator=(constPair&p);
};
typedefPairPairArray;
PairArray&Pair::operator=(constPairArray&p)//
{
if(this==&p)
return*this;
a=p.a;
b=p.b;
return*this;
}
classWine:privatestd::string,privatePair
{
private:
intyears;
public:
Wine(){}
Wine(constchar*l,inty,constintyr[],constintbot[])
:std::string(l),years(y),Pair(ArrayInt(yr,y),ArrayInt(bot,y)){}//这样写PairArray(ArrayInt(yr,y),ArrayInt(bot,y))编译器可以接受,但是这样写PairArray(ArrayInt(yr,years),ArrayInt(bot,years))编译器不可以接受.跪求大神指教
Wine(constchar*l,inty):std::string(l),years(y),Pair(){}//yb()调用构造函数Pair()
voidGetBottles();
conststd::string&Label()const{return(conststd::string&)(*this);}
intsum()const{returnPair::second().sum();}//通过类名和作用域解析运算符来调用基类方法,可使用second()接口,然后second()返回ArrayInt类对象b,b可使用sum()接口(valarray类的接口)
voidShow()const;
};
voidWine::GetBottles()
{
ArrayIntyear(years);
ArrayIntbottle(years);
std::cout>year;
std::cout>bottle;
}
Pair::first()=year;//通过类名和作用域解析运算符来调用基类方法,可使用first()接口,然后first()返回ArrayInt类对象a,a可接受另一个ArrayInt类对象year
Pair::second()=bottle;
}
voidWine::Show()const
{
std::cout::first()::second()>yrs;
Wineholding(lab,yrs);
holding.GetBottles();
holding.Show();
constintYRS=3;
inty[YRS]={1993,1995,1998};
intb[YRS]={48,60,72};
Winemore("GushingGrapeRed",YRS,y,b);
more.Show();
cout -->
|