C++代码重用——包含
#ifndef PAIRS_H
#define PAIRS_H #include <iostream>
#include <valarray> 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() {}
void Set_Pair(const T1 & aval,const T2 & bval);
void Show(int ys);
int Sum(void);
}; template <class T1,class T2>
T1 & Pair<T1,T2>::first()
{
return a;
} template <class T1,class T2>
T2 & Pair<T1,T2>::second()
{
return b;
} template <class T1,class T2>
void Pair<T1,T2>::Set_Pair(const T1 & aval,const T2 & bval)
{
a=aval;
b=bval; } template <class T1,class T2>
void Pair<T1,T2>::Show(int ys)
{
int i=;
for (;i<ys;i++)
{
cout <<"\t "<<a[i]<<"\t"<<b[i] <<endl;
} } template <class T1,class T2>
int Pair<T1,T2>::Sum(void)
{
return b.sum();
}
#endif
#ifndef WINE_H
#define WINE_H #include <string>
#include "pairs.h"
#include <valarray>
using namespace std; class wine
{
typedef valarray<int> ArrayInt;
typedef Pair<ArrayInt,ArrayInt> PairArray;
private:
string name;
PairArray year_bottles;
int years;
public:
wine(){}
wine(const char *l,int y,const int yr[],const int bot[]);
wine(const char *l,int y);
void GetBottles(void);
void Show(void);
int sum(void);
string Label(void){return name;}
};
#endif
#include"wine.h"
#include"pairs.h"
#include<iostream>
//#include <stdlib>
#include<string> using namespace std; wine::wine(const char *l,int y,const int yr[],const int bot[])
{
name=*l;
years=y;
year_bottles.Set_Pair(ArrayInt(yr,y),ArrayInt(bot,y));
}
wine::wine(const char *l,int y)
{
name=*l;
years=y;
}
void wine::GetBottles(void)
{
int i=;
ArrayInt year(years),bottle(years);
cout << "Enter "<< name <<"data for "<<years <<" year(s):" <<endl;
for(;i<years;i++)
{
cout << "Enter year: ";
cin >>year[i];
//cout << endl;
cout << "Enter bottles for that year: ";
cin >>bottle[i];
//cout << endl;
}
year_bottles.Set_Pair(year,bottle);
}
void wine::Show(void)
{ cout << "wine: "<< name <<endl;
cout <<"\t Year\t Bottles" <<endl;
year_bottles.Show(years); } int wine::sum(void)
{
int s=;
s=year_bottles.Sum();
return s;
}
#include "wine.h"
#include <iostream> using namespace std; int main(void)
{
cout << "Enter name of wine: ";
char lab[];
cin.getline(lab,);
cin.sync();
cout << "Enter number of years: ";
int yrs;
cin >> yrs; wine holding(lab,yrs);
holding.GetBottles();
holding.Show(); const int YRS=;
int y[YRS]={,,};
int b[YRS]={,,}; wine more("Gushing Grape Red",YRS,y,b);
more.Show();
cout << "Total bottles for " <<more.Label()
<< ": " <<more.sum() <<endl;
cout <<"Bey\n";
return ;
}
wine类包含string和Pair两个类,前者用于存储酒名,后者有2个valarray<int>对象,分别用于存储酿造年份和该年的瓶数。
C++代码重用——包含的更多相关文章
- “前.NET Core时代”如何实现跨平台代码重用 ——程序集重用
除了在源代码层面实现共享("前.NET Core时代"如何实现跨平台代码重用 --源文件重用)之外,我们还可以跨平台共享同一个程序集,这种独立于具体平台的"中性" ...
- PHP代码重用与函数编写
代码重用与函数编写 1.使用require()和include()函数 这两个函数的作用是将一个文件爱你载入到PHP脚本中,这样就可以直接调用这个文件中的方法.require()和include()几 ...
- 从零开始攻略PHP(6)——代码重用与函数编写的一些注意事项
一个新的项目是这样创建的:它将已有的可重新利用的组件进行组合,并将新的开发难度降低到最小. 代码重用的好处:降低成本.提升可靠性和一致性. 1.使用require()和include()函数 使用一条 ...
- [置顶] Android项目组织和代码重用
在Android应用开发过程中,只要涉及两个或以上人的开发,就需要考虑分工和代码的组织和重用问题. 代码重用有三种方式: 1.APK: 2.JAR:通过Libs/ 和Build path集成,缺点是不 ...
- 52. 模版和设计元素——Lotus Notes的代码重用
不论是理论上还是实用上,代码重用都是编程的一个重要议题.可以从两个角度来讨论代码重用. 一是逻辑上代码以怎样的方式被重用.既可以通过面向对象的思想普及以来耳熟能详的继承的方式.比如先建了一个车的基类, ...
- Java 代码重用:操作与上下文重用
目录 操作重用 参数化操作 上下文重用 上下文作为模板方法 结束语 我几乎不需要讨论为什么重用代码是有利的.代码重用(通常)会导致更快的开发与更少的 BUG.一旦一段代码被封装和重用,那么检查程序是否 ...
- php学习----错误处理和代码重用
php错误处理 一.错误分类:1.语法错误 2.运行时错误 3.逻辑错误 错误代号(部分): 所有看到的错误代码在php中都被定义为系统常量(可以直接使用) 1)系统错误 E_PARSE:编译错误,代 ...
- 《C++ Primer Plus》读书笔记之十二—C++中的代码重用
第14章 C++中的代码重用 1.C++代码重用方法:公有继承.使用本身是另一个类的对象的类成员(这种方法称为包含.组合或层次化).私有或保护继承.类模板等. 2.模板特性意味着声明对象时,必须指定具 ...
- C++_代码重用1-总览
C++的主要目的是促进代码重用. 公有继承是实现这一目标的机制之一: 本身是另一个类的成员,这种方法称为包含.组合.层次化. 另一种方法是使用私有.保护继承. 通常包含.私有继承和保护继承用于实现ha ...
随机推荐
- 设置java jvm(虚拟机) 的内存在大小
package WanWan; public class Test { /** * @param args */ public static void main(String[] args) { // ...
- Linux中使用crontab命令定时执行shell脚本或其他Linux命令
使用crontab你可以在指定的时间执行一个shell脚本或者一系列Linux命令.例如系统管理员安排一个备份任务使其每天都运行 如何往 cron 中添加一个作业? # crontab –e0 5 * ...
- 【HDU 5399】Too Simple
题 Description Rhason Cheung had a simple problem, and asked Teacher Mai for help. But Teacher Mai th ...
- Angulajs系列-01-入门
1.解决什么问题? a, controller的各种的创建 b,体验angular的双向绑定 2.怎么解决 2.1 引入angularjs 下载地址 2.2 创建controller的方法 2.2.1 ...
- poj 3463 最短路与次短路&&统计个数
题意:求最短路和比最短路长度多1的次短路的个数 本来想图(有)方(模)便(版)用spfa的,结果妹纸要我看看dijkstra怎么解.... 写了三遍orz Ver1.0:堆优化+邻接表,WA //不能 ...
- Intel 80x86 Linux Kernel Interrupt(中断)、Interrupt Priority、Interrupt nesting、Prohibit Things Whthin CPU In The Interrupt Off State
目录 . 引言 . Linux 中断的概念 . 中断处理流程 . Linux 中断相关的源代码分析 . Linux 硬件中断 . Linux 软中断 . 中断优先级 . CPU在关中断状态下编程要注意 ...
- 腾讯云ubuntu下mysqli服务的开启
腾讯云ubuntu下mysqli服务的开启 今天晚上搞了好久,在本地操作系统deepin下操作完全无需开启mysqli模块,自动就开启了.这次介绍一下服务器ubuntu下mysqli模块的开启. 首先 ...
- java 文件读取大全
1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile { /** * 以字节为单位读取文件,常用 ...
- easyui datagrid 通过复选框删除新追加的数据问题
之前写好的功能在保存好数据后再通过复选框删除是没有问题的,可现在想多追加几行,然后选择删除新追加的某几行或一行,通过$('#dg').datagrid('getChecked')方法返回选中行,然而返 ...
- 实时获取UITextField内容
在UISearchBar中,当输入信息改变时,它就会调用textDidChange方法, 但是UITextField没有这个功能,要实现就得手动addTarget,其实controlevent里还有很 ...