wine.h
#ifndef WINE_H_
#define WINE_H_
#include<string>
#include<valarray>
// delclarations and implementaions of Pair
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) { }
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;
}
// delclarations of Wine
typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt,ArrayInt> PairArray;
class Wine
{
private:
std::string lab;
PairArray pa;
int yrs;
public:
Wine();
Wine(const char* l, int y, const int yr[], const int bot[]);
Wine(const char* l, int y);
void GetBottles();
std::string& Label()
{
return lab;
};
int sum();
void Show();
};
#endif
wine.cpp
// implementations of Wine
// as did not use template for class Wine,so place implementations
// in seperate file
// attention, the use of valarray(array,elements_num)
#include<iostream>
#include"wine.h"
using std::cin;
using std::cout;
using std::endl;
Wine::Wine()
{
lab = "";
pa = PairArray(ArrayInt(0), ArrayInt(0));
yrs = 0;
}
Wine::Wine(const char* l, int y, const int yr[], const int bot[])
{
lab = l;
yrs = y;
// approach 1: use default copy-constructor of PairArray
pa = PairArray(ArrayInt(yr,yrs), ArrayInt(bot,yrs));
}
Wine::Wine(const char* l, int y)
{
lab = l;
yrs = y;
// approach 2: use default copy-constructor of ArrayInt
pa.first() = ArrayInt(yrs);
pa.second() = ArrayInt(yrs);
}
/*
// above constructor can also be written as:
void Wine::GetBottles()
{
cout << "Enter pair of year and number of bottles.\n";
int* pty = new int[yrs];
int* ptb = new int[yrs];
for (int i = 0; i < yrs; i++)
{
cout << "Year: ";
cin >> pty[i];
cout << "bottles: ";
cin >> ptb[i];
}
// there are overloaded first() and second()
// but only ArrayInt& version can be left value and be assigned value
pa.first() = ArrayInt(pty, yrs);
pa.second() = ArrayInt(ptb, yrs);
delete[]pty;
delete[]ptb;
}
*/
void Wine::GetBottles()
{
cout << "Enter pair of year and number of bottles.\n";
for (int i = 0; i < yrs; i++)
{
cout << "Year: ";
cin >> pa.first()[i];
cout << "bottles: ";
cin >> pa.second()[i];
}
}
int Wine::sum()
{
return pa.second().sum();
}
void Wine::Show()
{
cout << "Wine: " << lab << endl;
cout << "\t" << "year" << "\t" << "Bottles" << endl;
for (int i = 0; i < yrs; i++)
cout << "\t"<<pa.first()[i] << "\t" << pa.second()[i] << endl;
}
main.cpp
// pe14-1.cpp -- using Wine class with containment
#include <iostream>
#include "wine.h"
int main(void)
{
using std::cin;
using std::cout;
using std::endl;
cout << "Enter name of wine: ";
char lab[50];
cin.getline(lab, 50);
cout << "Enter number of years: ";
int yrs;
cin >> yrs;
Wine holding(lab, yrs); // store label, years, give arrays yrs elements
holding.GetBottles(); // solicit input for year, bottle count
holding.Show(); // display object contents
const int YRS = 3;
int y[YRS] = { 1993, 1995, 1998 };
int b[YRS] = { 48, 60, 72 };
// create new object, initialize using data in arrays y and b
Wine more("Gushing Grape Red", YRS, y, b);
more.Show();
cout << "Total bottles for " << more.Label() // use Label() method
<< ": " << more.sum() << endl; // use sum() method
cout << "Bye\n";
cin.get();
cin.get();
return 0;
}