C++ 进阶【03】

本文代码仓库地址: gitee码云CSDN笔记仓库地址



模板 – 类模板 – 分文件编写【template09_xy】

1、Main.cpp 文件

// 模板 -- 类模板 -- 分文件编写
/*
* 用一般的类方式进行书写会出现的问题:
* 1、类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到
* 
* 解决方式【2种】:
* 1、直接包含.cpp 源文件
* 2、将声明和现实写到通一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制的
*/
#include <iostream>
// 只声明.h的头文件会报错,因为类模板的成员函数的创建时机问题,导致创建类的时候链接不到其成员
#include "person02_xy.h"
// 1、直接包含.cpp 源文件【同时写上.h文件也是可以正常运行我的】
#include "person02_xy.cpp"
// 2、将声明和现实写到通一个文件中,并更改后缀名为.hpp【这种方式是主流我的写法】
#include "person03_xy.hpp"

using namespace std;


template<class T1, class T2>
class person01_xy {
public:
	T1 cName;
	T2 cAge;

	person01_xy(T1 name, T2 age) {
		this->cName = name;
		this->cAge = age;
	}

	void cShowPerson();
};

template<class T1, class T2>
void person01_xy<T1, T2>::cShowPerson() {
	cout << "姓名:" << this->cName << "  年龄:" << this->cAge << endl;
}

// 验证 
void mFun01() {
	person01_xy<string, int> p1("小印01", 22);
	p1.cShowPerson();

	cout << endl;
	person02_xy<string, int> p2("小印02", 23);
	p2.cShowPerson();

	cout << endl;
	person03_xy<string, int> p3("小印03", 24);
	p3.cShowPerson();
}


int main() {

	mFun01();
	cout << "------------------分界线------------------" << endl;

	cout << endl;
	system("pause");
	return 0;
}

2、person02_xy.h 文件

#pragma once

#include <iostream>

using namespace std;


template<class T1, class T2>
class person02_xy
{
public:
	T1 cName;
	T2 cAge;

	person02_xy(T1 name, T2 age);

	void cShowPerson();
};

3、person02_xy.cpp 文件

#include "person02_xy.h"

template<class T1, class T2>
person02_xy<T1, T2>::person02_xy(T1 name, T2 age) {
	this->cName = name;
	this->cAge = age;
}

template<class T1, class T2>
void person02_xy<T1, T2>::cShowPerson() {
	cout << "姓名:" << this->cName << "  年龄:" << this->cAge << endl;
}

4、person03_xy.hpp 文件

#pragma once

#include <iostream>

using namespace std;


template<class T1, class T2>
class person03_xy
{
public:
	T1 cName;
	T2 cAge;

	person03_xy(T1 name, T2 age);

	void cShowPerson();
};

template<class T1, class T2>
person03_xy<T1, T2>::person03_xy(T1 name, T2 age) {
	this->cName = name;
	this->cAge = age;
}

template<class T1, class T2>
void person03_xy<T1, T2>::cShowPerson() {
	cout << "姓名:" << this->cName << "  年龄:" << this->cAge << endl;
}

模板 – 类模板 – 类模板与友元【template10_xy】

// 模板 -- 类模板 -- 类模板与友元
/*
* 建议全局函数做类内实现,用法简单,而且编译器可以直接识别
*/
#include <iostream>

using namespace std;


// 3、同时全局函数声明的时候使用类型是 person01_xy,在其之前编译器也找不到这个类型的存在,所以需要在全局函数之前加上类的声明
template<class T1, class T2>
class person01_xy;

// 2、类外实现还需要实现让编译器知道这个函数的实现,不然找不到实现的方法
template<class T1, class T2>
void mShowPerson02(person01_xy<T1, T2>& p);

template<class T1, class T2>
class person01_xy {

	// 全局函数 显示person的信息【类内实现】没有 friend 就是成员函数【类内实现只需要这一步就好了】
	friend void mShowPerson01(person01_xy<T1, T2>& p) {
		cout << "姓名:" << p.cName << "  年龄:" << p.cAge << endl;
	}

	// 1、全局函数 显示person的信息【类外实现】【需要填加空模板参数列表,不然链接不到】
	friend void mShowPerson02<>(person01_xy<T1, T2>& p);

private:
	T1 cName;
	T2 cAge;

public:
	person01_xy(T1 name, T2 age) {
		this->cName = name;
		this->cAge = age;
	}
};

// 全局函数在内外实现
template<class T1, class T2>
void mShowPerson02(person01_xy<T1, T2>& p) {
	cout << "姓名:" << p.cName << "  年龄:" << p.cAge << endl;
}

