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

时间:2022-01-03 00:56:06

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

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

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

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

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

 #include<iostream>
#include<new> //定位new运算符
using namespace std; const int BUF=;
const int N=;
char buff[BUF]; void main() {
double *p1,*p2;
int i;
cout<<"Calling"<<endl;
p1=new double[N];//常规new:p1是double指针
p2=new (buff) double[N];//定位new运算符:将数组p2放在了数组buff中
for (i = ; i < N; i++)
p2[i]=p1[i]=+20.0*i;
cout<<"Memory addresses:"<<endl<<" heap: "<<p1<<" static: "<<(void *)buff<<endl;//buffer是char指针,所以要使用(void *)对buffer进行强转,否则将显示字符串
cout<<"Memory contents:"<<endl;
for (i = ; i < N; i++)
{
cout<<p1[i]<<" at "<<&p1[i]<<";";
cout<<p2[i]<<" at "<<&p2[i]<<endl;
} cout<<"\nCalling new"<<endl;
double *p3,*p4;
p3=new double[N];
p4=new (buff) double[N];
for (i = ; i < N; i++)
p4[i]=p3[i]=+40.0*i;
cout<<"Memory contents:"<<endl;
for (i = ; i < N; i++)
{
cout<<p3[i]<<" at "<<&p3[i]<<";";
cout<<p4[i]<<" at "<<&p4[i]<<endl;
} cout<<"\nCalling new third"<<endl;
delete [] p1;
p1=new double [N];
p2=new (buff+N*sizeof(double)) double[N];
for (i = ; i < N; i++)
p2[i]=p1[i]=+60.0*i;
cout<<"Memory contents:"<<endl;
for (i = ; i < N; i++)
{
cout<<p1[i]<<" at "<<&p1[i]<<";";
cout<<p2[i]<<" at "<<&p2[i]<<endl;
}
//buff指定的内存是静态内存,所以不能delete
delete [] p1;
delete [] p3; system("pause");
}

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

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

namesp.h  头文件

#include<string>
namespace pers{ //包含Person结构的定义和两个函数原型
struct Person{
std::string fname;
std::string lname;
};
void getPerson(Person &);//引用
void showPerson(const Person &);
} namespace debts{ //定义Debt结构,用于存储人名和金额,使用using编译指令,让pers中的名称在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<string>
#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{
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=;
for (int i = ; i < n; i++)
total+=ar[i].amount;
return total;
}
}

main.cpp  主函数

#include<iostream>
#include "namesp.h"
using std::cout;
using std::endl; void other(){
using namespace debts;
Person dg={"Doodles","Glister"};
showPerson(dg);
cout<<endl;//因为showPerson没有换行
Debt zippy[];
int i;
for (i = ; i < ; i++)
getDebt(zippy[i]);
for (i = ; i < ; i++)
showDebt(zippy[i]);
cout<<"Total debt: $"<<sumDebts(zippy,)<<endl;
} void another(){
using pers::Person;
Person collector={"Milo","Rightshift"};
pers::showPerson(collector);
cout<<endl;
} void main(){
using debts::Debt;
using debts::showDebt;
Debt golf={{"Benny","Goatsniff"},120.0};
showDebt(golf);
other();
another();
system("pause");
}

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

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

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

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

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

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

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

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

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

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

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

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

  6. &lbrack;C&plus;&plus; Primer Plus&rsqb; 第4章、复合类型(一)程序清单——指针new和delete

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

  7. &lpar;8&rpar;C&plus;&plus; 内存模型与名称空间

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

  8. &lbrack;C&plus;&plus; Primer Plus&rsqb; 第9章、内存模型和名称空间(二)课后习题

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

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

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

随机推荐

  1. Rocksdb引擎记录格式

    Rocksdb是一个kv引擎,由facebook团队基于levelDB改进而来,Rocksdb采用LSM-tree存储数据,良好的读写特性以及压缩特性使得其非常受欢迎.此外,Rocksdb引擎作为插件 ...

  2. 采购信息记录修改&lbrack;BDC&rsqb;

    *&---------------------------------------------------------------------* *& *&---------- ...

  3. 1、rhel 6&period;5 系统准备

    1.启动.关闭.重置服务    (rhel7 为 systemctl) [root@rhel-6 ~]# service atd status #关闭atd服务 atd 已停 [root@rhel-6 ...

  4. Silverlight用户自定义控件件中增加属性和方法

    下面的例子在用户控件MyCpmzSelect中增加了一个myCaption属性 public static readonly DependencyProperty myCaptionProperty ...

  5. Web Components是不是Web的未来

    今天 ,Web 组件已经从本质上改变了HTML.初次接触时,它看起来像一个全新的技术.Web组件最初的目的是使开发人员拥有扩展浏览器标签的能力,可以*的进行定制组件.面对新的技术,你可能会觉得无从下 ...

  6. 安装ngix

    第一步:解压源码包 第二步:./configure -->这个时候会提示缺少PCRE 这个时候要安装yum -y install pcre-devel 第三步:./configure --&gt ...

  7. Haskell 笔记(三)类型系统

    类型 (Type) Haskell的类型系统式静态类型系统,在编译的时候就知道数据类型,所以不同类型的值运算在编译的时候就会报错,比如用布尔值和整数运算,在C语言中这种运算就不会报错. Haskell ...

  8. FFMpeg编译之路

    为了编译这个东西,快折腾了一个星期了.期间经历了很多痛苦的过程,今天我把整个过程,以及在这个过程的感悟写下来,以备日后查看,也希望能帮到一些像我一样的兄弟姐妹. 在这一个星期里前前后后加起来总共使用了 ...

  9. linux挂载系统ios文件与gcc安装

    挂载方法: 1.将iso文件拷贝到某一目录下,(/test) 2.建立挂载点文件夹:mkdir  /mnt/iso1 3.进入 mount –o loop  /test/**.iso  /mnt/is ...

  10. python通过手机抓取微信公众号

    使用 Fiddler 抓包分析公众号 打开微信随便选择一个公众号,查看公众号的所有历史文章列表 在 Fiddler 上已经能看到有请求进来了,说明公众号的文章走的都是HTTPS协议,这些请求就是微信客 ...