C++ 簡單實現陣列類泛型程式設計示例

gaopengtttt發表於2017-03-24
原創:
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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章