《C++ Primer Plus》14章编程练习1、2

        就当练下手吧。后面的题不是太长就是太烦,不做了。

//Create a class by using the technique of composition
//Charpter 14,Programming Exercise 01
//File name: ch14-1.h
#ifndef _WINE_
#define _WINE_
#include <string>
#include <valarray>

using std::string; 
using std::valarray;

template <typename T1, typename T2>
class Pair
{
private:
    T1 a;
    T2 b;
public:
    Pair() {}
    Pair(const T1& aV, const T2& bV): a(aV), b(bV) {}
    const T1& getFirst() const {return a; }
    const T2& getSecond() const {return b; }
    T1& setFirst() {return a; }
    T2& setSecond(){return b; }
};

class Wine
{
private:
    typedef valarray<int> intArray;
    typedef Pair<intArray,intArray> PairArray;
    string label;       //reference to wine name
    PairArray WineInfo; //producted year and number of bottles
    int years;          //number of years
public:
    Wine() {}
    Wine(const char* l, int y, const int yr[], const int bot[]);
    Wine(const char* l, int y);
    void GetBottles();
    const string& Label() const {return label; }
    int sum () const;   
    void Show() const;
};
#endif
#include <iostream>
#include "ch14-1.h"

using std::cout; using std::endl;
using std::cin;  using std::left;

Wine::Wine(const char* l, int y, const int yr[], const int bot[])
    : label(l),years(y)
{
    WineInfo.setFirst().resize(y);
    WineInfo.setSecond().resize(y);
    for(int i = 0; i< y; i++)
    {
        WineInfo.setFirst()[i] = yr[i];
        WineInfo.setSecond()[i] = bot[i];
    }
}

Wine::Wine(const char* l, int y): label(l),years(y)
{
    WineInfo.setFirst().resize(y);
    WineInfo.setSecond().resize(y);
}

void Wine::GetBottles()
{
    cout << "Enter " << label << " data for " << years << " year(s)\n";
    for(int i = 0; i< years; i++)
    {
        cout << "Enter year: "; 
        cin >> WineInfo.setFirst()[i];
        cout << "Enter Bottles for that year: ";
        cin >> WineInfo.setSecond()[i];
    }
}

int Wine::sum() const
{
    return WineInfo.getSecond().sum();
}

void Wine::Show() const
{
    cout << "Wine: " << label << endl;
    cout << "   Year    Bottles\n";
    for(int i = 0; i< years; i++)
    {
        cout << "   ";
        cout.width(8);
        cout << left << WineInfo.getFirst()[i];
        cout << left << WineInfo.getSecond()[i] << endl;
    }
}

-------------------------Programming-Exercise-2----------------------------
//Create a class by using the technique of private inheritance
//Charpter 14,Programming Exercise 02
//File name: ch14-2.h
#ifndef _WINE_
#define _WINE_
#include <string>
#include <valarray>

using std::string; 
using std::valarray;

template <typename T1, typename T2>
class Pair
{
private:
    T1 a;
    T2 b;
public:
    Pair() {}
    Pair(const T1& aV, const T2& bV): a(aV), b(bV) {}
    const T1& getFirst() const {return a; }
    const T2& getSecond() const {return b; }
    T1& setFirst() {return a; }
    T2& setSecond(){return b; }
};

typedef valarray<int> intArray;
typedef Pair<intArray,intArray> PairArray;

class Wine: private PairArray,private string
{
public:
    Wine() {}
    Wine(const char* l, int y, const int yr[], const int bot[]);
    Wine(const char* l, int y);
    void GetBottles();
    const string& Label() const {return (const string&)*this; }
    int sum () const;   
    void Show() const;
};
#endif
#include <iostream>
#include "ch14-2.h"

using std::cout; using std::endl;
using std::cin;  using std::left;

Wine::Wine(const char* l, int y, const int yr[], const int bot[])
    : string(l)
{
    PairArray::setFirst() = intArray(yr,y);
    PairArray::setSecond() = intArray(bot,y);
}

Wine::Wine(const char* l, int y) :string(l)
{
   PairArray::setFirst() = intArray(y);
   PairArray::setSecond() = intArray(y);
}

void Wine::GetBottles()
{
    int years = PairArray::getFirst().size();
    cout << "Enter " << (const string&)*this << " data for " 
        << years << " year(s)\n";
    for(int i = 0; i< years; i++)
    {
        cout << "Enter year: "; 
        cin >> ((PairArray*)this)->setFirst()[i];
        cout << "Enter Bottles for that year: ";
        cin >> ((PairArray*)this)->setSecond()[i];
    }
}

int Wine::sum() const
{
    return ((PairArray*)this)->getSecond().sum();
}

void Wine::Show() const
{
    int years = PairArray::getFirst().size();
    cout << "Wine: " << (const string&)*this << endl;
    cout << "   Year    Bottles\n";
    for(int i = 0; i< years; i++)
    {
        cout << "   ";
        cout.width(8);
        cout << left << ((PairArray const*)this)->getFirst()[i];
        cout << left << ((PairArray const*)this)->getSecond()[i] << endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值