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 ...
随机推荐
- Shell(6): 多线程操作及线程数
任务需要:当我需要对一个文件夹中的文件,分别压缩的时候: 原始的代码: #!/usr/bin/shell function getdir(){ for element in `ls $1` do #e ...
- 关于electron的跨域问题,有本地的图片的地址,访问不了本地的图片
项目中有上传图片功能,自定义input type=file 改变透明度和超出部分隐藏,把按钮和 点击的图标放在上传文件的按钮上面,然后又碰到到获取input里面的图片的本地的路径, 在electron ...
- Vue2键盘事件:keydown/keyup...
Vue2键盘事件:keydown/keyup... 1.使用 <!DOCTYPE html> <html> <head> <title></tit ...
- css基础 -文本溢出 text-overflow:ellipsis;
.className{overflow:hidden;white-space:nowrap;text-overflow:ellipsis;} 外部结构如下是就失效了:(移动端) <a class ...
- 使用spin.js优化等待ajax返回时的页面效果
[本文出自天外归云的博客园] 最近在做一个JIRA信息统计的系统,在统计JIRA关联信息的过程中由于需要等待ajax返回结果到前端,时间较长,所以要添加一段等待时的loading画面,使用spin.j ...
- Fiddler工具非常强大好用
传递一个json对象发post请求案例: 1.打开Fiddler,点击Composer选项卡 2.下拉框选择Post 3.输入请求的URL,比如:http://localhost:49194/api/ ...
- VC/Wince 实现仿Win8 Metro风格界面2——页面滑动切换(附效果图)
前几天开始写仿Win8 Metro界面文章,部分网友觉得不错,感谢各位的意见.本来今天一直在折腾Android VLC播放器,没时间写.不过明天休息,所以今天就抽时间先写一下. 言归正传,我们都知道W ...
- WebApi增删改查Demo
1.新建webapi项目 2.配置WebApiConfig public const string DEFAULT_ROUTE_NAME = "MyDefaultRoute"; p ...
- [转]bootstrap的table插件动态加载表头
原文地址:https://blog.csdn.net/abubu123/article/details/78060321 bootstrap的table属性已经很熟悉了,最近遇到一个问题,犹豫每个列表 ...
- Android——selector背景选择器的使用详解(二)
在开发应用中,很多情况下要设计listview或button控件的背景,下面总结一下android的selector的用法:1.在drawable中配置Android的selector.将如下的XML ...