【C++】名称空间示例:一个小型程序的完全实现,深入理解程序结构!!!

举报
王博Kings 发表于 2020/12/29 23:39:12 2020/12/29
【摘要】 目录 这个程序包含三部分: namesp.h namesp.cpp namessp.cpp 运行结果:  这个程序包含三部分: namesp.h namesp.cpp namessp.cpp namesp.h //namesp.h #include<string>// 创造名称空间namespace pers{ struct Person { std...

目录

这个程序包含三部分:

namesp.h

namesp.cpp

namessp.cpp

运行结果:


 这个程序包含三部分:

namesp.h

namesp.cpp

namessp.cpp

namesp.h


  
  1. //namesp.h
  2. #include<string>
  3. // 创造名称空间
  4. namespace pers
  5. {
  6. struct Person
  7. {
  8. std::string fname;
  9. std::string lname;
  10. };
  11. void getPerson(Person &);
  12. void showPerson(const Person &);
  13. }
  14. namespace debts
  15. {
  16. using namespace pers;
  17. struct Debt
  18. {
  19. Person name;
  20. double amount;
  21. };
  22. void getDebt(Debt &);
  23. void showDebt(const Debt &);
  24. double sumDebts(const Debt ar[], int n);
  25. }

namesp.cpp


  
  1. // namesp.cpp -- namespaces
  2. #include<iostream>
  3. #include"namesp.h"
  4. namespace pers
  5. {
  6. using std::cout;
  7. using std::cin;
  8. void getPerson(Person & rp)
  9. {
  10. cout << "Enter first name: ";
  11. cin >> rp.fname;
  12. cout << "Enter last name: ";
  13. cin >> rp.lname;
  14. }
  15. void showPerson(const Person & rp)
  16. {
  17. std::cout << rp.lname << ", " << rp.fname;
  18. }
  19. }
  20. //头文件中已经using namespace pers;,所以showPerson可以用了
  21. namespace debts
  22. {
  23. void getDebt(Debt & rd)
  24. {
  25. getPerson(rd.name);
  26. std::cout << "Enter debt: ";
  27. std::cin >> rd.amount;
  28. }
  29. void showDebt(const Debt & rd)
  30. {
  31. showPerson(rd.name);
  32. std::cout << ": $ " << rd.amount << std::endl;
  33. }
  34. double sumDebts(const Debt ar[], int n)
  35. {
  36. double total = 0;
  37. for (int i = 0; i < n; i++)
  38. {
  39. total += ar[i].amount;
  40. }
  41. return total;
  42. }
  43. }

namessp.cpp


  
  1. // namessp.cpp -- 使用名称空间
  2. #include<iostream>
  3. #include"namesp.h"
  4. void other(void);
  5. void another(void);
  6. int main(void)
  7. {
  8. using debts::Debt;
  9. using debts::showDebt;
  10. Debt golf = { { "Benny", "Goastsinff" }, 120.0 };//忘了可以查看Debt定义
  11. showDebt(golf);
  12. other();
  13. another();
  14. return 0;
  15. }
  16. void other(void)
  17. {
  18. using std::cout;
  19. using std::endl;
  20. using namespace debts;
  21. Person dg = { "Doodels", "Glister" };
  22. showPerson(dg);
  23. cout << endl;
  24. Debt zippy[3];
  25. int i;
  26. for ( i = 0; i < 3; i++)
  27. {
  28. getDebt(zippy[i]);
  29. }
  30. for ( i = 0; i < 3; i++)
  31. {
  32. showDebt(zippy[i]);
  33. }
  34. cout << "Total debt : $" << sumDebts(zippy, 3) << endl;
  35. return;
  36. }
  37. void another(void)
  38. {
  39. using pers::Person;
  40. Person collector = { "Milo", "Rufsift" };
  41. pers::showPerson(collector);
  42. std::cout << std::endl;
  43. }

运行结果:

9.3.3 

文章来源: kings.blog.csdn.net,作者:人工智能博士,版权归原作者所有,如需转载,请联系作者。

原文链接:kings.blog.csdn.net/article/details/97239307

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。