程序清单9.9(静态存储连续性、无链接性)

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. const int Size=;
  5. void strcount(const char *str){//const表示str指针不能修改指向的内容(不过可以指向另外一块内容)
  6. static int total=;//static静态变量,首次初始化后,其值一直存在(即第二次调用strcount函数时,total的值不会再次初始化)
  7. int count=;
  8. cout<<"\""<<str<<"\" contains ";
  9. while (*str++)//先判断*str是否为NULL,然后再str++
  10. count++;
  11. total+=count;
  12. cout<<count<<" characters\n";
  13. cout<<total<<" characters total!\n";
  14. }
  15.  
  16. void main() {
  17. char in[Size];
  18. char next;
  19. cout<<"Enter a line:"<<endl;
  20. cin.get(in,Size);//最多接收Size-1个字符+1个'\0'
  21. while (cin) // ==while(!cin.fail()),即读入流成功
  22. {
  23. cin.get(next);
  24. while(next!='\n') //若next不是换行符
  25. cin.get(next);
  26. strcount(in);
  27. cout<<"Enter next line (empty line to quit):\n";
  28. cin.get(in,Size);
  29. }
  30. cout<<"Bye!"<<endl;
  31. system("pause");
  32. }

程序清单9.10(常规new和定位new运算符)

  1. #include<iostream>
  2. #include<new> //定位new运算符
  3. using namespace std;
  4.  
  5. const int BUF=;
  6. const int N=;
  7. char buff[BUF];
  8.  
  9. void main() {
  10. double *p1,*p2;
  11. int i;
  12. cout<<"Calling"<<endl;
  13. p1=new double[N];//常规new:p1是double指针
  14. p2=new (buff) double[N];//定位new运算符:将数组p2放在了数组buff中
  15. for (i = ; i < N; i++)
  16. p2[i]=p1[i]=+20.0*i;
  17. cout<<"Memory addresses:"<<endl<<" heap: "<<p1<<" static: "<<(void *)buff<<endl;//buffer是char指针,所以要使用(void *)对buffer进行强转,否则将显示字符串
  18. cout<<"Memory contents:"<<endl;
  19. for (i = ; i < N; i++)
  20. {
  21. cout<<p1[i]<<" at "<<&p1[i]<<";";
  22. cout<<p2[i]<<" at "<<&p2[i]<<endl;
  23. }
  24.  
  25. cout<<"\nCalling new"<<endl;
  26. double *p3,*p4;
  27. p3=new double[N];
  28. p4=new (buff) double[N];
  29. for (i = ; i < N; i++)
  30. p4[i]=p3[i]=+40.0*i;
  31. cout<<"Memory contents:"<<endl;
  32. for (i = ; i < N; i++)
  33. {
  34. cout<<p3[i]<<" at "<<&p3[i]<<";";
  35. cout<<p4[i]<<" at "<<&p4[i]<<endl;
  36. }
  37.  
  38. cout<<"\nCalling new third"<<endl;
  39. delete [] p1;
  40. p1=new double [N];
  41. p2=new (buff+N*sizeof(double)) double[N];
  42. for (i = ; i < N; i++)
  43. p2[i]=p1[i]=+60.0*i;
  44. cout<<"Memory contents:"<<endl;
  45. for (i = ; i < N; i++)
  46. {
  47. cout<<p1[i]<<" at "<<&p1[i]<<";";
  48. cout<<p2[i]<<" at "<<&p2[i]<<endl;
  49. }
  50. //buff指定的内存是静态内存,所以不能delete
  51. delete [] p1;
  52. delete [] p3;
  53.  
  54. system("pause");
  55. }

程序清单9.11-13(名称空间示例)

namesp.h  头文件

  1. #include<string>
  2. namespace pers{ //包含Person结构的定义和两个函数原型
  3. struct Person{
  4. std::string fname;
  5. std::string lname;
  6. };
  7. void getPerson(Person &);//引用
  8. void showPerson(const Person &);
  9. }
  10.  
  11. namespace debts{ //定义Debt结构,用于存储人名和金额,使用using编译指令,让pers中的名称在debts空间也能使用
  12. using namespace pers;
  13. struct Debt{
  14. Person name;
  15. double amount;
  16. };
  17. void getDebt(Debt &);
  18. void showDebt(const Debt &);
  19. double sumDebts(const Debt ar[],int n);
  20. }

namesp.cpp  函数定义

  1. #include<iostream>
  2. #include<string>
  3. #include "namesp.h"//自己编写的头文件只能使用引号"",系统自带的头文件使用<>,不过""也能用
  4.  
  5. namespace pers{
  6. using std::cout;
  7. using std::cin;
  8. void getPerson(Person &rp){
  9. cout<<"Enter first name:";
  10. cin>>rp.fname;
  11. cout<<"Enter last name:";
  12. cin>>rp.lname;
  13. }
  14. void showPerson(const Person &rp){
  15. cout<<rp.lname<<","<<rp.fname;
  16. }
  17. }
  18.  
  19. namespace debts{
  20. void getDebt(Debt &rd){
  21. getPerson(rd.name);
  22. std::cout<<"Enter debt:";
  23. std::cin>>rd.amount;
  24. }
  25. void showDebt(const Debt &rd){
  26. showPerson(rd.name);
  27. std::cout<<": $"<<rd.amount<<std::endl;
  28. }
  29. double sumDebts(const Debt ar[],int n){
  30. double total=;
  31. for (int i = ; i < n; i++)
  32. total+=ar[i].amount;
  33. return total;
  34. }
  35. }

