试题答案

3773考试网计算机等级考试试题答案正文

2009年9月全国计算机等级考试二级C++真题及答案

来源:fjzsksw.com 2010-4-2 16:02:23

 

   (11)有如下类定义:
  class Sample{
  public:
  Sample();
  ~Sample();
  Private:
  Static int date;
  };
  将静态数据成员data初始化为0的语句是 【11】
  
  (12)“图形”类Shape中定义了纯虚函数CalArea(),“三角形”类Triangle继承了类Shape,请将Triangle类中的CalArea函数补充完整。
  class Shape{
  public:
  virtual int CalArea()=0;
  }
  class Triangle: public Shape{
  public:
  Triangle{int s, int h}: side(s),height(h) {}
   【12】 { return side*height/2 ; }
  private:
  int side;
  int height;
  };
  
  (13)有如下程序:
  #include <iostream>
  using namespace std;
  class GrandChild{
  public:
  GrandChild(){ strcpy (name,”Unknown”); }
  const char * getName()const { return name; }
  virtual char * getAddress()const=0;
  private:
  char name[20];
  };
  class GrandSon : public GrandChild{
  public:
  GrandSon{char *name} {}
  Char * getAddress() const { return “Shanghai”; }
  };
  int main(){
  GrandChild *gs=new GrandSon(“Feifei”);
  cout<<gs->getName()<<”住在”<<gs->getAddress()<<endl;
  delete gs;
  return 0;
  }
  运行时的输出结果是 【13】

   (14)如下程序定义了“单词”类word,类中重载了<运算符,用于比较“单词”的大小,返回相应的逻辑值。程序的输出结果为:After Sorting: Happy Welcome,请将程序补充完整。
  #include <iostream>
  #include <string>
  using namespace std;
  class Word{
  public:
  Word(string s) : str(s) { }
  string getStr(){ return str; }
   【14】 const { return (str<w.str); }
  friend ostream& operator << (ostream& output, const Word &w)
  { output<<w.str; return output; }
  private:
  string str;
  };
  Int main(){
  Word w1(“Happy”),w2(“Welcome”);
  Cout<<”After sorting: “;
  if(w1<w2) cout<<w1<<’ ’<<w2;
  else cout<<w2<<’ ‘<<w1;
  return 0;
  }
  
  (15)请将下列模板类Data补充完整。
  template <typename T>
  class Data{
  public:
  void put (T v) { val=v; }
   【15】 get()   //返回数据成员val的值,返回类型不加转换
  { return val; }
  private:
  T val;
  };

 

上一页  [1] [2] [3] [4] [5] [6] [7] 下一页

触屏版 电脑版
3773考试网 琼ICP备12003406号-1