计算机二级C 技巧:二维动态数组类模板计算机二级考试

文章作者 100test 发表时间 2009:04:30 05:23:56
来源 100Test.Com百考试题网


  2009年下半年全国计算机等级考试你准备好了没?考计算机等级考试的朋友,2009年下半年全国计算机等级考试时间是2009年9月19日至23日。更多优质资料尽在百考试题论坛 百考试题在线题库
  C 提供了许多强大的机制来实现代码的高度复用、来使我们使用我们自已的类就像使用内置类型那样方便快捷。比如模板,运算符重载等等。模板好比如是一个大批量生产函数和类的工厂,使我们不用再去关心与数据类型相关的繁琐编程细节,把我们精力留给那些真正值得我们去认真思考的地方。而运算符重载则使我们的程序更直观更简洁,这不仅使我们更容易读懂我们的程序,而且使我们能以一种更为流畅的方式来表达我们的想法。就像上篇文章说到的,如果我们把动态分配的二维数组用类模板实现,并重载相应的操作符,我们就能十分方便的使用我们自己定义的数组类型了。今天我正好把以往的程序整理了一下,就一并贴了出来,看下面的程序。下面的两个类模板在vc 6.0上编译运行良好,测试程序也都得到正确的结果,如果在其他编译环境下有什么问题,欢迎给我留言。
  第一个头文件Array.h是一维动态数组类模板:
  //Array.h
  #ifndef CARL_SEN_ARRAY_H
  #define CARL_SEN_ARRAY_H
  #include <.iostream>.
  #include <.cstdarg>.
  using std::out_of_range.
  using std::ostream.
  template<.typename T>.
  class Array {
  protected:
  unsigned int size.
  T* data.
  public:
  //构造函数
  Array(unsigned int _size=0).
  Array(unsigned int count, T data1, ...).
  //复制控制
  Array(const Array<.T>.&. array).
  Array&. operator=(const Array<.T>.&. array).
  ~Array() {
  0delete[] data.
  }
  //两种重载运算符
  T&. operator[](unsigned int index).
  const T&. operator[](unsigned int index) const.
  friend ostream&. operator<.<.(ostream&. os, const Array<.T>.&. array).
  //get,set成员函数
  unsigned int getSize() const {
  return size.
  }
  void setSize(unsigned int newSize).
  }.
  template <.typename T>.
  Array<.T>.::Array(unsigned int _size):data(new T[_size]), size(_size) {
  for(unsigned int i=0. i<.size. i) {
  data[i]=T().
  }
  }
  template <.typename T>.
  Array<.T>.::Array(unsigned int count, T data1, ...):size(count), data(new T[count]) {
  va_list ap.
  va_start(ap, count).
  for(unsigned int i=0. i<.size. i) {
  data[i]=va_arg(ap, T).
  }
  va_end(ap).
  }
  template <.typename T>.
  Array<.T>.::Array(const Array<.T>.&. array):size(array.size), data(new T[array.size]) {
  for(unsigned int i=0. i<.size. i) {
  data[i]=array.data[i].
  }
  }
  template <.typename T>.
  Array<.T>.&. Array<.T>.::operator=(const Array<.T>.&. array) {
  if(&.array!=this) {
  0delete[] data.
  data=new T[array.size].
  size=array.size.
  for(unsigned int i=0. i<.size. i) {
  data[i]=array.data[i].
  }
  }
  return *this.
  }
  template <.typename T>.
  T&. Array<.T>.::operator[](unsigned int index) {
  if(index>.=size) {
  throw out_of_range("invalid index").
  }
  return data[index].
  }
  template <.typename T>.
  const T&. Array<.T>.::operator[](unsigned int index) const {
  if(index>.=size) {
  throw out_of_range("invalid index").
  }
  return data[index].
  }
  template <.typename T>.
  void Array<.T>.::setSize(unsigned int newSize) {
  T* const newData=new T[newSize].
  const unsigned int Min=size<.newSize?size:newSize.
  for(unsigned int i=0. i<.Min. i) {
  newData[i]=data[i].
  }
  0delete[] data.
  data=newData.
  size=newSize.
  }
  template <.typename T>.
  ostream&. operator<.<.(ostream&. os, const Array<.T>.&. array) {
  for(int i=0. i<.array.size. i) {
  os<.<.array.data[i]<.<."\t".
  }
  return os.
  }
  #endif

相关文章


计算机二级:C 三种排序算法实例代码计算机二级考试
Windows下基于C 的RRDTOOL命令行封装计算机二级考试
计算机二级C 技巧:二维动态数组类模板计算机二级考试
计算机二级C :编三维地质建模系统整体思路计算机二级考试
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