<dfn id="w48us"></dfn><ul id="w48us"></ul>
  • <ul id="w48us"></ul>
  • <del id="w48us"></del>
    <ul id="w48us"></ul>
  • 計算機二級C++模擬試題及答案

    時間:2024-07-04 01:10:17 計算機等級 我要投稿

    2016計算機二級C++模擬試題及答案

      一、改錯題

    2016計算機二級C++模擬試題及答案

      使用VC6打開考生文件夾下的工程kt7_1,此工程包含一個源程序文件kt7_1.cpp,但該程序運行有問題,請改正程序中的錯誤,使程序的輸出結果如下:

      Constructor1

      Constructor1

      Constructor1

      Destructor

      Constructor2

      Destructor

      x=0

      x=5

      Destructor

      Destructor

      源程序文件kt21_1.cpp清單如下:

      #include

      using namespace std;

      class B

      {

      private:

      int x;

      public:

      B(){x=0;cout<<"Constructor1"<

      B(int i){x=i;cout<<"Constructor2"<

      ~B(){cout<<"Destructor"<

      /**********found**********/

      ~B(int i){cout<<"Destructor"<

      void print(){cout<<"x="<

      };

      void main()

      {

      B *ptr;

      ptr=new B[2];

      /**********found**********/

      ptr[0]=B(0);

      ptr[1]=B(5);

      /**********found**********/

      for(int i=0;i<2;)

      ptr[i].print();

      delete []ptr;

      }

      【參考答案】

      (1)將~B(int i){cout<<"Destructor"<

      (2)將ptr[0]=B(0);改為:ptr[0]=B();

      (3)將for (int i=0;i<2;)改為:for(int i=0;i<2;i++)

      【試題解析】

      (1)主要考查對析構函數特性的掌握,析構函數不能帶參數也不能重載,一個類中只能定義一個析構函數,因為析構函數在刪除對象的時候被默認調用,如果含有多個析構函數則可能引起多次刪除產生的意外錯誤;

      (2)主要考查對構造函數與函數重載的掌握,由輸出結果可知其應該調用不帶參數的構造函數B();

      (3)主要考查對for循環語句的使用,如果在for循環中不寫第三個表達式就意味著該循環一直會執行下去,因為i的值總是小于2。

      #include

      using namespace std;

      class B

      {

      private:

      int x;

      public:

      B(){x=0;cout<<"Constructor1"<

      B(int i){x=i;cout<<"Constructor2"<

      ~B(){cout<<"Destructor"<

      /**********found**********/

      //~B(int i){cout<<"Destructor"<

      void print(){cout<<"x="<

      };

      void main()

      {

      B *ptr;

      ptr=new B[2];

      /**********found**********/

      ptr[0]=B(); //ptr[0]=B(0);

      ptr[1]=B(5);

      /**********found**********/

      for(int i=0;i<2;i++) //for(int i=0;i<2;)

      ptr[i].print();

      delete []ptr;

      }

      二、簡單應用題

      編寫一個函數intcharnum(charfn[10]),該函數以只讀方式打開文件fn,,通過統計,返回文件中字符的個數,請使用while循環實現計數功能。

      注意:部分源程序已存在文件kt7_2.cpp中。

      請勿修改主函數main和其他函數中的任何內容,僅在函數charnum的花括號中填寫若干語句。

      文件kt7_2.cpp的內容如下:

      #include

      #include

      #include

      using namespace std;

      int charnum(char fn[10]);

      void main()

      {

      int num;

      num=charnum("abc.txt");

      cout<<"num="<

      }

      int charnum(char fn[10])

      {

      }

      【參考答案】

      int charnum(char fn[10])

      {

      fstream file;

      file.open(fn,ios::in);

      if(!file) {

      cout<<"abc.txt can't open"<

      abort();

      }

      char ch;

      int i=0;

      while(!file.eof())

      {

      file.get(ch);

      i++;

      } file.close();

      return i-1;

      }

      【試題解析】 本題主要考查對文件相關操作的熟練程度。首先定義文件流類的變量,然后使用該對象的open方法打開一個文件,接著使用while循環和getch方法每次讀入一個字符并統計字符個數,最后使用close方法關閉文件,返回i值。

      #include

      #include

      #include

      using namespace std;

      int charnum(char fn[10]);

      void main()

      {

      int num;

      num=charnum("abc.txt");

      cout<<"num="<

      }

      int charnum(char fn[10])

      {

      fstream file;

      file.open(fn,ios::in);

      if(!file) {

      cout<<"abc.txt can't open"<

      abort();

      }

      char ch;

      int i=0;

      while(!file.eof())

      {

      file.get(ch);

      i++;

      } file.close();

      return i-1;

      }

      三、綜合應用題

      使用VC6打開考生文件夾下的工程kt7_3,此工程包含一個源程序文件kt7_3.cpp,其中含有一個類Circle的定義,但該類的定義并不完整。請按要求完成下列操作,將類Circle的定義補充完整。

      (1)為類Circle增加一個構造函數,該函數有一個參數,并在構造時將該參數值賦給成員radius。將該函數實現為一個非內聯函數,并且使用參數列表的方式將類成員賦值。請在注釋“//**1**”之后添加適當的語句。

      (2)為類Circle增加一個成員函數print(),使得可以輸出有關圓的信息,比如下列程序

      Circlec;

      c.SetRadius(5);

      c.Print();

      將輸出:The circle has radius of 5!

      請在注釋“//**2**”之后添加適當的語句。

      (3)完成友元函數void CompareR(Circle *c1,Circle *c2)的定義,在屏幕中輸出c1與c2比較radius大小結果,要求使用if-else結構完成。請在注釋“//**3**”之后添加適當的語句。

      輸出結果如下:

      The circle has radus of 5!

      The circle has radius of 10!

      c1

      注意:除在指定位置添加語句之外,請不要改動程序中的其他內容。

      源程序文件kt7_3.cpp清單如下:

      #include

      using namespace std;

      class Circle

      {

      public:

      Circle():radius(5){}

      //**1**

      void SetRadius(int r){radius=r;}

      int GetRadius(){return radius;}

      //**2**

      friend void CompareR(Circle*c1,Circle*c2);

      private:

      int radius;

      };

      void CompareR(Circle *c1,Circle *c2)

      {

      //**3**

      cout<<"c1>c2"<

      else if((c1->GetRadius())==(c2->GetRadius()))

      cout<<"c1=c2"<

      else if((c1->GetRadius())<(c2->GetRadius()))

      cout<<"c1

      void main()

      {

      Circle c1;

      c1.SetRadius(5);

      c1.Print();

      Circle c2(10);

      c2.Print();

      CompareR(&c1,&c2);

      }

      【參考答案】

      (1)Circle(int rad):radius(rad){}

      (2) void Print(){cout<<"The circle has radus of "<

      (3)if ((c1->GetRadius())>(c2->GetRadius()))

      【試題解析】本題考查成員函數的定義與實現,友元函數,if分支語句等知識點。友元函數的類體外的定義與一般函數一樣,其中if-else的使用,else總是與其最近的那個if配對使用的,書寫時最好使用縮進格式,將配對的if-else對齊,以免出錯

      #include

      using namespace std;

      class Circle

      {

      public:

      Circle():radius(5){}

      //**1**

      int r;

      Circle(int r):radius(r){}

      void SetRadius(int r){radius=r;}

      int GetRadius(){return radius;}

      //**2**

      void Print(){cout<<"The circle has radus of "<

      friend void CompareR(Circle*c1,Circle*c2);

      private:

      int radius;

      };

      void CompareR(Circle *c1,Circle *c2)

      {

      //**3**

      if((c1->GetRadius())>(c2->GetRadius()))

      cout<<"c1>c2"<

      else if((c1->GetRadius())==(c2->GetRadius()))

      cout<<"c1=c2"<

      else if((c1->GetRadius())<(c2->GetRadius()))

      cout<<"c1

      }

      void main()

      {

      Circle c1;

      c1.SetRadius(5);

      c1.Print();

      Circle c2(10);

      c2.Print();

      CompareR(&c1,&c2);

      }

    【計算機二級C++模擬試題及答案】相關文章:

    2016年計算機二級C++模擬試題及答案08-25

    2016計算機等級考試二級C++上機模擬試題及答案07-14

    計算機二級考試C++試題及答案08-01

    計算機二級考試C++試題及答案10-08

    2017年9月計算機二級C++考試模擬試題及答案10-08

    2016計算機二級模擬試題及答案07-16

    計算機二級考試模擬試題及答案10-29

    計算機二級Office模擬試題及答案09-02

    計算機二級Access模擬試題及答案06-11

    計算機二級沖刺模擬試題及答案10-29

    主站蜘蛛池模板: 欧美精品888| 欧美成人精品网站播放 | 国产精品免费无遮挡无码永久视频| 亚洲综合精品香蕉久久网| 国产精品极品| 国内精品免费在线观看 | 国产精品禁18久久久夂久| 亚洲国产精品毛片av不卡在线| 成人精品一区二区三区| www国产精品| 国精品午夜福利视频不卡麻豆 | 久久亚洲精品成人AV| 无码国内精品久久人妻麻豆按摩| 91精品国产综合久久香蕉| 99re这里只有精品热久久 | 十八18禁国产精品www| 香蕉依依精品视频在线播放| 国产亚洲精品精品国产亚洲综合| 亚洲午夜精品久久久久久人妖 | 中文字幕久久精品| 欧美精品黑人粗大视频| 国产精品无码无卡无需播放器| 欧美日韩精品一区二区| 国产亚洲精品xxx| 精品免费视在线观看| 国产日韩一区在线精品欧美玲| 97视频在线观看这里只有精品| 国产精品视频免费| 精品免费视在线观看| 亚洲愉拍自拍欧美精品| 青青青国产精品国产精品久久久久 | 91精品啪在线观看国产电影| 久久久国产精品福利免费| 国产精品亚洲专区在线观看| 欧美精品xxxxbbbb| 99爱在线精品免费观看| 精品久久久久久无码人妻热| 国产乱码精品一品二品| 久久久精品人妻一区二区三区四 | 国产在线国偷精品免费看| 国产精品午夜福利在线无码|