2017.06.27 有关c++命名空间的学习
来源:互联网 发布:分享到qq好友js代码 编辑:程序博客网 时间:2024/06/04 18:30
1.首先建立namesp.h文件,代码如下
#include "stdafx.h"#include <string>namespace pers{struct Person{std::string fname;std::string lname;};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);};2.建立namesp.cpp文件,代码如下
#include "stdafx.h"#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.fname<<", "<<rp.lname;};};namespace debts{void getDebt(Debt & rd){getPerson(rd.name);std::cout<<"Enter debt: ";std::cin>>rd.amount;};void showDebt(const Debt & rd){showPerson(rd.name);std::cout<<": $"<<rd.amount<<std::endl;};double sumDebts(const Debt ar[],int n){double total=0;for(int i=0;i<n;i++){total+=ar[i].amount;}return total;};};
3.main主函数
// ggg.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>#include "namesp.h"void other(void);void another(void);int _tmain(int argc, _TCHAR* argv[]){using debts::Debt;using debts::showDebt;Debt golf={{"Benny","Goatsniff"},120.0};showDebt(golf);other();another();return 0;}void other(){using std::cout;using std::cin;using namespace debts;Person dg={"Doodles","Glister"};showPerson(dg);cout<<std::endl;Debt zippy[3];for(int i=0;i<3;i++){getDebt(zippy[i]);}for(int i=0;i<3;i++){showDebt(zippy[i]);}cout<<"Total debt: $"<<sumDebts(zippy,3)<<std::endl;return;}void another(){using pers::Person;Person collector={"Milo","Rightshift"};pers::showPerson(collector);std::cout<<std::endl;}
阅读全文
0 0
- 2017.06.27 有关c++命名空间的学习
- 有关命名空间的知识点
- C++Primer学习-命名空间
- 有关命名空间的一些规则
- MVC有关的核心命名空间
- c++primer学习笔记-----3.1命名空间的using 声明
- javascript命名空间的学习
- NameSpace命名空间的学习
- C++学习之一.命名空间
- 一个与命名空间有关的奇怪错误!
- 有关C的学习
- C/C++:命名空间
- DateAndTime 的命名空间Microsoft.VisualBasic(C#)
- C语言,模拟命名空间的方法
- C++命名空间的使用
- PHP的命名空间的学习
- C++-namespace(命名空间)
- C++-namespace(命名空间)
- java 读取clob字段的几种方法
- 微信小程序之网络请求简单封装
- 大型项目架构演进过程及思考的点
- WPF
- Global 和 Local 函数
- 2017.06.27 有关c++命名空间的学习
- java实用方法系列 从源数组考贝到目标数组 边界检查的问题
- 重写alert方法
- 使用JNI技术实现Java和C/C++互调(C/C++调Java)
- Error querying database. Cause: java.lang.NumberFormatException: For input string: "否"
- 深入理解分布式事务
- CentOS6.6 RPM安装MySQL-5.6.36,并创建指定数据库账号
- polyfit多项式拟合函数的用法
- office 2016 安装