[C++ Primer Plus] 第9章、內存模型和名稱空間(一)程序清單

程序清單9.9(靜態存儲連續性、無連接性)

#include<iostream>
using namespace std;

const int Size=10;
void strcount(const char *str){//const表示str指針不能修改指向的內容(不過能夠指向另一塊內容)
    static int total=0;//static靜態變量,首次初始化後,其值一直存在(即第二次調用strcount函數時,total的值不會再次初始化)
    int count=0;
    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運算符)

 1 #include<iostream>
 2 #include<new>    //定位new運算符
 3 using namespace std;
 4 
 5 const int BUF=512;
 6 const int N=5;
 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 = 0; i < N; i++)
16         p2[i]=p1[i]=1000+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 = 0; 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 = 0; i < N; i++)
30         p4[i]=p3[i]=1000+40.0*i;
31     cout<<"Memory contents:"<<endl;
32     for (i = 0; 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 = 0; i < N; i++)
43         p2[i]=p1[i]=1000+60.0*i;
44     cout<<"Memory contents:"<<endl;
45     for (i = 0; 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  頭文件ios

#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=0;
        for (int i = 0; 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[3];
    int i;
    for (i = 0; i < 3; i++)
        getDebt(zippy[i]);
    for (i = 0; i < 3; i++)
        showDebt(zippy[i]);
    cout<<"Total debt: $"<<sumDebts(zippy,3)<<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");
}

相關文章
相關標籤/搜索