14.1

//pair.h
#ifndef _PARI_H
#define _PARI_H


/*#include<iostream>
#include<string>
#include<valarray>

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 setfirst(const T1& a1)
	{
		a=a1;
	};
	void setsecond(const T2 & a2)
	{
		b=a2;
	};
	Pair(const T1& aval,const T2& bval): a(aval),b(bval){ };
	Pair(){};

};*/
#include <iostream>  
using namespace std;  
//p580  
template <class T1,class T2>  
class Pair{  
private:  
    T1 a;  
    T2 b;  
public:  
    void setFirst(const T1 &t1)  
    {  
        a=t1;  
    }  
    void setSecond(const T2 &t2)  
    {  
        b=t2;  
    }  
    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(){}
};  

#endif
//pair.cpp
#include"pair.h"
template <class T1,class T2>  
T1 &Pair<T1,T2>::first()  
{  
    return a;  
}  
template <class T1,class T2>  
T2 &Pair<T1,T2>::second()  
{  
    return b;  
}  
//wine.h
#ifndef _WINE_
#define _WINE_


#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:  
    string label;  
    PairArray pa;  
    int yearNum;//永远存储年数,即一共有几种年份的  
public:  
    Wine(const string l,int y,const int yr[],int bot[]) ; 
   
    Wine(const string l,int y)  ;
    
    void GetBottles()  ;
   
  
    string &Label()  ;
   
    int sum()  ;
   
    void show() ; 
    
};
#endif
//wine.cpp

#include"wine.h"
Wine::Wine(const string l,int y,const int yr[],int bot[]) 
   
	 {  
        label=l;  
        //int leny=sizeof(yr);//先获取长度  
        //int lenb=sizeof(bot);  
        //yearNum=leny;//yr[]数组中存放的是年份  
        yearNum=y;//已经指明了长度  
        ArrayInt f(yr,y);//构造valarray<int>数组  
        ArrayInt b(bot,y);  
        pa.setFirst(f);  
        pa.setSecond(b);  
    }  
	Wine::Wine(const string l,int y)
	{  
        label=l;  
        pa.setFirst(ArrayInt(y));  
        pa.setSecond(ArrayInt(y));  
        yearNum=y;  
    }  
	void Wine::GetBottles()
	 {  
        int len=yearNum;  
        int i=0;  
        while(i<len)  
        {  
            cout<<"Enter year:";  
            cin>>pa.first()[i];//这个表达式是不是很奇怪?  
                            //pa.first()返回的是a,而a的类型是valarry,这个就不奇怪了吧,哈哈  
            cin.get();//记得吃掉这个回车;  
            cout<<"Enter bottles for that year:";  
            cin>>pa.second()[i];  
            cin.get();  
            i++;  
        }  
    }  
	std::string & Wine ::Label()
	 {  
        return label;  
    }  
	int Wine :: sum()
	 {  
        return pa.second().sum();  
    }  
	void Wine::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;  
        }  
      
    }  
  
 
//main.cpp
#include<iostream>
#include"wine.h"
//#include"pair.h"
int main()
{
	using std ::cin;
	using std::cout;
	using std::endl;
	cout <<"enter name ; ";
	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",YRS,y,b);
	more.show();
	cout<<"totlal bottles for" <<more.Label()<<"; "<<more.sum()<<endl;
	cout<<"end"<<endl;
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值