// 验证 
void mFun01() {
	person01_xy<string, int> p1("小印01", 22);
	mShowPerson01(p1);

	cout << endl;
	person01_xy<string, int> p2("小印02", 23);
	mShowPerson02(p2);
}


int main() {

	mFun01();
	cout << "------------------分界线------------------" << endl;

	cout << endl;
	system("pause");
	return 0;
}

模板 – 类模板 – 案例【template11_xy】

Main.cpp 文件

// 模板 -- 类模板 -- 案例
/*
* 实现一个通用的数组类,要求如下:
* 1、可以对内置数据类型以及自定义数据类型的数据进行存储
* 2、将数组中的数据存储到堆区
* 3、构造函数中可以传入数组的容量
* 4、提供对应的拷贝构造函数以及 operator=防止浅拷贝问题
* 5、提供尾插法和尾删法队驻足中的数据进行增加和删除
* 6、可以通过下标的方式访问数组中的元素
* 7、可以获取数组中当前元素个数和数组的容量
*/
#include <iostream>
#include "cArray_xy.hpp"

using namespace std;


// 打印数组
void mPrintIntArray(cArray_xy<int>& arr) {
	for (int i = 0; i < arr.cGetSize(); i++)
	{
		// 6、可以通过下标的方式访问数组中的元素
		cout << "打印的 array[" << i << "] = " << arr[i] << endl;
	}
}

// 验证 1:默认数据类型2、3、4
void mFun01() {
	cArray_xy<int> arr01(5);
	cArray_xy<int> arr02(arr01);
	cArray_xy<int> arr03(10);
	arr03 = arr01;
}

// 验证 1:默认数据类型、5、6、7
void mFun02() {
	cArray_xy<int> arr01(5);
	for (int i = 0; i < 5; i++)
	{
		// 利用尾插法向数组中插入数据
		arr01.cPushBack(i);
		cout << "这里直接在赋值的时候打印的 array[" << i << "] = " << arr01[i] << endl;
	}
	cout << "arr01的打印为:" << endl;
	// 打印数组
	mPrintIntArray(arr01);
	// 7、可以获取数组中当前元素个数和数组的容量
	cout << "获取数组元素个数为:" << arr01.cGetSize() << endl;
	cout << "获取数组的容量为:" << arr01.cGetCapacity() << endl;
	// 5、利用尾删法向数组中删除数据
	arr01.cPopBack();
	cout << "进行一次尾删后数组元素个数为:" << arr01.cGetSize() << endl;
	cout << "进行一次尾删后 arr01的打印为:" << endl;
	mPrintIntArray(arr01);
	arr01.cPopBack();
	cout << "进行二次尾删后数组元素个数为:" << arr01.cGetSize() << endl;
	cout << "进行二次尾删后 arr01的打印为:" << endl;
	mPrintIntArray(arr01);
}

// 自定义数据类型 person01_xy
class person01_xy {
public:
	string cName;
	int cAge;

	person01_xy() {
		cAge = 0;
	}

	person01_xy(string name, int age) {
		this->cName = name;
		this->cAge = age;
	}

};

// 打印自定义数据类型 person01_xy 的函数
void mPrintPerson01_xyArray(cArray_xy<person01_xy>& arr) {
	for (int i = 0; i < arr.cGetSize(); i++)
	{
		cout << "姓名:" << arr[i].cName << "  年龄:" << arr[i].cAge << endl;
	}
}

// 验证 1:自定义类型、2、3、4、5、6、7
void mFun03() {
	cArray_xy<person01_xy> arr01(10);
	person01_xy p1("小印01", 22);
	person01_xy p2("小印02", 23);
	person01_xy p3("小印03", 24);
	person01_xy p4("小印04", 25);
	person01_xy p5("小印05", 26);
	// 将数据插入到数组中
	arr01.cPushBack(p1);
	arr01.cPushBack(p2);
	arr01.cPushBack(p3);
	arr01.cPushBack(p4);
	arr01.cPushBack(p5);
	cout << "arr01的打印为:" << endl;
	// 打印数组
	mPrintPerson01_xyArray(arr01);
	// 拷贝构造
	cArray_xy<person01_xy> arr02(arr01);
	cout << "arr02的打印为:" << endl;
	mPrintPerson01_xyArray(arr02);
	cArray_xy<person01_xy> arr03(20);
	// 重载 = 号运算符
	arr03 = arr01;
	cout << "arr03的打印为:" << endl;
	mPrintPerson01_xyArray(arr03);
	// 使用尾删法进行删除
	arr03.cPopBack();
	// 获取数组容量
	cout << "arr03 进行尾删后的容量大小:" << arr03.cGetCapacity() << endl;
	// 获取数组大小【元素个数】
	cout << "arr03 进行尾删后的元素个数:" << arr03.cGetCapacity() << endl;
	cout << "进行尾删后 arr03的打印为:" << endl;
	mPrintPerson01_xyArray(arr03);
}


