程序清单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");
}
程序清单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");
}
程序清单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");
}