天天看點

C++ 簡單實作數組類泛型程式設計示例

原創:

C++ 簡單實作數組類泛型程式設計示例

1、使用模闆來實作泛型程式設計

2、本數組應該能夠存儲各種基礎類型,各種複雜的類類型

3、應該實作部分操作符重載

其實操作符重載滿滿的都是套路。

代碼如下:

點選(此處)折疊或打開

模闆類實作:

/*************************************************************************

    > File Name: arra.cpp

    > Author: gaopeng QQ:22389860 all right reserved

    > Mail: [email protected]

    > Created Time: Mon 10 Apr 2017 08:28:01 AM CST

 ************************************************************************/

#include<iostream>

#include <stdlib.h>

 #include <string.h>

using namespace std;

template <typename T>

class myarray

{

        private:

                T* array;

                unsigned int lenth;

        public:

                myarray();

                myarray(unsigned int len);

                myarray(const myarray& a);

                myarray& operator=(const myarray& b);

                T& operator[](int ind);

                ~myarray();

};

myarray<T>::~myarray()

        if(this->array != NULL)

        {

                delete [] this->array;

                this->array = NULL;

        }

}

myarray<T>::myarray()

        this->array = NULL;

        this->lenth = 0;

myarray<T>::myarray(unsigned int len)

        this->array = new T[len];

        this->lenth = len;

        memset(this->array,0,sizeof(T)*len);

myarray<T>::myarray(const myarray<T>& a)

        int i;

        this->lenth = a.lenth;

        this->array = new T[a.lenth];

        memset(this->array,0,sizeof(T)*a.lenth);

        for(i=0;i<a.lenth;i++)

                *(this->array+i) = *(a.array+i);

myarray<T>& myarray<T>::operator=(const myarray<T>& a)

                delete [] this->array;//調用類的析構函數不能用free

        for(int i=0;i<a.lenth;i++)

                *(this->array+i) = *(a.array+i);//元素對象複制調用對象的=操作符重載

        return *this;

T& myarray<T>::operator[](int ind)

        if(ind>=this->lenth)

                exit(10);

        return *(this->array+ind);

測試

    > File Name: main.cpp

    > Created Time: Mon 10 Apr 2017 08:31:57 AM CST

#include<stdlib.h>

#include<string.h>

#include"arra.cpp"

class test

                int a;

                int b;

                char* myc;

                test()

                {

                        a = 0;

                        b = 0;

                        myc = NULL;

                }

                test(const test& a)

                        this->a = a.a;

                        this->b = a.b;

                        this->myc = (char*)calloc(strlen(a.myc)+1,0);

                        strcpy(this->myc,a.myc);

                friend ostream& operator<<(ostream& out,test& a)

                        out<<a.a<<endl;

                        out<<a.b<<endl;

                        cout<<a.myc;

                        return out;

                ~test()

                        if(myc != NULL)

                        {

                                free(myc);

                                myc = NULL;

                        }

                test& operator=(const test& a)

                        if(this->myc != NULL)

                                free(this->myc);

                                this->myc = NULL;

                        return *this;

                test& operator=(const char* a)

                        if(a == NULL)

                                this->myc = (char*)calloc(1,0);

                                return *this;

                        this->myc = (char*)calloc(strlen(a)+1,0);

                        strcpy(this->myc,a);

int main()

        myarray<test> a(3); //測試class類數組

        a[0] = "asdasd";

        a[1] = "test";

        a[2] = "kkkk";

        myarray<int> b(3); //測試int數組

        b[0] = 1;

        b[1] = 2;

        b[2] = 3;

        myarray<char> c(3); //測試char數組

        c[0] = 'a';

        c[1] = 'b';

        c[2] = 'c';

        myarray<test> d = a;//測試myarray拷貝構造函數

        for(int i=0;i<3;i++)

                cout<<a[i]<<endl;

                cout<<d[i]<<endl;

                cout<<b[i]<<endl;

                 cout<<c[i]<<endl;

繼續閱讀