【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

相关推荐
长友cy2 分钟前
Qt官方示例BluetoothLowEnergyGame源码阅读
开发语言·qt
是隼人6 分钟前
buuctf-pwn PWN1题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
HugoStudio_SWAN10 分钟前
洛谷 P1321 单词覆盖还原——从蜡板上的密信到数字水印
c++·学习·程序人生·算法
草青工作室10 分钟前
java-不携带Token也能“打“自己的接口:一个绕开网关鉴权、用 URL 直接调度 SpringMVC 方法的轻量级定时任务方案
java·开发语言
shmily麻瓜小菜鸡11 分钟前
JavaScript / TypeScript 易踩坑知识点完全指南 -假值(Falsy Values)完全指南
开发语言·javascript·typescript
春风解人意13 分钟前
从零开始学习嵌入式P33----网络基础之HTTP
网络·学习·http
Coodor16 分钟前
信创系统基于QT5操作NFC读写器
开发语言·qt·智能卡·nfc读写器·yw-607hc
励志不掉头发的内向程序员19 分钟前
【LibreCAD 2D架构】从鼠标点击到屏幕像素:LibreCAD绘图架构全链路解析之Action与命令系统
linux·开发语言·c++·qt·学习·系统架构
小道士写程序26 分钟前
自然语言处理NLP - 第8章 词向量与神经网络:从手工特征到学习表示
神经网络·学习·自然语言处理
民乐团扒谱机28 分钟前
【超详细】PyQt6 实现 AU 风格波形图编辑器:多级缩放与采样点逐级渲染,附全过程代码
开发语言·python·编辑器·pyqt·audition·时域·频谱图