C++ Premier Plus 6th edition - Programming excercise - Chapter14 - 1

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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值