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

  • 相关阅读:
    物料序号不可修改
    物料序号不可输入
    禁用物料不允许BOM
    MRP工作台任务下达之x组织屏蔽全部发放功能
    MRP工作台任务下达之计划组为必输
    按计划员自动带出对应任务类型
    java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource解决方法
    web.xml 中<taglib>报错(转载)
    web.xml元素介绍
    Struts+Tomcat搭建
  • 原文地址:https://www.cnblogs.com/hnrainll/p/2474215.html
Copyright © 2020-2023  润新知