#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++代码重用——包含的更多相关文章

  1. “前.NET Core时代”如何实现跨平台代码重用 ——程序集重用

    除了在源代码层面实现共享("前.NET Core时代"如何实现跨平台代码重用 --源文件重用)之外,我们还可以跨平台共享同一个程序集,这种独立于具体平台的"中性" ...

  2. PHP代码重用与函数编写

    代码重用与函数编写 1.使用require()和include()函数 这两个函数的作用是将一个文件爱你载入到PHP脚本中,这样就可以直接调用这个文件中的方法.require()和include()几 ...

  3. 从零开始攻略PHP(6)——代码重用与函数编写的一些注意事项

    一个新的项目是这样创建的:它将已有的可重新利用的组件进行组合,并将新的开发难度降低到最小. 代码重用的好处:降低成本.提升可靠性和一致性. 1.使用require()和include()函数 使用一条 ...

  4. [置顶] Android项目组织和代码重用

    在Android应用开发过程中,只要涉及两个或以上人的开发,就需要考虑分工和代码的组织和重用问题. 代码重用有三种方式: 1.APK: 2.JAR:通过Libs/ 和Build path集成,缺点是不 ...

  5. 52. 模版和设计元素——Lotus Notes的代码重用

    不论是理论上还是实用上,代码重用都是编程的一个重要议题.可以从两个角度来讨论代码重用. 一是逻辑上代码以怎样的方式被重用.既可以通过面向对象的思想普及以来耳熟能详的继承的方式.比如先建了一个车的基类, ...

  6. Java 代码重用:操作与上下文重用

    目录 操作重用 参数化操作 上下文重用 上下文作为模板方法 结束语 我几乎不需要讨论为什么重用代码是有利的.代码重用(通常)会导致更快的开发与更少的 BUG.一旦一段代码被封装和重用,那么检查程序是否 ...

  7. php学习----错误处理和代码重用

    php错误处理 一.错误分类:1.语法错误 2.运行时错误 3.逻辑错误 错误代号(部分): 所有看到的错误代码在php中都被定义为系统常量(可以直接使用) 1)系统错误 E_PARSE:编译错误,代 ...

  8. 《C++ Primer Plus》读书笔记之十二—C++中的代码重用

    第14章 C++中的代码重用 1.C++代码重用方法:公有继承.使用本身是另一个类的对象的类成员(这种方法称为包含.组合或层次化).私有或保护继承.类模板等. 2.模板特性意味着声明对象时,必须指定具 ...

  9. C++_代码重用1-总览

    C++的主要目的是促进代码重用. 公有继承是实现这一目标的机制之一: 本身是另一个类的成员,这种方法称为包含.组合.层次化. 另一种方法是使用私有.保护继承. 通常包含.私有继承和保护继承用于实现ha ...

随机推荐

  1. Java设计模式-外观模式(Facade)

    外观模式是为了解决类与类之家的依赖关系的,像spring一样,可以将类和类之间的关系配置到配置文件中,而外观模式就是将他们的关系放在一个Facade类中,降低了类类之间的耦合度,该模式中没有涉及到接口 ...

  2. 【转】Eclipse下导入外部jar包的3种方式

    我们在用Eclipse开发程序的时候,经常要用到第三方jar包.引入jar包不是一个小问题,由于jar包位置不清楚,而浪费时间.下面配图说明3种Eclipse引入jar包的方式.   1.最常用的普通 ...

  3. 理解SIFT

    理解SIFT.tab{font-size:12px; margin-bottom: 10px;}.tab a{cursor:pointer;cursor:pointer;display:inline- ...

  4. Vijos1056 图形面积

    描述 桌面上放了N个平行于坐标轴的矩形,这N个矩形可能有互相覆盖的部分,求它们组成的图形的面积. 格式 输入格式 输入第一行为一个数N(1≤N≤100),表示矩形的数量.下面N行,每行四个整数,分别表 ...

  5. eclipse中新建python项目报错:Project interpreter not specified

    eclipse-windows-preferences-python

  6. C# WPF 显示图片和视频显示 EmuguCv、AForge.Net测试(续)

    介绍 本文是接着上文<C# WPF 显示图片和视频显示 EmuguCv.AForge.Net测试>写的,建议先看下上文,因为有些代码还需要了解. 增添 接着上文的代码,我们可以在事件处理方 ...

  7. LINUX下为ORACLE数据库设置大页--hugepage

    在Linux中配置hugepage可以提高oracle的性能,减少oracle sga的页交换,类似于aix中的lagepage. 为什么 使用大页? LINUX内存的默认块大小是4K如果SGA为:1 ...

  8. 工匠若水 Android应用开发编译框架流程与IDE及Gradle概要

    http://blog.csdn.net/yanbober/article/details/45306483 http://blog.csdn.net/yanbober/article/details ...

  9. JavaScript中的位置坐标

    参考来源 http://www.cnblogs.com/tugenhua0707/p/4501843.html screenX.screenY:浏览器屏幕水平.垂直坐标(相对于浏览器内整个屏幕,包括地 ...

  10. hdu 2049 不容易系列之(4)——考新郎

    在本博AC代码中,求CNM用的是Anm/amm没用阶乘的形式,两者皆可 #include <stdio.h> int main(void) { long long a,b,larr[21] ...