#ifndef PAIRS_H#define PAIRS_H#include#include template class Pair{ private: T1 a; T2 b; public: T1 & first(); T2 & second(); T1 first() const { return a;} T2 second() const { return b;} Pair(const T1 & aval,const T2 & bval) : a(aval),b(bval) {} Pair() {} void Set_Pair(const T1 & aval,const T2 & bval); void Show(int ys); int Sum(void);};template T1 & Pair ::first(){ return a;}template T2 & Pair ::second(){ return b;}template void Pair ::Set_Pair(const T1 & aval,const T2 & bval){ a=aval; b=bval;}template void Pair ::Show(int ys){ int i=0; for (;i int Pair ::Sum(void){ return b.sum();}#endif
#ifndef WINE_H#define WINE_H#include#include "pairs.h"#include using namespace std;class wine{ typedef valarray ArrayInt; typedef Pair PairArray; private: string name; PairArray year_bottles; int years; public: wine(){} wine(const char *l,int y,const int yr[],const int bot[]); wine(const char *l,int y); void GetBottles(void); void Show(void); int sum(void); string Label(void){ return name;}};#endif
#include"wine.h"#include"pairs.h"#include//#include #include using namespace std;wine::wine(const char *l,int y,const int yr[],const int bot[]) { name=*l; years=y; year_bottles.Set_Pair(ArrayInt(yr,y),ArrayInt(bot,y)); }wine::wine(const char *l,int y){ name=*l; years=y;} void wine::GetBottles(void){ int i=0; ArrayInt year(years),bottle(years); cout << "Enter "<< name <<"data for "< <<" year(s):" < >year[i]; //cout << endl; cout << "Enter bottles for that year: "; cin >>bottle[i]; //cout << endl; } year_bottles.Set_Pair(year,bottle);}void wine::Show(void){ cout << "wine: "<< name <
#include "wine.h"#includeusing namespace std;int main(void){ cout << "Enter name of wine: "; char lab[50]; cin.getline(lab,50); cin.sync(); 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 " < << ": " < <
wine类包含string和Pair两个类,前者用于存储酒名,后者有2个valarray<int>对象,分别用于存储酿造年份和该年的瓶数。