C++ Primer Plus第六版编程题(第14章)

该博客主要介绍了C++ Primer Plus第六版中的编程练习,包括Wine类的设计,使用包含和私有继承的方式实现,以及QueueTp模板的定义。Wine类涉及了string、Pair对象以及valarray的使用,要求实现默认构造函数、特定构造函数以及GetBottles、Label和sum方法。同时,博客提到了Person、Gunsliner、PokerPlayer和BadDude类的多层次继承和虚函数应用,以及类层次结构中的抽象基类abstr_emp和赋值运算符、虚函数和友元函数的相关讨论。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++ Primer Plus第六版编程题(第14章)

题目

1.Wine类有一个string类对象成员(参见第4章)和一个Pair对象(参见本章):其中前者用来存储葡萄酒的名称,而后者有2个valarry < int>对象(参见本章),这两个valarry< int>对象分别保存了葡萄酒的酿造年份和该年生产的瓶数。例如,Pair的第1个valarray< int>对象可能为1988、1992和1996,第二个valarry< int> 对象可能为24、48和144瓶。Wine最好有1个int成员用于存储年数。另外,一些typedef可能有助于简化编程工作:

typedef std::valarry ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
这样,PairArray表示的类型是Pair<std::valarry,std::valarray>。使用包含来实现Wine类,并用一个简单的程序对其进行测试。Wine类应该有一个默认构造函数以及如下的构造函数:
Wine(const char* l, int y, const int yr[], ocnst int bot[]);
Wine(const char* l, int y);
Wine类应该有一个GetBottles()方法,它根据Wine对象能够存储集中年份(y),提示用户输入年份和瓶数。方法Label()返回一个指向葡萄酒名称的引用。sum()方法返回Pair对象中第二个valarray< int>对象中的瓶数总和。
测试程序应提示用户输入葡萄酒名称、元素个数以及每个元素存储的年份和瓶数等信息。程序将使用这些数据来构造一个Wine对象,然后显示对象中保存的信息。

下面是一个简单的测试程序:
(·········代码省略·········)

2.采用私有继承而不是包含来完成编程练习1.同样,一些typedef可能会有所帮助,另外,您可能还需要考虑诸如下面这样的语句的含义:

PairArray::operator=(PairArray(ArrayInt(), ArrayInt()));
cout << (const string&)(*this);
您设计的类应该可以使用编程练习1中的测试程序进行测试。

3.定义一个QueueTp模板。然后在一个类似于程序清单14.12的程序中创建一个指向Worker的指针队列(参见程序清单14.10中的定义),并使用该队列来测试它。

4.Person类保存人的名和姓。除构造函数外,它还有Show()方法,用于显示名和姓。Gunsliner类以Person类为虚基类派生而来,它包含一个Draw()成员,该方法返回一个double值,表示枪手的拔枪时间。这个类还包含一个int成员,表示枪手枪上的刻痕数。最后,这个类还包含一个Show()函数,用于显示所有这些信息。

PokerPlayer类以Person类为虚基类派生而来。它包含一个Draw()成员,该函数返回一个1~52的随机数,用于表示扑克牌的值(也可以定义一个Card类,其中包含花色和面值成员,然后让Draw()返回一个Card对象)。PokerPlayer类使用Person类的show()函数。BadDude()类从Gunslinger和PokerPlayer类公有派生而来。它包含Gdraw()成员(返回坏蛋拔枪的时间)和Cdraw()成员(返回下一张扑克牌),另外还有一个合适的Show()函数。请定义这些类和方法以及其他必要的方法(如用于设置对象值的方法),并使用一个类似于程序清单14.12的简单程序对它们进行测试。

5.下面是一些类声明:
(··········代码省略········)
注意,该类层次结构使用了带虚基类的MI,所以要牢记这种情况下用于构造函数初始化列表的特殊规则。还需要注意的是,有些方法被声明为保护的。这可以简化一些HighFink方法的代码(例如,如果HightFink::showAll()只是调用Fink::showAll()和Manager::showAll(),则它将调用abstr_emp::ShowAll()两次)。提供类方法的实现,并在一个程序中对这些类进行测试。下面是一个小型测试程序:
(··········代码省略········)
为什么没有定义赋值运算符?
为什么要将ShowAll()和SetAll()定义为虚的?

