#include <iostream>
#include <string>
using namespace std;
template <class T1,class T2>
class Pair
{
private:
T1 a;
T2 b;
public:
T1 &first();
T2 &second();
T1 first()const{return a;}
T2 second()const{return b;}
void setmember(const T1 &t1,const T2 &t2)
{
a=t1;
b=t2;
}
Pair(const T1 &aval,const T2 &bval):a(aval),b(bval){}
Pair(){}
};
template<class T1,class T2>
T1 & Pair<T1,T2>::first()
{
return a;
}
template<class T1,class T2>
T2 & Pair<T1,T2>::second()
{
return b;
}
#include <iostream>
#include <string>
#include <valarray>
#include "pair.cpp"//解决类声明和定义写在同一个cpp造成的定义冲突的方法,将所有方法写成内联方法
using namespace std;
typedef valarray<int> ArrayInt;
typedef Pair<ArrayInt,ArrayInt> PairArray;
class Wine
{
private:
string name;
PairArray p;
int yearnumber;
public:
Wine(const char *l,int y,const int yr[],const int bot[]):name(l)
{
yearnumber=y;
ArrayInt f(yr,y);//构造valarray<int>数组
ArrayInt b(bot,y);
p.setmember(f,b);
}
Wine(const char *l,int y)
{
name=l;
yearnumber=y;
p.setmember(ArrayInt(y),ArrayInt(y));
}
void GetBottles()
{
cout<<"您能够输入"<<yearnumber<<"次年份和瓶数"<<endl;
for(int i=0;i<yearnumber;i++)
{
cout<<"请输入年份:";
cin>>p.first()[i];
cout<<"请输入瓶数:";
cin>>p.second()[i];
}
}
string Label()
{
return name;
}
int sum()
{
return p.second().sum();
}
void show()
{
cout<<"葡萄酒名称:"<<name<<endl;
cout<<"储存年数:"<<yearnumber<<endl;
for(int i=0;i<yearnumber;i++)
{
cout<<"年份:"<<p.first()[i]<<endl;
cout<<"瓶数:"<<p.second()[i]<<endl;
}
}
};
#include <iostream>
#include "wine.cpp"
using namespace std;
int main()
{
cout<<"Eneter name of wine:";
char lab[50];
cin.getline(lab,50);
cout<<"Enter number of year:";
int yrs;
cin>>yrs;
Wine holding(lab,yrs);
holding.GetBottles();
holding.show();
// Pair<int,int> a(1,2);
// cout<<a.first()<<endl
// <<a.second()<<endl;
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)