【C++ Primer Plus习题】14.2

大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←

问题:

解答:

main.cpp

cpp 复制代码
#include <iostream>
#include "wine.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 bottles for " << more.Label() << ":;" << more.sum() << endl;
	cout << "Bye!" << endl;

	return 0;
}

wine.h

cpp 复制代码
#include <iostream>
#include <string>
#include <valarray>

using namespace std;

template <class T1, class T2> class Pair;
typedef valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;

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) {}
	Pair() {}
};

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

wine.cpp

cpp 复制代码
#include "wine.h"

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

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

Wine::Wine(const char* l, int y, const int yr[], const int bot[]) :string(l),PairArray(ArrayInt(yr, y), ArrayInt(bot, y))
{
	this->year = y;
}
Wine::Wine(const char* l, int y) :string(l),PairArray(ArrayInt(0, 0), ArrayInt(0, 0))
{
	this->year = y;
}
void Wine::GetBottles()
{
	cout << "Enter " << (const string&)(*this) << " data for" << year << "year:" << endl;
	this->first().resize(year);
	this->second().resize(year);
	for (int i = 0; i < year; i++)
	{
		cout << "Enter year: ";
		cin >> this->first()[i];
		cout << "Enter bottles for that year: ";
		cin >> this->second()[i];
	}
}
const string& Wine::Label()const
{
	return (const string&)(*this);
}
int Wine::sum()const
{
	return this->second().sum();
}
void Wine::show()
{
	cout << "Wine: " << (const string&)(*this) << endl;
	cout << " Year Bottles" << endl;
	for (int i = 0; i < year; i++)
	{
		cout << " " << this->first()[i] << "   " << this->second()[i] << endl;
	}
}

运行结果:

考查点:

  • 私有继承

注意:

  • 私有继承构造函数的初始化用初始化列表,用类名.
  • 强制转化为基类对象,因为私有继承无法访问数据成员.cout输出基类对象,<<运算符重载.

2024年9月9日21:18:42

相关推荐
沐知全栈开发6 分钟前
PHP 超级全局变量
开发语言
Deng9452013143 小时前
基于Python的职位画像系统设计与实现
开发语言·python·文本分析·自然语言处理nlp·scrapy框架·gensim应用
一只小青团6 小时前
Python之面向对象和类
java·开发语言
qq_529835356 小时前
ThreadLocal内存泄漏 强引用vs弱引用
java·开发语言·jvm
景彡先生6 小时前
C++并行计算:OpenMP与MPI全解析
开发语言·c++
不太可爱的叶某人7 小时前
【学习笔记】MySQL技术内幕InnoDB存储引擎——第5章 索引与算法
笔记·学习·mysql
岁岁岁平安7 小时前
Redis基础学习(五大值数据类型的常用操作命令)
数据库·redis·学习·redis list·redis hash·redis set·redis string
归去_来兮8 小时前
深度学习模型在C++平台的部署
c++·深度学习·模型部署
量子联盟8 小时前
原创-基于 PHP 和 MySQL 的证书管理系统,免费开源
开发语言·mysql·php
知识分享小能手9 小时前
Vue3 学习教程,从入门到精通,使用 VSCode 开发 Vue3 的详细指南(3)
前端·javascript·vue.js·学习·前端框架·vue·vue3