为什么要将abstr_emp定义为虚基类?

为什么highfink类没有数据部分?

为什么只需一个operator<<()版本?

如果使用下面的代码替换程序的结尾部分,将会发生什么情况?

abstr_emp str[4] = {em, fi, hf, hf2};
for(int i = 0; i < 4; ++i)
tr[i].showAll()

程序

//winec.h
#ifndef WINEC_H_
#define WINEC_H_

#include <iostream>
#include <valarray>
#include <string>

template <class T1,class T2>
class Pair
{
   
private:
	T1 a;
	T2 b;
public:
	Pair(const T1 &aval,const T2 &bval):a(aval),b(bval){
   }
	Pair(){
   }
	void set(const T1 &yr,const T2 &bot);
	int sum() const;
	void Show(int y) const;	
};

typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt,ArrayInt> PairArray;

class Wine
{
   
private:
	std::string name;
	PairArray b;
	int yrs;
public:
	Wine(const char *l,int y,const int yr[],const int bot[]);
	Wine(const char *l,int y);
	void GetBottles();
	std::string & Label();
	int sum() const;
	void Show() const;	
};

#endif 
//winec.cpp
#include "winec.h"
#include <iostream>
#include <string>
#include <valarray>
using namespace std;

template <class T1,class T2>
void Pair<T1,T2>::set(const T1 &yr,const T2 &bot)
{
   
	a=yr;
	b=bot;
}

template <class T1,class T2>
int Pair<T1,T2>::sum() const
{
   
	return b.sum();
}

template <class T1,class T2>
void Pair<T1,T2>::Show(int y) const
{
   
	for(int i=0;i<y;i++)
	{
   
		cout<<"\t\t"<<a[i]<<"\t\t"<<b[i]<<endl;
	}
}

Wine::Wine(const char *l,int y,const int yr[],const int bot[])
{
   
	name=l;
	yrs=y;
	b.set(ArrayInt(yr,yrs),ArrayInt(bot,yrs));
}
Wine::Wine(const char *l,int y)
{
   
	name=l;
	yrs=y;
}
void Wine::GetBottles()
{
   
	ArrayInt yr(yrs),bot(yrs);
	cout<<"Enter"<<name<<" data for "<<yrs<<" year(s):\n";
	for(int i=0;i<yrs;i++)
	{
   
		cout<<"Enter year: ";
		cin>>yr[i];
		cout<<"Enter bottles for that year: ";
		cin>>bot[i];
	}
	b.set(yr,bot);
}
string & Wine::Label()
{
   
	return name;
}
int Wine::sum() const
{
   
	return b.sum();
}
void Wine::Show() const
{
   
	cout<<"Wine: "<<name<<endl;
	cout<<"\t\tYear\t\tBottles\n";
	b.Show(yrs);
}
//main.cpp
#include <iostream>
#include "winec.h"

int main(void)
{
   
	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;
}

2.主程序同1。

//wine.h
#ifndef WINEC_H_
#define WINEC_H_

#include <iostream>
#include <valarray>
#include <string>

template <class T1,class T2>
class Pair
{
   
private:
	T1 a;
	T2 b;
public:
	Pair(const T1 &aval,const T2 &bval):a(aval),b(bval){
   }
	Pair(){
   }
	void set(const T1 &yr,const T2 &bot);
	int sum() const;
	void Show(int y) const;	
};

typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt,ArrayInt> PairArray;

class Wine:private std::string,private PairArray
{
   
private:
	int yrs;
public:
	Wine(const char *l,int y,const int yr[],const int bot[]);
	Wine(const char *l,int y);
	void GetBottles();
	std::string & Label();
	int sum(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值