//1题///
类声明
#ifndef wine_hpp
#define wine_hpp
#include
#include
#include
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; }
Pair(const T1 & aval, const T2 & bval)
:a(aval), b(bval) {}
void Set(const T1 & aval, const T2 & bval);
void Show()const;
~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;
}
template<class T1, class T2>
void Pair<T1, T2>::Set(const T1 & aval, const T2 & bval)
{
a = aval;
b = bval;
}
template<class T1, class T2>
void Pair<T1, T2>::Show()const
{
std::cout<<"\t\tYear " << ‘\t’ <<“Bottles\n”;
for (int i = 0; i < a.size(); i++)
{
std::cout <<’\t’<<’\t’<< a[i] <<’\t’ << b[i] << std::endl;
}
}
typedef std::valarray ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
class Wine
{
private:
PairArray pa;
std::string name; //存储葡萄酒名
int years; //存储几种年数
public:
Wine(const char * l , const int y, const int ye[], const int bot[]);
Wine(const char * l, int y);
void GetBottles();
const std::string & Label()const;
int sum()const;
void Show()const;
~Wine(){}
};
#endif /* wine_hpp */
类函数实现/
#include “wine.hpp”
Wine::Wine(const char * l, const int y, const int ye[], const int bot[])
{
std::valarray a(ye, y);
std::valarray b(bot, y);
name = l;
years = y;
pa.Set(a, b);
}
Wine::Wine(const char * l, const int y)
{
std::valarray a(y);
std::valarray b(y);
pa.Set(a, b);
name = l;
years = y;
}
void Wine::GetBottles()
{
std::valarray a(years);
std::valarray b(years);
std::cout <<“Enter " << name <<” data for " << years <<" year(s):\n";
for (int i = 0; i < years; i++)
{
std::cout <<"Enter years: ";
std::cin >> a[i];
std::cout <<"Enter bottles for that year: ";
std::cin >> b[i];
}
pa.Set(a, b);
}
const std::string & Wine::Label()const
{
return name;
}
int Wine::sum()const
{
std::valarray a(years);
a = pa.second();
int totl = 0;
totl = a.sum();
return totl;
}
void Wine::Show()const
{
std::cout<<"Wine: " << name << std::endl;
pa.Show();
}
//2题//
类定义
#ifndef wine_hpp
#define wine_hpp
#include
#include
#include
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; }
Pair(const T1 & aval, const T2 & bval)
:a(aval), b(bval) {}
void Set(const T1 & aval, const T2 & bval);
virtual void Show()const;
virtual ~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;
}
template<class T1, class T2>
void Pair<T1, T2>::Set(const T1 & aval, const T2 & bval)
{
a = aval;
b = bval;
}
template<class T1, class T2>
void Pair<T1, T2>::Show()const
{
std::cout<<"\t\tYear " << ‘\t’ <<“Bottles\n”;
for (int i = 0; i < a.size(); i++)
{
std::cout <<’\t’<<’\t’<< a[i] <<’\t’ << b[i] << std::endl;
}
}
typedef std::valarray ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
class Wine :private PairArray
{
private:
std::string name; //存储葡萄酒名
int years; //存储几种年数
public:
Wine(const char * l , const int y, const int ye[], const int bot[]);
Wine(const char * l, int y);
void GetBottles();
const std::string & Label()const;
int sum()const;