博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ Primer Plus 学习笔记 第九章最后的一点完结 名称空间
阅读量:4127 次
发布时间:2019-05-25

本文共 1805 字,大约阅读时间需要 6 分钟。

示例代码:

namesp.h

#include 
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);}

namesp.cpp

#include 
#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) { std::cout << rp.lname << ", " << rp.fname; }}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; }}

namessp.cpp

#include 
#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 = {"Doodles", "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;}

执行结果:

最后 总结:

转载地址:http://rjepi.baihongyu.com/

你可能感兴趣的文章
VUe+webpack构建单页router应用(一)
查看>>
Node.js-模块和包
查看>>
(python版)《剑指Offer》JZ01:二维数组中的查找
查看>>
Spring MVC中使用Thymeleaf模板引擎
查看>>
PHP 7 的五大新特性
查看>>
深入了解php底层机制
查看>>
PHP中的stdClass 【转】
查看>>
XHProf-php轻量级的性能分析工具
查看>>
OpenCV gpu模块样例注释:video_reader.cpp
查看>>
就在昨天,全球 42 亿 IPv4 地址宣告耗尽!
查看>>
如果你还不了解 RTC,那我强烈建议你看看这个!
查看>>
Mysql复制表以及复制数据库
查看>>
Linux下SVN客户端使用教程
查看>>
Linux分区方案
查看>>
如何使用 systemd 中的定时器
查看>>
git命令速查表
查看>>
linux进程监控和自动重启的简单实现
查看>>
OpenFeign学习(三):OpenFeign配置生成代理对象
查看>>
OpenFeign学习(四):OpenFeign的方法同步请求执行
查看>>
OpenFeign学习(五):OpenFeign请求结果处理及重试控制
查看>>