zoukankan      html  css  js  c++  java
  • 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()和其他这些函数的函数放在第三个文件中. 

  • 相关阅读:
    WinScan2PDF ----将图片批量选中后转换成pdf
    画质王(iCYPlayer)播放器
    如何让UEFI BIOS主板在Windows XP SP3 32位系统下识别GPT格式移动硬盘
    windows xp 文件排列全部默认以“详细信息”显示
    asus 笔记本从window 7系统安装到window xp系统的过程
    FastCopy --- 快速复制文件工具
    《极地》纪录片
    Android 机器全擦之后无法驻网了,如何备份和恢复QCN?
    Google Camera
    Android 命令行工具-apkanalyzer
  • 原文地址:https://www.cnblogs.com/kobe8/p/3984953.html
Copyright © 2011-2022 走看看