//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. MonoBehaviour类Invoke, Coroutine

    异步函数 在一个方法执行时调用另一个方法.而被调用的方法或者其中的某些语句不是立刻执行,而是过一段时间后才执行. MonoBehaviour提供了两种异步方法 调用(Invoke) 协程(Corout ...

  2. iOS开发transform的使用

    // //  ViewController.m //  18-transform的使用 #import "ViewController.h" @interface ViewCont ...

  3. 【ARM】2440裸机系列-图片显示

    功能 LCD显示字汉字,字符和图片 说明 汉字,字符和图片需要用相应的取模软件得到相应的c文件,然后包含到工程中 主要代码   1)绘制背景 void Brush_ U32 c) { int x,y ...

  4. 练习1 Just Java

    任务:做这样一个界面,选择数量,自动计算价格.超级简单.. <?xml version="1.0" encoding="utf-8"?> <a ...

  5. [转]SpringMVC单文件上传、多文件上传、文件列表显示、文件下载

    一.新建一个Web工程,导入相关的包 springmvc的包+commons-fileupload.jar+connom-io.jar+commons-logging,jar+jstl.jar+sta ...

  6. Linux 系统内核空间与用户空间通信的实现与分析-NETLINK (转载)

    Linux中的进程间通信机制源自于Unix平台上的进程通信机制.Unix的两大分支AT&T Unix和BSD Unix在进程通信实现机制上的各有所不同,前者形成了运行在单个计算机上的Syste ...

  7. JProfiler 9版本注册码(亲测可用!!!)

    JProfiler 9版本注册码(亲测可用!!!) 按默认选择“Single or evaluation license” ,Name 和 Company 随意填!!! JProfiler 9.2  ...

  8. 【oneday_onepage】——Microsoft addresses DevOps with InRelease technology

    Microsoft addresses DevOps with InRelease technology A Microsoft-branded version of InRelease will b ...

  9. 使用OpenOffice.org将各类文档转为PDF

    http://blog.zhaojie.me/2010/05/convert-document-to-pdf-via-openoffice.html ————————————————————————— ...

  10. php curl_multi系列函数实现多线程抓取网页

    最近几天在做一个多搜索引擎关键字排名查询工具,用于及时方便的了解关键词在各大搜索引擎的排名. 在抓取360搜索的时候,发现360搜索每页只支持显示10个搜索结果,如果想获取100个搜索结果数据,就得搜 ...