int main() {

	mFun01();
	cout << "------------------分界线------------------" << endl;
	mFun02();
	cout << "------------------分界线------------------" << endl;
	mFun03();

	cout << endl;
	system("pause");
	return 0;
}

cArray_xy.hpp 文件

#pragma once
#include <iostream>
using namespace std;

template<class T>
class cArray_xy
{
private:
	// 数组【指针想想堆区开辟的真实数组】
	T* cAddress;
	// 数组容量【数组的总容量】
	int cCapacity;
	// 数组大小【数组汇总已存数据的个数】
	int cSize;

public:
	// 3、构造函数中可以传入数组的容量
	// 有参构造函数,传进来的参数为数组的容量
	cArray_xy(int capacity) {
		cout << "有参构造函数" << endl;
		this->cCapacity = capacity;
		this->cSize = 0;
		// 2、将数组中的数据存储到堆区
		// 手动在堆区开辟,需要手动在析构函数中释放
		this->cAddress = new T[this->cCapacity];
	}
	
	// 拷贝构造函数
	cArray_xy(const cArray_xy& arr) {
		cout << "拷贝构造函数" << endl;
		this->cCapacity = arr.cCapacity;
		this->cSize = arr.cSize;
		// 使用下面这句就会出现浅拷贝的情况
		// this->cAddress = arr.cAddress;
		// 需要使用下的拷贝,我就不会出现浅拷贝的情况——地址重复释放
		this->cAddress = new T[arr.cCapacity];
		// 重新开辟数组后,防止之前的数组中有数据,这里我们进行拷贝数组里面的数据
		for (int i = 0; i < this->cSize; i++)
		{
			this->cAddress[i] = arr.cAddress[i];
		}
	}
	
	// 4、提供对应的拷贝构造函数以及 operator=防止浅拷贝问题
	// 重载 = 号运算符,也是为了防止浅拷贝的问题
	cArray_xy& operator=(const cArray_xy& arr) {
		cout << "重载 = 号运算符" << endl;
		// 先判断原来数组堆区是否有数据,如果有先释放【判断需要赋值的数组】
		if (this->cAddress != NULL)
		{
			cout << "重载 = 中成功释放" << endl;
			delete[] this->cAddress;
			this->cAddress = NULL;
			// 既然是为了防止出现问题,相关的数据我们都进行重置
			this->cCapacity = 0;
			this->cSize = 0;
		}
		// 然后再进行深拷贝
		this->cCapacity = arr.cCapacity;
		this->cSize = arr.cSize;
		this->cAddress = new T[arr.cCapacity];
		for (int i = 0; i < this->cSize; i++)
		{
			this->cAddress[i] = arr.cAddress[i];
		}
		return *this;
	}

	// 尾插法
	void cPushBack(const T& val) {
		// 判断容量是否等于大小了
		if (this->cCapacity == this->cSize)
		{
			return;
		}
		// 给数组的尾部赋值
		this->cAddress[this->cSize] = val;
		// 更新数组中已存数据的个数
		this->cSize++;
	}

	// 尾删法
	void cPopBack() {
		// 判断数组中是否存在元素
		if (this->cSize == 0)
		{
			return;
		}
		// 进行假删除
		this->cSize--;
	}

	// 通过下标的方式访问数组中的元素【重载 [] 号运算符】
	T& operator[](int index) {
		return this->cAddress[index];
	}

	// 7、可以获取数组中当前数组的容量
	// 返回数组的容量
	int cGetCapacity() {
		return this->cCapacity;
	}

	// 7、可以获取数组中当前元素个数
	// 返回数组的大小
	int cGetSize() {
		return this->cSize;
	}

	// 析构函数
	~cArray_xy() {
		cout << "析构函数" << endl;
		// 这里进行判断数组是否为空
		if (this->cAddress != NULL)
		{
			cout << "析构中成功释放" << endl;
			// 不为空,这里我们就需要手动释放我们在堆区开辟的空间
			delete[] this->cAddress;
			this->cAddress = NULL;
		}
	}

};

一点点笔记,以便以后翻阅。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小印丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值