main.cpp  主函数

  1. #include<iostream>
  2. #include "namesp.h"
  3. using std::cout;
  4. using std::endl;
  5.  
  6. void other(){
  7. using namespace debts;
  8. Person dg={"Doodles","Glister"};
  9. showPerson(dg);
  10. cout<<endl;//因为showPerson没有换行
  11. Debt zippy[];
  12. int i;
  13. for (i = ; i < ; i++)
  14. getDebt(zippy[i]);
  15. for (i = ; i < ; i++)
  16. showDebt(zippy[i]);
  17. cout<<"Total debt: $"<<sumDebts(zippy,)<<endl;
  18. }
  19.  
  20. void another(){
  21. using pers::Person;
  22. Person collector={"Milo","Rightshift"};
  23. pers::showPerson(collector);
  24. cout<<endl;
  25. }
  26.  
  27. void main(){
  28. using debts::Debt;
  29. using debts::showDebt;
  30. Debt golf={{"Benny","Goatsniff"},120.0};
  31. showDebt(golf);
  32. other();
  33. another();
  34. system("pause");
  35. }

[C++ Primer Plus] 第9章、内存模型和名称空间(一)程序清单的更多相关文章

  1. C++ primer plus读书笔记——第9章 内存模型和名称空间

    第9章 内存模型和名称空间 1. 头文件常包含的内容: 函数原型. 使用#define或const定义的符号常量. 结构声明. 类声明. 模板声明. 内联函数. 2. 如果文件名被包含在尖括号中,则C ...

  2. 《C++ Primer Plus》第9章 内存模型和名称空间 学习笔记

    C++鼓励程序员在开发程序时使用多个文件.一种有效的组织策略是,使用头文件来定义用户类型,为操纵用户类型的函数提供函数原型,并将函数定义放在一个独立的源代码文件中.头文件和源代码文件一起定义和实现了用 ...

  3. 《C++ Primer Plus 6th》读书笔记 - 第九章 内存模型和名称空间

    1. 单独编译 1.1 头文件中常包含的内容: 函数原型 使用#define或const定义的符号常量 结构声明 类声明 模板声明 内联声明 1.2 只需将源代码文件加入到项目中,而不用加入头文件.这 ...

  4. 《C++ Primer Plus》读书笔记之七—内存模型和名称空间

    第九章 内存模型和名称空间 1.不要将函数定义或者变量声明放到头文件中. 2.头文件常包含的内容:函数原型.使用#define或者const定义的常量.结构声明.类声明.模板声明.内联函数. 3.避免 ...

  5. [C++ Primer Plus] 第8章、函数探幽(一)程序清单——内联、引用、格式化输入输出、模板、decltype

    程序清单8.1(inline内联函数) #include<iostream> using namespace std; inline double square(double x) {// ...

  6. [C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete

    程序清单4.1 #include<iostream> using namespace std; void main(){ ]; yams[]=; yams[]=; yams[]=; ]={ ...

  7. (8)C++ 内存模型与名称空间

    一.单独编译 头文件 不要将函数定义或者变量声明放到头文件中,引入多个文件时可能会造成同一个函数定义多次 引入头文件 #include "文件名" File1.h #ifndef ...

  8. [C++ Primer Plus] 第9章、内存模型和名称空间(二)课后习题

    一.复习题 2.using声明和using编译指令的区别 using声明: using std::cin; using std::cout; using std::endl; using编译指令:us ...

  9. C++ Primer Plus读书笔记(九)内存模型和名称空间

    1.作用域和链接 int num3; static int num4; int main() { } void func1() { static int num1; int num2; } 上边的代码 ...

随机推荐

  1. 阿里云服务器ssh经常一段时间就断掉解决办法

    #vim /etc/ssh/sshd_config 找到下面两行 #ClientAliveInterval 0#ClientAliveCountMax 3 去掉注释,改成 ClientAliveInt ...

  2. ES6语法 Promise Iterator

    类和对象 基本定义: class Parent{ constructor(name='lmx'){ //name= 默认值 this.name=name } } let v_parent = new ...

  3. .Net Core知识点

    1:const,readonly,和get访问器,三者都可在自己的生命域里赋值,但是编译器也是可以在构造函数里进行初始化赋值的 2:Debugger.IsAttached 属性 http://msdn ...

  4. Prometheus部署监控容器

    Prometheus架构描述 Prometheus 是一个非常优秀的监控工具.准确的说,应该是监控方案.Prometheus 提供了监控数据搜集.存储.处理.可视化和告警一套完整的解决方案 Prome ...

  5. 谷歌将一些弱小的库从安卓代码移除Google Removes Vulnerable Library from Android

    Google this week released the November 2018 set of security patches for its Android platform, which ...

  6. 00JAVA语法基础_动手动脑

    1.仔细阅读示例: EnumTest.java,运行它,分析运行结果? 枚举类型的使用是借助ENUM这样一个类,这个类是JAVA枚举类型的公共基本类.枚举目的就是要让某个变量的取值只能为若干固定值中的 ...

  7. Jar包的手动导入

      (1)打开项目的file  找到project structure   (2)进行以下操作

  8. java 访问数据库

    Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);//依据不同数据库,加载不同驱动 String url = “jdbc:sq ...

  9. liunx驱动----系统滴答时钟的使用

    2019-3-12系统滴答定时器中断使用 定义一个timer ​​ 其实就是使用系统的滴答定时器产生一个中断. 初始化timer init_timer函数 实现如下 void fastcall ini ...

  10. 使用MSBuild Tools调用csproj项目文件发布网站时$(SolutionDir)宏参数值丢失为空的解决方案

    使用Visual Studio打开解决方案,对<网站项目>右键点击<发布>,一切都是正常的,所有宏都可用,宏参数值也是正确的. 而通过批处理脚本命令调用MSBuild.exe对 ...