The size of two dimension pointer

   1:  //:c08:Quoter.cpp
   2:  //random quote selection
   3:  /*
   4:  *C++编程思想第八章,关于const成员函数的例子
   5:  *这个例子说明,const对象只能调用cosnt成员函数,例子利用随机数生成器构造了
   6:  *一个选举器,每次都从若干句话中选出与上次不同的一句话,打印输出。
   7:  *时间:2011-7-17 10:34
   8:  *作者:张超
   9:  *Email:uestczhangchao@gmail.com
  10:  */
  11:   
  12:  #include "X:\编程练习\C-C++\global.h"
  13:   
  14:  #if Quoter_cpp==stdon
  15:  #include <iostream>
  16:  #include <cstdlib>
  17:  #include <ctime>
  18:  using namespace std;
  19:   
  20:  class Quoter
  21:  {
  22:      int lastquote;
  23:  public:
  24:      Quoter();
  25:      int lastQuote() const;
  26:      const char* quote();
  27:  };
  28:   
  29:  Quoter::Quoter()
  30:  {
  31:      lastquote=-1;
  32:      srand(time(0));
  33:  }
  34:   
  35:  int Quoter::lastQuote() const
  36:  {
  37:      return lastquote;
  38:  }
  39:   
  40:  const char* Quoter::quote()
  41:  {
  42:      static const char* quotes[]=
  43:      {
  44:          "Are we having fucn yet?",
  45:          "Doctors always know best!",
  46:          "Is it ... Atomic",
  47:          "Fear is obscene",
  48:          "There is no significant evidence",
  49:          "to support the idea",
  50:          "that life is serious",
  51:          "Things that make us happy, make us wise",
  52:          "This line is add by JustinZhang",
  53:      };
  54:      //对二维指针进行sizeof运算的结果为:(二维指针包含的一位指针的个数)×(一维指针的大小)
  55:      const int qsize = sizeof quotes/sizeof *quotes;
  56:      cout << "qsize=" << qsize <<" sizeof quotes="<<sizeof quotes <<" sizeof *quotes="<<sizeof *quotes << endl;
  57:      int qnum = rand() % qsize;
  58:      //从quotes中选择一句与上一句不同的话出来。
  59:      while (lastquote >=0 && qnum==lastquote)
  60:      {
  61:          qnum = rand()%qsize;
  62:      }
  63:      return quotes[lastquote = qnum];
  64:  }
  65:  int main()
  66:  {
  67:      Quoter q;
  68:      const Quoter cq;
  69:      cq.lastQuote();
  70:      int a = 0;
  71:      int c = a&&10/a;
  72:      cout << c << endl;
  73:      //cq.quote();
  74:      for(int i=0; i<20; i++)
  75:      {
  76:          cout << q.quote() << endl;
  77:   
  78:      }
  79:      system("pause");
  80:  }
  81:   
  82:  #endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值