1.Wine类有一个string类对象成员(参见第4章)和一个Pair对象(参见本章);其中前者用于存储葡萄酒的名称,而后者有2个valarray对象(参见本章),这两个valarray对象分别保存了葡萄酒的酿造年份和该年生产的瓶数。例如,Pair的第1个valarray对象可能为1988、1992和1996年,第2个valarray对象可能为24、48和144瓶。Wine最好有1个int成员用于存储年数。另外,一些typedef可能有助于简化编程工作:
typedef std::valarry<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
这样,PairArray表示的是类型Pair<std::valarray, std::valarray >。使用包含来实现Wine类,并用一个简单的程序对其进行测试。Wine类应该有一个默认构造函数以及如下构造函数:
Wine(const char* l, int y, const int yr[], const int bot[]);
Wine(const char* l, int y);
Wine类应该有一个GetBottles( )方法,它根据Wine对象能够存储几种年份(y),提示用户输入年份和瓶数。方法Label( )返回一个指向葡萄酒名称的引用。sum( )方法返回Pair对象中第二个valarray对象中的瓶数总和。
测试程序应提示用户输入葡萄酒名称、元素个数以及每个元素存储的年份和瓶数等信息。程序将使用这些数据来构造一个Wine对象,然后显示对象中保存的信息。
下面是一个简单的测试程序:
int main()
{
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);
holding.GetBottles();
holding.Show();
const int YRS = 3;
int y[YRS] = {
1993, 1995, 1998};
int b[YRS] = {
48, 60, 72};
Wine more("Gushing Grape Red", YRS, y, b);
more.Show();
cout << "Total bottle for " << more.Label()
<<": " << more.sum() << endl;
cout << "Bye\n";
return 0;
}
下面是该程序的运行情况:
Enter name of wine: Gully Wash
Enter number of years: 4
Enter Gully Wash data for 4 year(s):
Enter year: 1988
Enter bottles for that year: 42
Enter year: 1994
Enter bottles for that year: 58
Enter year: 1998
Enter bottles for that year: 122
Enter year: 2001
Enter bottles for that year: 144
Wine: Gully Wash
Year Bottles
1988 42
1994 58
1998 122
2001 144
Wine: Gushing Grape Red
Year Bottles
1993 48
1995 60
1998 72
Total bottle for Gushing Grape Red: 180
Bye
//clss.h
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
#include <valarray>
#include <string>
class Wine
{
typedef std::valarray<int> ArrayInt;
typedef std::pair<ArrayInt, ArrayInt> PairArray;
public:
Wine(const char* l, int y, const int yr[], const int bot[]);
Wine(const char* l, int y);
~Wine() {
};
void GetBottles();
std::string& Label();
int sum()const;
void Show()const;
private:
std::string label;
PairArray info;
int yearnum;
};
#endif // !CLASS_H_
//class.cpp
#include"class.h"
Wine::Wine(const char* l, int y, const int yr[], const int bot[])
{
label = l;
yearnum = y;
info.first.resize(yearnum);
info.second.resize(yearnum);
for (int i = 0; i < y; i++)
{
info.first[i] = yr[i];
info.second[i] = bot[i];
}
}
Wine::Wine(const char* l, int y)
{
label = l;
yearnum = y;
}
void Wine::GetBottles()
{
using namespace std;
cout << "Enter " << label << " data for " << yearnum << " year(s):\n";
info.first.resize(yearnum); //这一步非常重要,不重新设置长度的话会退出
info.second.resize(yearnum);
for (int i = 0;i < yearnum;i++)
{
cout << "Enter year: ";
cin >> info.first[i];
cout << "Enter bottles for that year: ";
cin >> info.second[i];
}
}
std::string& Wine::Label()
{
return label;
}
int Wine::sum() const
{
return info.second.sum();
}
void Wine::Show() const
{
using namespace std;
cout << "Wine: " << label << endl;;
cout << "\tYear\tBottles\n";
for (int i = 0;i < yearnum;i++)
{
cout << '\t'<<info.first[i];
cout << '\t' << info.second[i] << endl;
}
}
//main.cpp
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/5/6
* 描述:
************************************************* */
#include<iostream>
#include"class.h"
using namespace std;
int main()
{
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);
holding.GetBottles();
holding.Show();
const int YRS = 3;
int y[YRS] = {
1993, 1995, 1998 };
int b[YRS] = {
48, 60, 72 };
Wine more("Gushing Grape Red", YRS, y, b);
more.Show();
cout << "Total bottle for " << more.Label()
<< ": " << more.sum() << endl;
cout << "Bye\n";
return 0;
}
2.采用私有继承而不是包含来完成编程练习1。同样,一些typedef可能会有所帮助,另外,您可能还需要考虑诸如下面这样的语句的含义:
PairArray::operator=(PairArray(ArrayInt(),ArrayInt()));
cout<<(const string& )(*this);
您设计的类应该可以使用编程练习1中的测试程序进行测试。
//clss.h
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
#include <valarray>
#include <string>
typedef std::valarray<int> ArrayInt;
typedef std::pair<ArrayInt, ArrayInt> PairArray;
class Wine :private std::string, private PairArray
{
public:
Wine(const char* l, int y, const int yr[], const int bot[]);
Wine(const char* l, int y);
~Wine() {
}<