程序清单4.1
1 #include<iostream> 2 using namespace std; 3 4 void main(){ 5 int yams[3]; 6 yams[0]=7; 7 yams[1]=8; 8 yams[2]=6; 9 int yams_cost[3]={20,30,5}; 10 11 cout<<"Total yams="<<yams[0]+yams[1]+yams[2]<<endl; 12 cout<<"The package with "<<yams[1]<<" yams costs "<<yams_cost[1]<<" cents per yam"<<endl; 13 cout<<"Total costs="<<yams[0]*yams_cost[0]+yams[1]*yams_cost[1]+yams[2]*yams_cost[2]<<" cents"<<endl; 14 15 cout<<"Size of yams array="<<sizeof(yams)<<" bytes"<<endl; 16 cout<<"Size of yams[0]="<<sizeof(yams[0])<<" bytes"<<endl; 17 18 system("pause"); 19 }

程序清单4.2
1 #include<iostream> 2 #include<cstring>//strlen(),c语言版为string.h 3 using namespace std; 4 5 void main(){ 6 const int size=15; 7 char name1[size];//char占一个字节 8 char name2[size]="C++owboy"; 9 10 cout<<"I am "<<name2<<".What's your name?"<<endl; 11 cin>>name1; 12 cout<<"Well,"<<name1<<",your name has "<<strlen(name1)<<" letters and is stored in an array of "<<sizeof(name1)<<" bytes"<<endl; 13 cout<<"Your initial is "<<name1[0]<<endl; 14 name2[3]='