这章有点难的= =。。。那个虚MI和模板把我看的有点晕QAQ
第一题
//main.cpp
#include <iostream>
#include "winec.h"
int main()
{
using std::cin;
using std::cout;
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;
}
//winec.h
#ifndef WINEC_H_
#define WINEC_H_
#include <iostream>
#include <string>
#include <valarray>
#include <utility>
class Wine
{
private:
std::string name;
std::pair<std::valarray<int>, std::valarray<int>> info;
public:
Wine() {}
Wine(const char *l, int y, const int yr[], const int bot[]);
Wine(const char *l, int y);
void GetBottles();
const std::string &Label() const;
int sum() const;
void Show() const;
};
#endif
//winec.cpp
#include "winec.h"
Wine::Wine(const char * l, int y, const int yr[], const int bot[])
{
name = l;
std::valarray<int> a(y);
std::valarray<int> b(y);
for (int i = 0; i < y; ++i)
{
a[i] = yr[i];
b[i] = bot[i];
}
info = std::make_pair(a, b);
}
Wine::Wine(const char * l, int y)
{
name = l;
std::valarray<int> a(y);
std::valarray<int> b(y);
info = std::make_pair(a, b);
}
void Wine::GetBottles()
{
std::valarray<int> a(info.first.size());
std::valarray<int> b(info.first.size());
std::cout << "Enter " << name << " for " << info.first.size() << " year(s):" << std::endl;
for (int i = 0; i < (int)info.first.size(); ++i)
{
std::cout << "Enter year: ";
std::cin >> a[i];
std::cout << "Enter bottles for that year: ";
std::cin >> b[i];
}
info = std::make_pair(a, b);
}
const std::string & Wine::Label() const
{
return name;
}
int Wine::sum() const
{
int ans = 0;
for (int i = 0; i < (int)info.first.size(); ++i)
ans += info.second[i];
return ans;
}
void Wine::Show() const
{
std::cout << "Wine: " << name << std::endl;
std::cout << "\t\t" << "Year" << "\t" << "Bottles" << std::endl;
for (int i = 0; i < (int)info.first.size(); ++i)
std::cout << "\t\t" << info.first[i] << "\t" << info.second[i] << std::endl;
}
第二题
//第二题写的有点崩...主要是没有用typedef 或者 using 简化类型名,结果每次都好长一串类型....
//main.cpp
#include <iostream>
#include "winec.h"
int main()
{
using std::cin;
using std::cout;
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;
}
//winec.h
#ifndef WINEC_H_
#define WINEC_H_
#include <iostream>
#include <utility>
#include <string>
#include <valarray>
class Wine : private std::string, private std::pair<std::valarray<int>, std::valarray<int>>
{
public:
Wine() {}
Wine(const char *l, int y, const int yr[], const int bot[]);
Wine(const char *l, int y);
void GetBottles();
const std::string &Label() const;
int sum() const;
void Show() const;
};
#endif
//winec.cpp
#include "winec.h"
Wine::Wine(const char * l, int y, const int yr[], const int bot[]) : std::string(l), std::pair<std::valarray<int>, std::valarray<int>>(std::valarray<int>(yr, y), std::valarray<int>(bot, y))
{
}
Wine::Wine(const char * l, int y) : std::string(l), std::pair<std::valarray<int>, std::valarray<int>>(