Data Abstraction(Chapter 4 of Thinking in C++)

ExpandedBlockStart.gif CppLib.h
 1  struct  Stash
 2  {
 3       int  size;
 4       int  quantity;
 5       int  next;
 6      unsigned  char *  storage;
 7       void  initialize( int  size);
 8       void  cleanup();
 9       int  add( const   void *  element);
10       void *  fetch( int  index);
11       int  count();
12       void  inflate( int  increase);
13  };

 

ExpandedBlockStart.gif CppLib.cpp
 1  #include  " CppLib.h "
 2  #include  < iostream >
 3  #include  < cassert >
 4  using   namespace  std;
 5 
 6  const   int  increment  =   100 ;
 7 
 8  void  Stash::initialize( int  size)
 9  {
10       this -> size  =  size;
11      quantity  =   0 ;
12      storage  =   0 ;
13      next  =   0 ;
14  }
15 
16  int  Stash::add( const   void *  element)
17  {
18       if (next  >=  quantity)
19      {
20          inflate(increment);
21      }
22 
23       int  startBytes  =  next  *  size;
24      unsigned  char *  e  =  (unsigned  char * )element;
25       for ( int  i = 0 ; i < size; i ++ )
26      {
27          storage[startBytes + i]  =  e[i];
28      }
29      next ++ ;
30       return  (next - 1 );
31  }
32 
33  void *  Stash::fetch( int  index)
34  {
35      assert( 0   <=  index);
36       if (index  >=  next)
37      {
38           return   0 ;
39      }
40 
41       return   & (storage[index  *  size]);
42  }
43 
44  int  Stash::count()
45  {
46       return  next;
47  }
48 
49  void  Stash::inflate( int  increase)
50  {
51      assert(increase  >   0 );
52       int  newQuantity  =  quantity  +  increase;
53       int  newBytes  =  newQuantity  *  size;
54       int  oldBytes  =  quantity  *  size;
55      unsigned  char *  b  =   new  unsigned  char [newBytes];
56       for ( int  i = 0 ; i < oldBytes; i ++ )
57      {
58          b[i]  =  storage[i];
59      }
60      delete []storage;
61      storage  =  b;
62      quantity  =  newQuantity;
63  }
64 
65  void  Stash::cleanup()
66  {
67       if (storage  !=   0 )
68      {
69          cout  <<   " freeeing storage "   <<  endl;
70          delete []storage;
71      }
72  }

 

 

ExpandedBlockStart.gif CppLibTest.cpp
 1  #include  " CppLib.h "
 2  #include  < fstream >
 3  #include  < iostream >
 4  #include  < string >
 5  using   namespace  std;
 6 
 7  int  main()
 8  {
 9      Stash intStash;
10      intStash.initialize( sizeof ( int ));
11       for ( int  i = 0 ; i < 100 ; i ++ )
12      {
13          intStash.add( & i);
14      }
15 
16       for ( int  j = 0 ; j < intStash.count(); j ++ )
17      {
18          cout  <<   " intStash.fetch( "   <<  j  <<   " ) =  "
19               <<   * ( int * )intStash.fetch(j)  <<  endl;
20      }
21 
22      Stash stringStash;
23       const   int  bufsize  =   80 ;
24      stringStash.initialize( sizeof ( char *  bufsize);
25      ifstream  in ( " CppLibTest.cpp " );
26       string  line;
27       while (getline( in , line))
28      {
29          stringStash.add(line.c_str());
30      }
31 
32       int  k = 0 ;
33       char *  cp;
34       while ((cp  =  ( char * )stringStash.fetch(k ++ ))  !=   0 )
35      {
36          cout  <<   " stringStash.fetch( "   <<  k  <<   " ) =  "
37               <<  cp  <<  endl;
38      }
39 
40      intStash.cleanup();
41      stringStash.cleanup();
42      cin. get ();
43  }

 

 

转载于:https://www.cnblogs.com/zhtf2014/archive/2010/11/10/1873278.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值