//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. iOS开发transform的使用

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

  2. TensorFlow学习笔记(8)--网络模型的保存和读取【转】

    转自:http://blog.csdn.net/lwplwf/article/details/62419087 之前的笔记里实现了softmax回归分类.简单的含有一个隐层的神经网络.卷积神经网络等等 ...

  3. openfire聊天记录插件

    package com.sqj.openfire.chat.logs; import java.io.File; import java.util.Date; import java.util.Lis ...

  4. Ant编译android程序

    http://blog.csdn.net/xyz_lmn/article/details/7268582 这一篇主要做了创建android项目.update已存在项目.ant编译项目. 一,准备ant ...

  5. LeetCode: Balanced Binary Tree 解题报告

    Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...

  6. Python3求m以内的素数、求m个数中最小的n个数

    [本文出自天外归云的博客园] 题1:求m以内的素数(m>2) def find_all_primes_in(m): def prime(num): for i in range(2, num): ...

  7. executor.Executor: Managed memory leak detected; size = 37247642 bytes, TID = 5

    https://stackoverflow.com/questions/34359211/debugging-managed-memory-leak-detected-in-spark-1-6-0 h ...

  8. Django: AttributeError: 'str' object has no attribute 'resolve'

    再次重温Django的时候,遇到了这个错误.看了页面上,没啥有用的信息.遂谷歌一下,原来是一个很低级的错误:It's because you forgot to type the word " ...

  9. Android(我还是个菜鸟)——UI-开源框架ImageLoader的完美例子

    开源框架ImageLoader 可在文件里面找——Desktop.zip(原文件为jar格式) 使用这个框架的好处: 让图片能在异步加载更加流畅,可以显示大量图片,在拖动ListView的时候不会出现 ...

  10. C# .net微信开发,开发认证,关注触发消息,自动应答,事件响应,自定义菜单

    成为开发者 string[] ArrTmp = { "token", Request["timestamp"], Request["nonce&quo ...