参考链接:https://blog.csdn.net/weixin_41882882/article/details/82118401
https://blog.csdn.net/u012175089/article/details/53914092
1.
//头文件,类的声明
#ifndef WINE_H_
#define WINE_H_
#include<iostream>
#include<string>
#include<valarray>
typedef std::valarray<int> ArrayInt;
typedef std::pair<ArrayInt, ArrayInt> PairArray;
class Wine
{
private:
std::string label;
PairArray number;
int yearnumber;
public:
//Wine(){}
Wine(const char * l, int y, const int yr[], const int bot[]);
Wine(const char * l, int y);
//~Wine(){}
void GetBottles();
void Show();
const std::string & Label();
int sum();
};
#endif
#include "Wine.h"
#include<iostream>
#include<string>
using std::cin;
using std::cout;
using std::string;
using std::endl;
Wine::Wine(const char * l, int y, const int yr[], const int bot[])
{
label = l;
yearnumber = y;
ArrayInt a(y);
ArrayInt b(y);
for (int i = 0; i < y; i++)
{
a[i] = yr[i];
b[i] = bot[i];
}
number = std::make_pair(a, b);
}
Wine::Wine(const char * l, int y)
{
label = l;
yearnumber = y;
ArrayInt a(y);
ArrayInt b(y);
number = std::make_pair(a, b);
}
void Wine::GetBottles()
{
cout << "Enter" << label << "data for" << yearnumber << "year(s):\n";
number.first.resize(yearnumber);
number.second.resize(yearnumber);
for (int i = 0; i < yearnumber; i++)
{
cout << "Enter year:";
cin >> number.first[i];
cout << "Enter bottles for that year:";
cin >> number.second[i];
}
}
void Wine::Show()
{
cout << "Wine:" << label << endl;
cout << " Year Bottles" << endl;
for (int i = 0; i < yearnumber; i++)
{
cout << " " << number.first[i]
<< " " << number.second[i] << endl;
}
}
const std::string & Wine::Label()
{
return label;
}
int Wine::sum()
{
int count = 0;
for (int i = 0; i < yearnumber; i++)
count += number.second[i];
return count;
}
//书中的测试程序
#include<iostream>
#include"Wine.h"
int main(void)
{
using std::cout;
using std::cin;
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);
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 bottles for" << more.Label()
<< ":" << more.sum() << endl;
cout << "Bye\n";
return 0;
}
2.
//头文件,类的声明
#ifndef WINE_H_
#define WINE_H_
#include<iostream>
#include<string>
#include<valarray>
typedef std::valarray<int> ArrayInt;
typedef std::pair<ArrayInt, ArrayInt> PairArray;
class Wine:private std::string, private PairArray
{
private:
int yearnumber;
public:
//Wine(){}
Wine(const char * l, int y, const int yr[], const int bot[]);
Wine(const char * l, int y);
//~Wine(){}
void GetBottles();
void Show();
const std::string & Label();
int sum();
};
#endif
#include "Wine.h"
#include<iostream>
#include<string>
#include<cstdlib>
using std::cin;
using std::cout;
using std::string;
using std::endl;
Wine::Wine(const char * l, int y, const int yr[], const int bot[])
{
(string &)(*this) = l;
yearnumber = y;
ArrayInt a(y);
ArrayInt b(y);
for (int i = 0; i < y; i++)
{
a[i] = yr[i];
b[i] = bot[i];
}
(PairArray &)(*this) = std::make_pair(a, b);
}
Wine::Wine(const char * l, int y)
{
(string &)(*this) = l;
yearnumber = y;
}
void Wine::GetBottles()
{
cout << "Enter " << (string &)(*this) << " data for " << yearnumber << " year(s):\n";
PairArray& number = (PairArray&)(*this);
number.first.resize(yearnumber);
number.second.resize(yearnumber);
for (int i = 0; i < yearnumber; i++)
{
cout << "Enter year:";
cin >> number.first[i];
cout << "Enter bottles for that year:";
cin >> number.second[i];
}
}
void Wine::Show()
{
cout << "Wine:" << (string&)(*this) << endl;
cout << " Year Bottles" << endl;
PairArray & number = (PairArray&)(*this);
for (int i = 0; i < yearnumber; i+