當(dāng)前位置:高考升學(xué)網(wǎng) > 招聘筆試題 > 正文

上海貝爾筆試真題(三)

更新:2023-09-17 19:43:54 高考升學(xué)網(wǎng)

標(biāo)準(zhǔn)答案:

  // String的析構(gòu)函數(shù)

  String::~String(void) // 3分

  {

  delete [] m_data;

  // 由于m_data是內(nèi)部數(shù)據(jù)類型,也可以寫成 delete m_data;

  }

  // String的普通構(gòu)造函數(shù)

  String::String(const char str) // 6分

  {

  if(str==NULL)

  {

  m_data = new char; // 若能加 NULL 判斷則更好

  m_data = ‘\0’;

  }

  else

  {

  int length = strlen(str);

  m_data = new char[length+1]; // 若能加 NULL 判斷則更好

  strcpy(m_data, str);

  }

  }

  // 拷貝構(gòu)造函數(shù)

  String::String(const String &other) // 3分

  {

  int length = strlen(other.m_data);

  m_data = new char[length+1]; // 若能加 NULL 判斷則更好

  strcpy(m_data, other.m_data);

  }

  // 賦值函數(shù)

  String & String::operate =(const String &other) // 13分

  {

  // (1) 檢查自賦值 // 4分

  if(this == &other)

  return this;

  // (2) 釋放原有的內(nèi)存資源 // 3分

  delete [] m_data;

  // (3)分配新的內(nèi)存資源,并復(fù)制內(nèi)容 // 3分

  int length = strlen(other.m_data);

  m_data = new char[length+1]; // 若能加 NULL 判斷則更好

  strcpy(m_data, other.m_data);

  // (4)返回本對象的引用 // 3分

相關(guān)文章

最新圖文

9999久久久国产精品,日韩在线一区二区三区欧美,日韩精品综合在线人妻,免费AAAAAA毛片看
这里只有精品久久 | 亚洲国产精品第一区二区三区 | 久久久国产精品天天影视 | 九九国产精品视频 | 中文字AV字幕在线观看老师 | 外国一级AⅤ在线观看 |