C++ namespace的用法
//namesp.h
namespace pers{
const int LEN = 40; struct Person{ char fname[LEN]; char lname[LEN]; };
void getPerson(Person &);
void showPerson(const Person &); }
namespace debts{
using namespace pers; struct Debt{ Person name; double amount; };
void getDebt(Debt &);
void showDebt(const Debt &);
double sumDebts(const Debt ar[], int n); }
第二个文件:
//namesp.cpp
#include <iostream>#include "namesp.h"
namespace pers{ using std::cout; using std::cin;
void getPerson(Person &rp){ cout<<"Enter first name: "; cin>>rp.fname;
cout<<"Enter last name: "; cin>>rp.lname; }
void showPerson(const Person &rp){
cout<<rp.lname<<", "<< rp.fname; } }
namespace debts{ using std::cout; using std::cin; using std::endl;
void getDebt(Debt & rd){ getPerson(rd.name); cout<< "Enter debt: "; cin>>rd.amount; }
void showDebt(const Debt &rd){ showPerson(rd.name);
cout<<": $"<<rd.amount<<endl; }
double sumDebts(const Debt ar[], int n){ double total = 0;
for(int i = 0; i < n; i++){ total += ar[i].amount; }
return total; } }
第三个文件:
//namessp.cpp
#include <iostream>#include "namesp.h"
void other (void); void another(void);
int main(void) {
using debts::Debt; using debts::showDebt;
Debt golf = { {"Benny","Goatsniff"},120.0}; showDebt(golf); other(); another();
return 0; }
void other(void) {
using std::cout; using std::endl;
using namespace debts;
Person dg = {"Doodle", "Glister"}; showPerson(dg); cout<<endl; Debt zippy[3]; int i;
for(i = 0; i < 3; i++){ getDebt(zippy[i]); }
for(i = 0; i < 3; i++){ showDebt(zippy[i]); }
cout<<"Total debt: $" <<sumDebts(zippy,3)<<endl; return; }
void another(void){ using pers::Person;
Person collector = {"Milo", "Rightshift"}; pers::showPerson(collector); std::cout<<std::endl; }
C++鼓励程序员在开发程序时使用多个文件.一种有效的组织策略是,使用头文件来定义用户类型,为操纵用户类型 的函数 提供函数原型;并将函数 定义放在一个独立的源代码当中.头文件和源代码文件一起定义和实现了用户定义的类型 及其使用方式.最后,将main()和其他这些函数的函数放在第三个文件中.
C++ namespace的用法的更多相关文章
- C++ 之namespace常见用法
一.背景 需要使用Visual studio的C++,此篇对namespace的常用用法做个记录. 二.正文 namespace通常用来给类或者函数做个区间定义,以使编译器能准确定位到适合的类或者函数 ...
- namespace的用法
C++中采用的是单一的全局变量命名空间.在这单一的空间中,如果有两个变量或函数的名字完全相同,就会出现冲突.当然,你也可以使用不同的名字,但有时我们并不知道另一个变量也使用完全相同的名字:有时为了程序 ...
- namespace的作用
namespace的用法 1.什么是命名空间 通常我们学c++的时候经常看见头文件下有一句using namespace std,有什么用呢? 例如: #include<iostream> ...
- Mybatis高级查询之关联查询
learn from:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#Result_Maps 关联查询 准备 关联结果查询(一对一) resul ...
- Mybatis-mapper-xml-基础
今天学习http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html.关于mapper.xml的sql语句的使用. 项目路径:https://github.c ...
- winform 异步读取数据 小实例
这几天对突然对委托事件,异步编程产生了兴趣,大量阅读前辈们的代码后自己总结了一下. 主要是实现 DataTable的导入导出,当然可以模拟从数据库读取大量数据,这可能需要一定的时间,然后 再把数据导入 ...
- C++命名空间<转>
熟练掌握C/C++语言,熟悉Windows开发平台,能熟练运用MFC自主编开发出一些应用程序: 熟练掌握SQL语句,对数据库有很好的认识,能熟练使用SQL Server2000软件: 熟练掌握JAVA ...
- (转)struts2.0配置文件、常量配置详解
一.配置: 在struts2中配置常量的方式有三种: 在struts.xml文件中配置 在web.xml文件中配置 在sturts.propreties文件中配置 1.之所以使用struts.prop ...
- Django之反向生成url
首先新建一个项目test_url,项目包含一个名为app01的应用 在urls.py文件中生成如下内容 from django.conf.urls import url from django.sho ...
随机推荐
- (原)docker的一个“Driver aufs failed to remove...”问题的解决
1. /var/lib/docker/aufs/mnt下的目录不能乱删! /var/lib/docker/aufs/diff下的目录删了就死了!!!!!2. 尽量不要用docker tag -f 这 ...
- nexus maven私服搭建
1.在服务器上安装jdk 2.下载 nexus-3.14.0-04-unix.tar.gz,并上传到服务器/opt目录 3.解压 tar -zxvf nexus-3.14.0-04-unix.tar. ...
- MIME详解
MIME详解 原文:http://blog.csdn.net/cxm_hwj/article/details/6690058 MIME,英文全称为“Multipurpose Internet Mail ...
- 【FindReport】图表快速部署开发
在线帮助文档:http://help.finereport.com/ 开发部署环境:Java Tomcat 数据可视化工具 大屏动态显示
- 初学SSH 配置+错误总结
初学java web 一上手就接触ssh,前段时间,断断续续配置好了ssh,今天从Hibernate入手开始学习.先总结今天遇到的问题. 配置链接的是sqlserver的数据库,首先是下载了一个jdb ...
- Pandas.plot 做图 demo(scatter,bar,pie)
#coding:utf-8import numpy as npimport matplotlib.pyplot as pltplt.rcParams['font.sans-serif']=['SimH ...
- Maven 统一指定jar包版本的方法
在看别人的源码的过程中,会遇到这种情况,就是很多jar包没有指定版本,却能够下载下来. 在后来的研究中发现,有这样一个配置. <parent> <groupId>org.spr ...
- PHP判断ajax请求:HTTP_X_REQUESTED_WITH
PHP判断ajax请求的原理: 在发送ajax请求的时候,我们可以通过XMLHttpRequest这个对象,创建自定义的 header头信息, 在jquery框架中,对于通过它的$.ajax, $.g ...
- 调整Intellij IDEA内存
最近IDEA真是卡的要死,下面
- kubernetes应用部署原理
Kubernetes应用部署模型解析(原理篇) 十多年来Google一直在生产环境中使用容器运行业务,负责管理其容器集群的系统就是Kubernetes的前身Borg.其实现在很多工作在Kubernet ...