【C++】02 类模板

#include <iostream>
#include <string>
using namespace std;
 
 
template <class T1,class T2>
class Person{
    public :
        Person(T1 id,T2 name);
        void show();
        
    public:
        T1 mId;
        T2 mname;
};

//类模板成员函数类外实现 
template <class T1,class T2>
Person<T1,T2>::Person(T1 id,T2 name){
        this->mname = name;
        this->mId = id;
}

template <class T1,class T2>
void Person<T1,T2>::show(){
            cout<<"ID:"<<mId<<endl<<"Age:"<<mname<<endl;
        } 
    
void funtion(){
    Person<int,string> p1(10,"Jack");
    p1.show();
}
int main(){
    funtion();
    return 0;
}

注意:

1)类模板无法使用自动类型推导,必须使用指定类型显示

2)与普通类的成员函数不同,类模板的成员函数在调用时才创建

3)当子类继承的父类是类模板时,子类在声明时要指定父类中T的类型(如果不指定,编译器无法给子类分配内存),否则就将子类也声明为类模板

4)类模板的声明和实现可以一同写在.hpp文件中,让主函数文件直接包含

5)如果要将类的声明与实现分开成头文件和源文件编写,则主函数文件直接包含.cpp文件

 

类模板案例

.hpp文件

#pragma once
#include<iostream>
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[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];
		for(int i=0;i<this->m_size;i++){
			this->pAddress[i] = arr.pAddress[i];
		}
	}

	//operator= 防止浅拷贝问题
	MyArray& operator=(const MyArray& arr){
		cout<<" operator=构造函数"<<endl;
		//先判断原来堆区是否有数据,如果有先释放
		if(this->pAddress!=NULL){
			delete[] 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 ;
			cout<<"容量已满,插入失败、"<<endl;
			return ;
		}

		this->pAddress[this->m_size] = val;
		this->m_size++;
	}

	//尾删法
	void Pop_Back(){
		if(this->m_size==0){
			cout<<"容量为空,删除失败"<<endl;
			return ;
		}
		this->m_size--;
	}

	//通过下标方式访问数据
	T& operator[](int index){   //返回引用是为了能实现等号左边赋值操作,例如:arr[1] = 100;
		return this->pAddress[index];
	}

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

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

	~MyArray(){
		if(this->pAddress!=NULL){
			delete[] pAddress;
		}
	}

private:
	T* pAddress;   //指针指向丢去开辟的真实数组
	int m_capacity;  //数组容量
	int m_size;   //数组大小
};

.cpp文件

#include<iostream>
#include<string>
using namespace std;
#include"MyArray.hpp"

void PrintIntArray(MyArray<int> & arr){
	for(int i=0;i<arr.getSize();i++){
		cout<<arr[i]<<" ";
	}
	cout<<endl<<"容量为:"<<arr.getCapacity()<<endl;
	cout<<"大小为:"<<arr.getSize()<<endl;
}

void test01(){
	MyArray<int> arr1(5);
	MyArray<int> arr2(arr1);
	MyArray<int> arr3(10);
	arr3 = arr2;

	for(int i=0;i<5;i++){
		arr1.Push_Back(i);
	}

	PrintIntArray(arr1);
	arr1.Pop_Back();
	cout<<"尾删除后"<<endl;
	PrintIntArray(arr1);
}


//测试自定义类型
class Person{
public:
	Person(){};
	Person(string name,int age){
		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;
	}
	cout<<endl<<"容量为:"<<arr.getCapacity()<<endl;
	cout<<"大小为:"<<arr.getSize()<<endl;
}

void test02(){
	MyArray<Person> arr(10);

	Person p1("Jack",10);
	Person p2("Bob",20);
	Person p3("Lucy",30);
	Person p4("Alan",30);
	Person p5("Tony",30);

	arr.Push_Back(p1);
	arr.Push_Back(p2);
	arr.Push_Back(p3);
	arr.Push_Back(p4);
	arr.Push_Back(p5);

	PrintPersonArray(arr);
}


int main(){
	test01();
	test02();
	system("pause");
	return 0;
}




 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值