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

261人阅读 评论(0)

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

//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;
}
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：30769次
• 积分：826
• 等级：
• 排名：千里之外
• 原创：53篇
• 转载：2篇
• 译文：0篇
• 评论：1条