第一百一十四天学习记录:C++提高:类模板案例(黑马教学视频)

类模板案例


main.cpp代码:

cpp 复制代码
#include "myarray.hpp"

void printIntArray(MyArray <int>& arr)
{
	for (int i = 0; i < arr.getSize(); ++i)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
}

void test01()
{
	MyArray <int>arr1(5);
	for (int i = 0; i < 5; ++i)
	{
		//利用尾插法向数组中插入数据
		arr1.Push_Back(i);
	}
	cout << "arr1的打印输出为:" << endl;
	printIntArray(arr1);
	cout << "arr1的容量为:" << arr1.getCapacity() << endl;
	cout << "arr1的大小为:" << arr1.getSize() << endl;
	MyArray <int>arr2(arr1);
	cout << "arr2的打印输出为:" << endl;
	printIntArray(arr2);
	//尾删
	arr2.Pop_Back();
	cout << "arr2尾删后:" << endl;
	cout << "arr2的容量为:" << arr2.getCapacity() << endl;
	cout << "arr2的大小为:" << arr2.getSize() << endl;
	//MyArray <int>arr3(100);
	//arr3 = arr1;
}

class Person
{
public:
	Person() 
	{
		//cout << "调用默认构造Person" << endl;
	}
	Person(string name, int age)
	{
		//cout << "调用有参构造Person" << endl;
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};

void printPersonArray(MyArray<Person>& arr)
{
	for (int i = 0; i < arr.getSize(); i++)
	{
		cout << "姓名:" << arr[i].m_Name << "年龄:" << arr[i].m_Age << endl;
	}
}

void test02()
{
	MyArray<Person> arr(10);
	Person p1("张三", 32);
	Person p2("赵四", 33);
	Person p3("王五", 35);
	Person p4("杨一", 25);
	//将数据插入到数组中
	arr.Push_Back(p1);
	arr.Push_Back(p2);
	arr.Push_Back(p3);
	arr.Push_Back(p4);
	//打印数组
	printPersonArray(arr);
	//输出容量
	cout << "arr容量为:" << arr.getCapacity() << endl;
	//输出大小
	cout << "arr大小为:" << arr.getSize() << endl;
}

int main(int argc,char* argv[])
{
	//for (int i = 0; i < argc; ++i)
	//{
	//	cout << argv[i] << endl;
	//}
	test01();
	test02();
	return 0;
}

myarray.hpp代码:

cpp 复制代码
#pragma once
#include <iostream>
#include <string>
using namespace std;

template<class T>
class MyArray
{
public:
	//有参构造  参数 容量
	MyArray(int capacity)
	{
		//cout << "调用构造函数" << endl;
		this->m_Capacity = capacity;
		this->m_Size = 0;
		this->pAddress = new T[this->m_Capacity];
	}

	//拷贝构造
	MyArray(const MyArray& arr)
	{
		//cout << "调用拷贝函数" << endl;
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		//this->pAddress = arr.pAddress;
		this->pAddress = new T[arr.m_Capacity];//深拷贝
		//将arr中的数据都拷贝过来
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
	}

	MyArray& operator=(const MyArray& arr)
	{
		//先判断原来堆区是否有数据,如果有,先释放
		//cout << "调用赋值函数" << endl;
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}

		//深拷贝
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		this->pAddress = new T[arr.m_Capacity];
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
		return *this;
	}

	//尾插法
	void Push_Back(const T& val)
	{
		//判断容量是否等于大小
		if (this->m_Capacity == this->m_Size)
		{
			return;
		}
		this->pAddress[this->m_Size] = val;//在数组末尾插入数据
		this->m_Size++;//更新数组大小
	}

	//尾删法
	void Pop_Back()
	{
		//让用户访问不到最后一个元素,即为尾删,逻辑删除
		if (this->m_Size == 0)
		{
			return;
		}
		this->m_Size--;
	}

	//通过下标方式访问数组中的元素 arr[0]
	T& operator[](int index)
	{
		return this->pAddress[index];
	}

	//返回数组容量
	int getCapacity()
	{
		return this->m_Capacity;
	}

	//返回数组大小
	int getSize()
	{
		return this->m_Size;
	}

	//析构函数
	~MyArray()
	{
		//cout << "调用析构函数" << endl;
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}
	
private:
	T* pAddress;//指针指向堆区开辟的真实数组
	int m_Capacity;//数组容量
	int m_Size;//数组大小
};

输出:

总结:能够利用所学知识点实现通用的数组。

个人感悟:学习了这个案例之后接下来的课程就是STL了,STL其实在我工作中写代码时经常碰到,尤其是在读同事代码的时候,因为没有学过,通过自行百度获得碎片的知识导致读起来很很吃力。这个案例给我的感觉应该是老师在模仿Vector类。相信将STL学习完之后对我工作效率会极大的提升。

相关推荐
Chester_199912 分钟前
CSP202203C.计算资源调度器
开发语言·数据结构·c++·蓝桥杯
会周易的程序员2 小时前
给 PLC 写一个字节码虚拟机:STVM 虚拟机架构设计
开发语言·c++·虚拟机·软plc·iec61131·stvm
小灰灰搞电子2 小时前
完全驾驭 Qt 与数据库:C++ ORM 框架 QxOrm 原理与实践指南
数据库·c++·qt
QT界面美化性能优化2 小时前
QT+AI:使用AI技术为QT应用程序赋能
c++·人工智能·qt·opencv·qt教程·qt6.3
是隼人3 小时前
buuctf-pwn xdctf2015_pwn200题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
大衛說3 小时前
《Python 从入门到精通》系列总览与学习路线
开发语言·python·学习
吃好睡好便好4 小时前
查找函数的使用
学习·算法·matlab·生活·查找函数
学习星球4 小时前
单调栈——从“找下一个更大的“到柱状图中的最大矩形
数据库·c++·算法·leetcode·xcode
学运维的Kysan4 小时前
暑假运维学习打卡第二十九天8.29
学习
zbyyd5 小时前
Linux 进程间通信学习笔记
linux·笔记·学习