C++ Primer Plus (第六版)编程练习记录(chapter14 C++中的代码重用)

本文通过C++ Primer Plus第六版的编程练习,探讨了类的设计与重用,包括Wine类的实现,私有继承的应用,模板QueueTp的创建,Person类及其派生类Gunslinger、PokerPlayer、BadDude的定义,以及一个类层次结构的实例。内容涵盖了虚基类、多重继承、保护方法、抽象类和虚函数的使用。
摘要由CSDN通过智能技术生成

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

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

这样,PairArray表示的是类型Pair<std::valarray, std::valarray >。使用包含来实现Wine类,并用一个简单的程序对其进行测试。Wine类应该有一个默认构造函数以及如下构造函数:

Wine(const char* l, int y, const int yr[], const int bot[]);
Wine(const char* l, int y);

Wine类应该有一个GetBottles( )方法,它根据Wine对象能够存储几种年份(y),提示用户输入年份和瓶数。方法Label( )返回一个指向葡萄酒名称的引用。sum( )方法返回Pair对象中第二个valarray对象中的瓶数总和。
测试程序应提示用户输入葡萄酒名称、元素个数以及每个元素存储的年份和瓶数等信息。程序将使用这些数据来构造一个Wine对象,然后显示对象中保存的信息。
下面是一个简单的测试程序:

int main()
{
   
    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 bottle for " << more.Label()
    <<": " << more.sum() << endl;
    cout << "Bye\n";
    
    return 0;
}

下面是该程序的运行情况:

Enter name of wine: Gully Wash
Enter number of years: 4
Enter Gully Wash data for 4 year(s):
Enter year: 1988
Enter bottles for that year: 42
Enter year: 1994
Enter bottles for that year: 58
Enter year: 1998
Enter bottles for that year: 122
Enter year: 2001
Enter bottles for that year: 144
Wine: Gully Wash
	Year Bottles
	1988 42
	1994 58
	1998 122
	2001 144
Wine: Gushing Grape  Red
	Year Bottles
	1993 48
	1995 60
	1998 72
Total bottle for Gushing Grape  Red: 180
Bye
//clss.h
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
#include <valarray>
#include <string>

class Wine
{
   
	typedef std::valarray<int> ArrayInt;
	typedef std::pair<ArrayInt, ArrayInt> PairArray;

public:
	Wine(const char* l, int y, const int yr[], const int bot[]);
	Wine(const char* l, int y);
	~Wine() {
   };

	void GetBottles();
	std::string& Label();
	int sum()const;
	void Show()const;

private:
	std::string label;
	PairArray info;
	int yearnum;
};


#endif // !CLASS_H_
//class.cpp
#include"class.h"

Wine::Wine(const char* l, int y, const int yr[], const int bot[])
{
   
	label = l;
	yearnum = y;
	info.first.resize(yearnum);
	info.second.resize(yearnum);
	for (int i = 0; i < y; i++)	
	{
   
		info.first[i] = yr[i];
		info.second[i] = bot[i];
	}
}

Wine::Wine(const char* l, int y)
{
   
	label = l;
	yearnum = y;
}

void Wine::GetBottles()
{
   
	using namespace std;
	cout << "Enter " << label << " data for " << yearnum << " year(s):\n";

	info.first.resize(yearnum);	//这一步非常重要,不重新设置长度的话会退出
	info.second.resize(yearnum);

	for (int i = 0;i < yearnum;i++)
	{
   
		cout << "Enter year: ";
		cin >> info.first[i];

		cout << "Enter bottles for that year: ";
		cin >> info.second[i];
	}
}

std::string& Wine::Label() 
{
   
	return label;
}

int Wine::sum() const
{
   
	return info.second.sum();
}

void Wine::Show() const
{
   
	using namespace std;
	cout << "Wine: " << label << endl;;
	cout << "\tYear\tBottles\n";
	for (int i = 0;i < yearnum;i++)
	{
   
		cout << '\t'<<info.first[i];
		cout << '\t' << info.second[i] << endl;
	}
}
//main.cpp
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/5/6
* 描述:
************************************************* */
#include<iostream>
#include"class.h"
using namespace std;


int main()
{
   
	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 bottle for " << more.Label()
		<< ": " << more.sum() << endl;
	cout << "Bye\n";

	return 0;
}

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

PairArray::operator=(PairArray(ArrayInt(),ArrayInt()));
cout<<(const string& )(*this);

您设计的类应该可以使用编程练习1中的测试程序进行测试。

//clss.h
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
#include <valarray>
#include <string>

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

class Wine :private std::string, private PairArray
{
   
public:
	Wine(const char* l, int y, const int yr[], const int bot[]);
	Wine(const char* l, int y);
	~Wine() {
   }<
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值