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 ...
随机推荐
- python 参数
摘自:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00137473844933 ...
- Svn-如何清除eclipse中保存的svn用户名和密码
1. 查看你的Eclipse中使用的是什么SVN Interface windows > prefeSence > Team > SVN #SVN Interface 2. 如果是用 ...
- Oracle中的伪列
分页查询中,需要用到伪列rownum,代码如下: select * from (select rownum rn, name from cost where rownum <= 6) where ...
- codevs1225 八数码难题
题目描述 Description Yours和zero在研究A*启发式算法.拿到一道经典的A*问题,但是他们不会做,请你帮他们.问题描述 在 3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数 ...
- 总结css之内联图片的优缺点
会不会有这样一种感觉?IT技术开发知识面很广也很深,总会有你不懂得问题出现.一个接着一个新的问题,一个接着一个新的挑战. 今天,解读[内联图片],什么是内联图片,使用内联图片的优缺点是什么?这个问题是 ...
- DRUPAL-PSA-CORE-2014-005 && CVE-2014-3704 Drupal 7.31 SQL Injection Vulnerability /includes/database/database.inc Analysis
目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Use Drupal to build everything from perso ...
- python中对字典按照value排序
age = {'a':24, 'd':35, 'c':12} print sorted(age.items(),key = lambda age:age[1]) 使用到了lambda和sorted函数 ...
- 求一元二次方程ax^2+bx+c=0的解
Console.WriteLine("求解方程ax^2+bx+c=0的解."); Console.WriteLine("请分别输入a,b,c的值(注意每输入一个值按一下回 ...
- mysql 外键(FOREIGN KEY)
最近有开始做一个实验室管理系统,因为分了几个表进行存储·所以要维护表间的关联··研究了一下MySQL的外键. (1)只有InnoDB类型的表才可以使用外键,mysql默认是MyISAM,这种类型不支持 ...
- SYN攻击TIME_WAIT防御。
#! /bin/bash /bin/netstat -anput | grep TIME_WAIT >time_wait.txt /bin/cat time_wait.txt |awk '{pr ...