乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C++習題與解析(引用-01)

       lvgs 2006-08-09
      02.分析以下程序的執(zhí)行結果
      #include<iostream.h>
      class Sample
      {
      int x;
      public:
      Sample(){};
      Sample(int a){x=a;}
      Sample(Sample &a){x=a.x+1;}
      void disp(){cout<<"x="<<x<<endl;}
      };
      void main()
      {
      Sample s1(2),s2(s1);
      s2.disp();
      }
      解:
      本題說明類拷貝構造函數(shù)的使用方法。Sample類的Sample(Sample &a)構造函數(shù)是一個拷貝構造函數(shù),將a對象的x值賦給當前對象的x后加1。
      所以輸出為:x=3。

      -----------------------------------------------------

      03.編寫程序,調用傳遞引用的參數(shù),實現(xiàn)兩個字符串變量的交換。
      例如開始:
      char *ap="hello";
      char *bp="how are you?";
      交換的結果使得ap和bp指向的內(nèi)容分別為:
      ap: "how are you?"
      bp: "hello"
      解:
      本題使用引用調用(實現(xiàn)由于字符串指針本身就是地址,這里可不使用引用參數(shù),效果是一樣的)。
      程序如下:
      #include<iostream.h>
      #include<string.h>
      void swap(char *&x,char *&y) // 引用作為參數(shù)
      {
      char *temp;
      temp=x;x=y;y=temp;
      }
      void main()
      {
      char *ap="hello";
      char *bp="how are you?";
      cout<<"ap:"<<ap<<endl;
      cout<<"bp:"<<bp<<endl;
      swap(ap,bp);
      cout<<"swap ap,bp"<<endl;
      cout<<"ap:"<<ap<<endl;
      cout<<"bp:"<<bp<<endl;
      }
      本程序的執(zhí)行結果如下:
      ap:hello
      bp:hoe are you?
      swap ap,bp
      ap:how are you?
      bp:hello

      -----------------------------------------------------

      04.設計一個集合類Set,包括將集合置空、添加元素、判斷元素是否在集合中、輸出集合,以及將集合中元素逆置,另外還有一個拷貝構造函數(shù),并使用一些數(shù)據(jù)進行測試。
      解:
      Set類包括私有數(shù)據(jù)成員elems(存放集合元素)、pc(當前元素指針),一個默認構造函數(shù)和拷貝構造函數(shù)Set(Set &s),另有成員函數(shù)empty()(將集合置空)、isempty()(判斷集合是否為空)、ismemberof()(判斷元素是否在集合中)、add()(添加元素)、print()(輸出集合)、reverse(將集合中元素置逆)。
      本題程序如下:
      #include<iostream.h>
      #define Max 100
      class Set
      {
      public:
      Set(){pc=0;}
      Set(Set &s); // 對象引用作為參數(shù)
      void empty(){pc=0;}
      int isempty(){return pc==0;}
      int ismemberof(int n);
      int add(int n);
      void print();
      void reverse();
      private:
      int elems[Max];
      int pc;
      };
      int Set::ismemberof(int n)
      {
      for(int i=0;i<pc;i++)
      if(elems[i]==n)
      return 1;
      return 0;
      }
      int Set::add(int n)
      {
      if(ismemberof(n))
      return 1;
      else if(pc>Max)
      return 0;
      else
      {
      elems[pc++]=n;
      return 1;
      }
      }
      Set::Set(Set &p)
      {
      pc=p.pc;
      for(int i=0;i<pc;i++)
      elems[i]=p.elems[i];
      }
      void Set::print()
      {
      cout<<"{";
      for(int i=0;i<pc-1;i++)
      cout<<elems[i]<<",";
      if(pc>0)
      cout<<elems[pc-1];
      cout<<"}"<<endl;
      }
      void Set::reverse()
      {
      int n=pc/2;
      for(int i=0;i<n;i++)
      {
      int temp;
      temp=elems[i];
      elems[i]=elems[pc-i-1];
      elems[pc-i-1]=temp;
      }
      }
      void main()
      {
      Set A;
      cout<<"A是否為空:"; cout<<A.isempty()<<endl;
      cout<<"A:"; A.print();
      Set B;
      for(int i=1;i<=8;i++)
      B.add(i);
      cout<<"B:"; B.print();
      cout<<"5是否在B中:"; cout<<B.ismemberof(5)<<endl;
      B.empty();
      for(int j=11;j<20;j++)
      B.add(j);
      Set C(B);
      cout<<"C:"; C.print();
      C.reverse();
      cout<<"C逆置"<<endl;
      cout<<"C:"; C.print();
      }
      本程序執(zhí)行結果如下:
      A是否為空:1
      A:{}
      B:{1,2,3,4,5,6,7,8}
      5是否在B中:1
      C:{11,12,13,14,15,16,17,18,19}
      C逆置
      C:{19,18,17,16,15,14,13,12,11}

        本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約