服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++中类模板的应用你了解多少

C++中类模板的应用你了解多少

2022-09-27 15:33是小明同学啊 C/C++

这篇文章主要为大家详细介绍了C++中类模板的应用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助

类模板应用

数组类的封装

属性:

?
1
2
3
1,T *pAddress 指向堆区数组的指针。
2,int m_Capacity 数组容量
3,int m_Size 数组大小

行为:

?
1
2
3
4
5
6
7
8
1,myArray(int capacity) 构造函数
2,myArray(const MyArray&arr) 拷贝构造函数
3,operator= 重载赋值操作符=
4,operator[] 重载中括号[]
5,~myArray()  析构函数
6,getCapacity 获取容量
7,getSize     获取大小
8,pushback   尾插

将头文件与实现文件写到一起,后缀是.hpp

Int的.hpp文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<string>
template<class T>
class MyArray
{
public:
    MyArray() {};//默认构造
    MyArray(int capacity)//有参构造
    {
        this->m_Capacity = capacity;
        this->m_Size = 0;
        this->pAddress = new T[this->m_Capacity];
    }
    MyArray(const MyArray& arr)//拷贝构造
    {
        this->m_Capacity = arr.m_Capacity;
        this->m_Size = arr.m_Size;
        this->pAddress = new T[this->m_Capacity];//这个不能直接拷贝,需要自己重新创建
        for (int i = 0; i < arr.m_Size; i++)//然后将数组的元素一个个的赋值过来
        {
            this->pAddress[i] = arr.pAddress[i];
        }
    }
    MyArray& operator=(const MyArray &arr)//重载赋值操作符=(返回自身的引用)
    {
        if (this->pAddress)//如果原先有数据了,那么就删除
        {
            delete[] this->pAddress;
            this->pAddress = NULL;
        }
        //然后进行深拷贝
        this->m_Capacity = arr.m_Capacity;
        this->m_Size = arr.m_Size;
        this->pAddress = new T[this->m_Capacity];//这个不能直接拷贝,需要自己重新创建
        for (int i = 0; i < arr.m_Size; i++)//然后将数组的元素一个个的赋值过来
        {
            this->pAddress[i] = arr.pAddress[i];
        }
        return *this;
    }
    T& operator[](int dex)//重载[] 为了访问数组中的值,
    {
        return this->pAddress[dex];
    }
 
    void pushBack(const T& val)//尾插
    {
        if (this->m_Capacity <= this->m_Size)//如果已经超过范围了
        {
            return;
        }
        this->pAddress[this->m_Size] = val;
        this->m_Size++;
    }
    int getCapacity()//获取数组容量
    {
        return this->m_Capacity;
    }
    int getSize()//获取数组大小
    {
        return this->m_Size;
    }
    ~MyArray()//析构
    {
        if (this->pAddress)
        {
            delete[] this->pAddress;
            this->pAddress = NULL;
        }
    }
private:    
    T* pAddress;//指向堆区真实数组指针
    int m_Capacity;//数组容量
    int m_Size;
};

int的测试文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<string>
#include"myArray.hpp"
void myPrint(MyArray<int> &myIntArray)
{
    for (int i = 0; i < myIntArray.getSize(); i++)
    {
        cout << myIntArray[i] << endl;
    }
}
int main()
{
    MyArray<int> myIntArray(100);
    for (int i = 0; i < 10; i++)
    {
        myIntArray.pushBack(i + 100);
    }
    myPrint(myIntArray);
    return 0;
}

输出结果:

100
101
102
103
104
105
106
107
108
109

以上代码证明写的数组类的封装对内置数据类型是适用的,接下来试试自定义类型Person

ps:如果识别出来了是要开辟Person类的数组的空间,需要调用Person的默认构造(有参构造不行),所以必须在Person类中加一个默认构造。

Person类的.hpp文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<string>
 
template<class T>
class MyArray
{
public:
    MyArray() {};//默认构造
    MyArray(int capacity)//有参构造
    {
        this->m_Capacity = capacity;
        this->m_Size = 0;
        this->pAddress = new T[this->m_Capacity];
    }
    MyArray(const MyArray& arr)//拷贝构造
    {
        this->m_Capacity = arr.m_Capacity;
        this->m_Size = arr.m_Size;
        this->pAddress = new T[this->m_Capacity];//这个不能直接拷贝,需要自己重新创建
        for (int i = 0; i < arr.m_Size; i++)//然后将数组的元素一个个的赋值过来
        {
            this->pAddress[i] = arr.pAddress[i];
        }
    }
    MyArray& operator=(const MyArray &arr)//重载赋值操作(返回自身的引用)
    {
        if (this->pAddress)//如果原先有数据了,那么就删除
        {
            delete[] this->pAddress;
            this->pAddress = NULL;
        }
        //然后进行深拷贝
        this->m_Capacity = arr.m_Capacity;
        this->m_Size = arr.m_Size;
        this->pAddress = new T[this->m_Capacity];//这个不能直接拷贝,需要自己重新创建
        for (int i = 0; i < arr.m_Size; i++)//然后将数组的元素一个个的赋值过来
        {
            this->pAddress[i] = arr.pAddress[i];
        }
        return *this;
    }
    T& operator[](int dex)//重载[] 为了访问数组中的值,
    {
        return this->pAddress[dex];
    }
 
    void pushBack(const T& val)//尾插
    {
        if (this->m_Capacity <= this->m_Size)//如果已经超过范围了
        {
            return;
        }
        this->pAddress[this->m_Size] = val;
        this->m_Size++;
    }
    int getCapacity()//获取数组容量
    {
        return this->m_Capacity;
    }
    int getSize()//获取数组大小
    {
        return this->m_Size;
    }
    ~MyArray()//析构
    {
        if (this->pAddress)
        {
            delete[] this->pAddress;
            this->pAddress = NULL;
        }
    }
private:    
    T* pAddress;//指向堆区真实数组指针
    int m_Capacity;//数组容量
    int m_Size;
};

Person类的测试文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<string>
#include"myArray.hpp"
class Person
{
public:
    Person() {};
    string m_name;
    int m_age;
    Person(string name, int age)
    {
        this->m_age = age;
        this->m_name = name;
    }
};
void myPrintInt(MyArray<int> &myIntArray)//int的
{
    for (int i = 0; i < myIntArray.getSize(); i++)
    {
        cout << myIntArray[i] << endl;
    }
}
void myPrintPerson(MyArray<Person>& myPersonArray)//Person的
{
    for (int i = 0; i < myPersonArray.getSize(); i++)
    {
        cout << myPersonArray[i].m_name << " " << myPersonArray[i].m_age << endl;
    }
}
int main()
{
    /*MyArray<int> myIntArray(100);
    for (int i = 0; i < 10; i++)
    {
        myIntArray.pushBack(i + 100);
    }
    myPrintInt(myIntArray);*/
    MyArray<Person>myPersonArray(100);
    Person p1("小明", 18);
    Person p2("小宏", 18);
    Person p3("小量", 19);
    Person p4("小应", 18);
    myPersonArray.pushBack(p1);
    myPersonArray.pushBack(p2);
    myPersonArray.pushBack(p3);
    myPersonArray.pushBack(p4);
    myPrintPerson(myPersonArray);
    cout << "数组容量:"<<myPersonArray.getCapacity()<< endl;//100
    cout << "数组大小:" << myPersonArray.getSize() << endl;//4
    return 0;
}

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注服务器之家的更多内容! 

原文链接:https://blog.csdn.net/qq_51399192/article/details/123029498

延伸 · 阅读

精彩推荐