博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 简单实现数组类泛型编程示例
阅读量:6628 次
发布时间:2019-06-25

本文共 3390 字,大约阅读时间需要 11 分钟。

原创:
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. }

转载地址:http://qyhpo.baihongyu.com/

你可能感兴趣的文章
配置XenDesktop一例报错-序列不包含任何元素
查看>>
javascript理解数组和数字排序
查看>>
微软同步框架入门之五--使用WCF同步远程数据
查看>>
Last-Modified、If-Modified-Since 实现缓存和 OutputCache 的区别
查看>>
理解SQL代理错误日志
查看>>
Multipart Internet Mail Extensions (MIME)
查看>>
C# WinForm控件之Dock顺序调整
查看>>
中控科技 ZK Software的售后服务真像一坨屎,技术人员嚣张
查看>>
NSPredicate过滤数组数据
查看>>
设置MYSQL允许用IP访问
查看>>
spark 数据预处理 特征标准化 归一化模块
查看>>
大道至简,系统设计和模块划分的实用经验之谈
查看>>
正则表达式中参数g、i、m的作用(share)
查看>>
使用Solr构建企业级的全文检索(四)---------写入文档
查看>>
squid的正向代理和反向代理
查看>>
linux下命令与文件的查询
查看>>
SEO意识的网站设计:设计和SEO的完美结合可能么?
查看>>
IP 算法
查看>>
IBM_System_x3650服务器固件升级手顺
查看>>
awk单行脚本
查看>>