C++ Primer Plus 第十四章 课后编程练习题1-5

//1题///
类声明

#ifndef wine_hpp
#define wine_hpp
#include
#include
#include
template <class T1, class T2>
class Pair
{
private:
T1 a;
T2 b;
public:
T1 & first();
T2 & second();
T1 first() const {return a; }
T2 second() const { return b; }
Pair(const T1 & aval, const T2 & bval)
:a(aval), b(bval) {}
void Set(const T1 & aval, const T2 & bval);
void Show()const;
~Pair(){}
};

template<class T1, class T2>
T1 & Pair<T1, T2>::first()
{
return a;
}

template<class T1, class T2>
T2 & Pair<T1, T2>::second()
{
return b;
}

template<class T1, class T2>
void Pair<T1, T2>::Set(const T1 & aval, const T2 & bval)
{
a = aval;
b = bval;
}

template<class T1, class T2>
void Pair<T1, T2>::Show()const
{
std::cout<<"\t\tYear " << ‘\t’ <<“Bottles\n”;
for (int i = 0; i < a.size(); i++)
{
std::cout <<’\t’<<’\t’<< a[i] <<’\t’ << b[i] << std::endl;
}
}
typedef std::valarray ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;

class Wine
{
private:
PairArray pa;
std::string name; //存储葡萄酒名
int years; //存储几种年数
public:
Wine(const char * l , const int y, const int ye[], const int bot[]);
Wine(const char * l, int y);
void GetBottles();
const std::string & Label()const;
int sum()const;
void Show()const;
~Wine(){}
};

#endif /* wine_hpp */

类函数实现/
#include “wine.hpp”
Wine::Wine(const char * l, const int y, const int ye[], const int bot[])
{
std::valarray a(ye, y);
std::valarray b(bot, y);
name = l;
years = y;
pa.Set(a, b);
}

Wine::Wine(const char * l, const int y)
{
std::valarray a(y);
std::valarray b(y);
pa.Set(a, b);
name = l;
years = y;
}

void Wine::GetBottles()
{
std::valarray a(years);
std::valarray b(years);
std::cout <<“Enter " << name <<” data for " << years <<" year(s):\n";
for (int i = 0; i < years; i++)
{
std::cout <<"Enter years: ";
std::cin >> a[i];
std::cout <<"Enter bottles for that year: ";
std::cin >> b[i];
}
pa.Set(a, b);
}
const std::string & Wine::Label()const
{
return name;
}
int Wine::sum()const
{
std::valarray a(years);
a = pa.second();
int totl = 0;
totl = a.sum();

return totl;

}
void Wine::Show()const
{
std::cout<<"Wine: " << name << std::endl;
pa.Show();
}

//2题//
类定义
#ifndef wine_hpp
#define wine_hpp
#include
#include
#include
template <class T1, class T2>
class Pair
{
private:
T1 a;
T2 b;
public:
T1 & first();
T2 & second();
T1 first() const {return a; }
T2 second() const { return b; }
Pair(const T1 & aval, const T2 & bval)
:a(aval), b(bval) {}
void Set(const T1 & aval, const T2 & bval);
virtual void Show()const;
virtual ~Pair(){}
};

template<class T1, class T2>
T1 & Pair<T1, T2>::first()
{
return a;
}

template<class T1, class T2>
T2 & Pair<T1, T2>::second()
{
return b;
}

template<class T1, class T2>
void Pair<T1, T2>::Set(const T1 & aval, const T2 & bval)
{
a = aval;
b = bval;
}

template<class T1, class T2>
void Pair<T1, T2>::Show()const
{
std::cout<<"\t\tYear " << ‘\t’ <<“Bottles\n”;
for (int i = 0; i < a.size(); i++)
{
std::cout <<’\t’<<’\t’<< a[i] <<’\t’ << b[i] << std::endl;
}
}
typedef std::valarray ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;

class Wine :private PairArray
{
private:
std::string name; //存储葡萄酒名
int years; //存储几种年数
public:
Wine(const char * l , const int y, const int ye[], const int bot[]);
Wine(const char * l, int y);
void GetBottles();
const std::string & Label()const;
int sum()const;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值