您的位置:首页 > 编程语言 > C语言/C++

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

2012-03-25 14:55 363 查看

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

//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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ 编程 string pair class