//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的用法的更多相关文章

  1. C++ 之namespace常见用法

    一.背景 需要使用Visual studio的C++,此篇对namespace的常用用法做个记录. 二.正文 namespace通常用来给类或者函数做个区间定义,以使编译器能准确定位到适合的类或者函数 ...

  2. namespace的用法

    C++中采用的是单一的全局变量命名空间.在这单一的空间中,如果有两个变量或函数的名字完全相同,就会出现冲突.当然,你也可以使用不同的名字,但有时我们并不知道另一个变量也使用完全相同的名字:有时为了程序 ...

  3. namespace的作用

    namespace的用法 1.什么是命名空间 通常我们学c++的时候经常看见头文件下有一句using namespace std,有什么用呢? 例如: #include<iostream> ...

  4. Mybatis高级查询之关联查询

    learn from:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#Result_Maps 关联查询 准备 关联结果查询(一对一) resul ...

  5. Mybatis-mapper-xml-基础

    今天学习http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html.关于mapper.xml的sql语句的使用. 项目路径:https://github.c ...

  6. winform 异步读取数据 小实例

    这几天对突然对委托事件,异步编程产生了兴趣,大量阅读前辈们的代码后自己总结了一下. 主要是实现 DataTable的导入导出,当然可以模拟从数据库读取大量数据,这可能需要一定的时间,然后 再把数据导入 ...

  7. C++命名空间<转>

    熟练掌握C/C++语言,熟悉Windows开发平台,能熟练运用MFC自主编开发出一些应用程序: 熟练掌握SQL语句,对数据库有很好的认识,能熟练使用SQL Server2000软件: 熟练掌握JAVA ...

  8. (转)struts2.0配置文件、常量配置详解

    一.配置: 在struts2中配置常量的方式有三种: 在struts.xml文件中配置 在web.xml文件中配置 在sturts.propreties文件中配置 1.之所以使用struts.prop ...

  9. Django之反向生成url

    首先新建一个项目test_url,项目包含一个名为app01的应用 在urls.py文件中生成如下内容 from django.conf.urls import url from django.sho ...

随机推荐

  1. python List使用

    1.enumerate 用在遍历中,返回下标和数据 name_arr = ["shijingjing", "renjiangfeng", "anqi& ...

  2. nohup 后台启动程序,并输出到指定日志

    1.启动程序并输入到指定日志 nohup python manage.py runserver 0.0.0.0:9090 > /data/zyj/xadstat/xadstat.log 2&am ...

  3. maven pom属性 详解

    pom.xml文件(实践用):<project xmlns="http://maven.apache.org/POM/4.0.0"   xmlns:xsi="htt ...

  4. html页面去掉滚动条

    有时候特别需要,个别网页要去掉横向滚动条和竖向滚动条,那该怎么去掉呢,很简单,看代码: 让竖条没有: <body style=`overflow:-Scroll;overflow-y:hidde ...

  5. OpenStack云计算(一)——OpenStack 网络

    关于OpenStack OpenStack它是 Rackspace Cloud 和 NASA 负责的一个开源基础架构即服务的云计算项目. OpenStack 是一个由开发者和云计算技术人员的全球协作开 ...

  6. Orcale分析函数OVER(PARTITION BY... ORDER BY...)的讲解

    顾名思义,PARTITION 中文是分割的意思,ORDER 是排序的意思,所以翻译一下就是先把一组数据按照制定的字段进行分割成各种组,然后组内按照某个字段排序. 以实际案例来说明这个函数的使用, 首先 ...

  7. 变量使用self.foo还是_foo

    selfOR_html, body {overflow-x: initial !important;}html { font-size: 14px; } body { margin: 0px; pad ...

  8. PostgreSQL学习手册(数据表)<转>

    一.表的定义: 对于任何一种关系型数据库而言,表都是数据存储的最核心.最基础的对象单元.现在就让我们从这里起步吧.    1. 创建表:    CREATE TABLE products (      ...

  9. Android ——Toolbar开发实践总结(转)

    过年前发了一篇介绍 Translucent System Bar 特性的文章 Translucent System Bar 的最佳实践 ,收到很多开发者的关注和反馈.今天开始写第二篇,全面的介绍一下  ...

  10. 使用 Easy Sysprep v4(ES4) 封装 Windows 7教程

      总:1.装系统ctrl+shift+f3(administrator)2.备份系统,快速备份(pe下)3.安装补丁,软件(系统下),不装安全类软件,需激活的软件重装后还要激活4.系统备份(pe下叠 ...