1. 首页
  2. IT资讯

C++ 简单实现数组类泛型编程示例

原创:
C++ 简单实现数组类泛型编程示例

1、使用模板来实现泛型编程
2、本数组应该能够存储各种基础类型,各种复杂的类类型
3、应该实现部分操作符重载

其实操作符重载满满的都是套路。

代码如下:

点击(此处)折叠或打开

  1. 模板类实现:
  2. /*************************************************************************
  3.     > File Name: arra.cpp
  4.     > Author: gaopeng QQ:22389860 all right reserved
  5.     > Mail: gaopp_200217@163.com
  6.     > Created Time: Mon 10 Apr 2017 08:28:01 AM CST
  7.  ************************************************************************/
  8. #include<iostream>
  9. #include <stdlib.h>
  10.  #include <string.h>
  11. using namespace std;
  12. template <typename T>
  13. class myarray
  14. {
  15.         private:
  16.                 T* array;
  17.                 unsigned int lenth;
  18.         public:
  19.                 myarray();
  20.                 myarray(unsigned int len);
  21.                 myarray(const myarray& a);
  22.                 myarray& operator=(const myarray& b);
  23.                 T& operator[](int ind);
  24.                 ~myarray();
  25. };
  26. template <typename T>
  27. myarray<T>::~myarray()
  28. {
  29.         if(this>array != NULL)
  30.         {
  31.                 delete [] this>array;
  32.                 this>array = NULL;
  33.         }
  34. }
  35. template <typename T>
  36. myarray<T>::myarray()
  37. {
  38.         this>array = NULL;
  39.         this>lenth = 0;
  40. }
  41. template <typename T>
  42. myarray<T>::myarray(unsigned int len)
  43. {
  44.         this>array = new T[len];
  45.         this>lenth = len;
  46.         memset(this>array,0,sizeof(T)*len);
  47. }
  48. template <typename T>
  49. myarray<T>::myarray(const myarray<T>& a)
  50. {
  51.         int i;
  52.         this>lenth = a.lenth;
  53.         this>array = new T[a.lenth];
  54.         memset(this>array,0,sizeof(T)*a.lenth);
  55.         for(i=0;i<a.lenth;i++)
  56.         {
  57.                 *(this>array+i) = *(a.array+i);
  58.         }
  59. }
  60. template <typename T>
  61. myarray<T>& myarray<T>::operator=(const myarray<T>& a)
  62. {
  63.         if(this>array != NULL)
  64.         {
  65.                 delete [] this>array;//调用类的析构函数不能用free
  66.                 this>array = NULL;
  67.         }
  68.         this>array = new T[a.lenth];
  69.         this>lenth = a.lenth;
  70.         for(int i=0;i<a.lenth;i++)
  71.         {
  72.                 *(this>array+i) = *(a.array+i);//元素对象复制调用对象的=操作符重载
  73.         }
  74.         return *this;
  75. }
  76. template <typename T>
  77. T& myarray<T>::operator[](int ind)
  78. {
  79.         if(ind>=this>lenth)
  80.         {
  81.                 exit(10);
  82.         }
  83.         return *(this>array+ind);
  84. }

点击(此处)折叠或打开

  1. 测试
  2. /*************************************************************************
  3.     > File Name: main.cpp
  4.     > Author: gaopeng QQ:22389860 all right reserved
  5.     > Mail: gaopp_200217@163.com
  6.     > Created Time: Mon 10 Apr 2017 08:31:57 AM CST
  7.  ************************************************************************/
  8. #include<iostream>
  9. #include<stdlib.h>
  10. #include<string.h>
  11. #include“arra.cpp”
  12. using namespace std;
  13. class test
  14. {
  15.         private:
  16.                 int a;
  17.                 int b;
  18.                 char* myc;
  19.         public:
  20.                 test()
  21.                 {
  22.                         a = 0;
  23.                         b = 0;
  24.                         myc = NULL;
  25.                 }
  26.                 test(const test& a)
  27.                 {
  28.                         this>a = a.a;
  29.                         this>b = a.b;
  30.                         this>myc = (char*)calloc(strlen(a.myc)+1,0);
  31.                         strcpy(this>myc,a.myc);
  32.                 }
  33.                 friend ostream& operator<<(ostream& out,test& a)
  34.                 {
  35.                         out<<a.a<<endl;
  36.                         out<<a.b<<endl;
  37.                         cout<<a.myc;
  38.                         return out;
  39.                 }
  40.                 ~test()
  41.                 {
  42.                         if(myc != NULL)
  43.                         {
  44.                                 free(myc);
  45.                                 myc = NULL;
  46.                         }
  47.                 }
  48.                 test& operator=(const test& a)
  49.                 {
  50.                         if(this>myc != NULL)
  51.                         {
  52.                                 free(this>myc);
  53.                                 this>myc = NULL;
  54.                         }
  55.                         this>a = a.a;
  56.                         this>b = a.b;
  57.                         this>myc = (char*)calloc(strlen(a.myc)+1,0);
  58.                         strcpy(this>myc,a.myc);
  59.                         return *this;
  60.                 }
  61.                 test& operator=(const char* a)
  62.                 {
  63.                         if(this>myc != NULL)
  64.                         {
  65.                                 free(this>myc);
  66.                                 this>myc = NULL;
  67.                         }
  68.                         if(a == NULL)
  69.                         {
  70.                                 this>myc = (char*)calloc(1,0);
  71.                                 return *this;
  72.                         }
  73.                         this>myc = (char*)calloc(strlen(a)+1,0);
  74.                         strcpy(this>myc,a);
  75.                         return *this;
  76.                 }
  77. };
  78. int main()
  79. {
  80.         myarray<test> a(3); //测试class类数组
  81.         a[0] = “asdasd”;
  82.         a[1] = “test”;
  83.         a[2] = “kkkk”;
  84.         myarray<int> b(3); //测试int数组
  85.         b[0] = 1;
  86.         b[1] = 2;
  87.         b[2] = 3;
  88.         myarray<char> c(3); //测试char数组
  89.         c[0] = a;
  90.         c[1] = b;
  91.         c[2] = c;
  92.         myarray<test> d = a;//测试myarray拷贝构造函数
  93.         for(int i=0;i<3;i++)
  94.         {
  95.                 cout<<a[i]<<endl;
  96.                 cout<<d[i]<<endl;
  97.         }
  98.         for(int i=0;i<3;i++)
  99.         {
  100.                 cout<<b[i]<<endl;
  101.         }
  102.         for(int i=0;i<3;i++)
  103.         {
  104.                  cout<<c[i]<<endl;
  105.         }
  106. }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7728585/viewspace-2136044/,如需转载,请注明出处,否则将追究法律责任。

主题测试文章,只做测试使用。发布者:深沉的少年,转转请注明出处:http://www.cxybcw.com/183822.html

联系我们

13687733322

在线咨询:点击这里给我发消息

邮件:1877088071@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code