WINE.h
#pragma once
#ifndef WINE_H_
#define WINE_H_
#include<valarray>
#include<iostream>
template <class T1,class T2>
class Pair {
private:
T1 a;
T2 b;
public:
Pair();
void setfist(const T1 & y) { a = y; };
void setscond(const T2 & bo) { b = bo; };
T1 & first();
T2 & second();
T1 fist()const{return a; };
T2 second()const{return b; };
Pair(const T1 & y, const T2 & b) :year(y), bottle(b) {};
};
class String {
private:
char * str;
public:
String();
virtual ~String();
String(char * ch);
String(const String & st);
String & operator=(const String & st);
char * display()const { return str; };
friend std::ostream & operator<<(std::ostream & os, const String & st);
};
typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt>PairArray;
class Wine :private PairArray ,String//普通类继承类模板需要将模板实例化
{
private:
int years;
public:
Wine(const String & l, int y, const int yr[], const int bot[]);
Wine(const String & l, int y);
void GetBottles();//提示用户输入年份
void Label() { std::cout << (const String &)(*this); };//显示葡萄酒的名称,这里使用向上强制转换访问基类数据
int sum() {return Pair::second().sum();};//返回瓶数的总和
void Show()const;
Wine & operator=(const Wine & w);
};
template<class T1, class T2>
inline Pair<T1, T2>::Pair()
{
a = 0;
b = 0;
}
template<class T1,class T2>
T1 & Pair<T1,T2>::first()
{
return a;
}
template<class T1,class T2>
T2 & Pair<T1, T2>::second()
{
return b;
}
#endif
WINE.cpp
#include "WINE.h"
#include <iostream>
Wine::Wine(const String & l, int y, const int yr[], const int bot[]):String(l)
{
years = y;
ArrayInt f(yr, y);//创建valarray数组
ArrayInt b(bot, y);
Pair::setfist(f);
Pair::setscond(b);
}
Wine::Wine(const String & l, int y):String(l)
{
Pair::setfist(ArrayInt(y));
Pair::setscond(ArrayInt(y));
years = y;
}
void Wine::GetBottles()
{
using std::cin;
using std::cout;
for (int i = 0; i < years; i++)
{
cout << "Enter years: ";
cin >> Pair::first()[i];//pa.first()返回一个valarray<int> a,所以这里等价于valarray<int>a[i]
cout << "Enter bottle for the year: ";
cin >> Pair::second()[i];
}
}
void Wine::Show() const
{
using std::cout;
using std::endl;
cout << "Wine:" << String::display() << endl;
cout << "\tYear\tBottle" << endl;
for (int i = 0; i < years; i++)
{
cout<<"\t"<< Pair::fist()[i] << "\t" << Pair::second()[i] << endl;
}
}
Wine & Wine::operator=(const Wine & w)
{
PairArray::operator=(w);//调用基类的赋值运算符
String::operator=(w);
years = w.years;
return *this;
}
String::String()
{
str = new char[1];
str[0] = '\0';
}
String::~String()
{
delete[]str;
}
String::String(char * ch)
{
str = new char[std::strlen(ch) + 1];
std::strcpy(str, ch);
}
String::String(const String & st)
{
str = new char[std::strlen(st.str) + 1];
std::strcpy(str, st.str);
}
String & String::operator=(const String & st)
{
if (this == &st)
return *this;
delete[]str;
str = new char[std::strlen(st.str) + 1];
std::strcpy(str, st.str);
return *this;
}
std::ostream & operator<<(std::ostream & os, const String & st)
{
os << st.str ;
return os;
}
main.cpp
#include <iostream>
#include "WINE.h"
int main()
{
using std::cout;
using std::endl;
using std::cin;
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();
cout << ": " << more.sum() << endl;
cout << "Bye\n";
cin.get();
cin.get();
return 0;
}