当前位置: 首页 > news >正文

【C++ Primer Plus习题】14.1

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

问题:

这里是引用
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

解答:
main.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

#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:string label;PairArray info;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

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

运行结果:
在这里插入图片描述

考查点:

  • 模版类
  • 类型重定义typedef
  • 组合
  • valarray模版类

注意:

  • 构造函数用的初始化列表进行初始化,且用了valarray构造函数
    在这里插入图片描述
  • valarray可重新指定数组大小
    在这里插入图片描述
  • valarray也可以计算数组总数
    在这里插入图片描述
  • 模版类一定要记得说明哦
    在这里插入图片描述

2024年9月9日19:49:55

http://www.lryc.cn/news/433943.html

相关文章:

  • 在Ubuntu上运行QtCreator相关程序
  • MybatisPlus 快速入门
  • Java.lang中的String类和StringBuilder类介绍和常用方法
  • notepad++软件介绍(含安装包)
  • chapter13-常用类——(章节小结)——day17
  • RTX AI PC 和工作站上部署多样化 AI 应用支持 Multi-LoRA
  • C++ STL-deque容器入门详解
  • 数据结构之折半查找
  • linux高级学习12
  • leetcode:3174 清除数字 使用栈,时间复杂度O(n)
  • 神经网络卷积操作
  • 专题二_滑动窗口_算法专题详细总结
  • 【机器学习-三-无监督学习】
  • JAVA基础:Lambda表达式(上)
  • Vue使用fetch获取本地数据
  • 《酒饮真经》秘籍4,让你的酒场技巧更上一层楼!
  • 回车符与快捷键记录
  • 计算机网络-VRRP工作原理
  • 6.5椒盐噪声
  • CSS样式的引用方式以及选择器使用
  • Python Flask_APScheduler定时任务的正确(最佳)使用
  • Linux命名管道
  • Xinstall助力App全渠道统计,参数传递下载提升用户体验!
  • 【时时三省】(C语言基础)指针进阶 例题4
  • k8s的配置管理
  • JAVA- 多线程
  • 【Qt】解决设置QPlainTextEdit控件的Tab为4个空格
  • elementUI根据列表id进行列合并@莫成尘
  • 基于人工智能的智能安防监控系统
  • 分享从零开始学习网络设备配置--任务6.3 使用基本ACL限制网络访问