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()和其他这些函数的函数放在第三个文件中. 

  • 相关阅读:
    OAuth2 协议原理简析及Azure AD OAuth2示例
    MySQL Innodb MVCC(多版本并发控制)
    乐观锁和悲观锁
    数据库索引
    SQL盲注
    JAVA array to list and list to array
    缓存穿透,缓存雪崩和缓存击穿
    四、Spring Cloud 之旅 -- Ribbon 负载均衡
    JavaScript中 require、import 有什么区别?
    vue 2.x Vue 3.x 日常采坑之 设置alias别名、background引入图片、全局引入scss文件 的问题
  • 原文地址:https://www.cnblogs.com/kobe8/p/3984953.html
Copyright © 2011-2022 走看看