C++ 类模板

文章详细介绍了C++中的类模板,包括如何创建和使用类模板,类模板的成员函数创建时机,类模板对象作为函数参数的情况,类模板遇到继承时的问题,类模板成员函数的类外实现,以及类模板的友元函数的类内外实现。还提供了一个实现通用数组类的案例,展示了如何处理内置类型和自定义类型的存储,以及拷贝构造函数和赋值运算符的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
using namespace std;

#include<string>
//类模板
template<class NameType,class AgeType>
class Person {
public:
	Person(NameType name, AgeType age) {
		this->name = name;
		this->age = age;
	}

	void showPerson() {
		cout << this->name << " , " << this->age << endl;
	}

	NameType name;
	AgeType age;
};

void test01() {
	Person<string, int> p("Tom", 11);
	p.showPerson();
}


int main() {

	test01();
	return -1;
}

2、类模板的创建时机

#include<iostream>
using namespace std;

//类模板的成员函数创建时机

class Person1 {
public:
	void showPerson1() {
		cout << "Person1" << endl;
	}
};

class Person2 {
public:
	void showPerson2() {
		cout << "Person2" << endl;
	}
};

template<class T>
class Myclass {
public:
	T obj;

	void func1() {
		obj.showPerson1();
	}

	void func2() {
		obj.showPerson2();
	}
	
};

void test01() {
	Myclass<Person1> m1;
	m1.func1();
	//m1.func2();
}

void test02() {
	Myclass<Person2> m2;
	//m2.func1();
	m2.func2();
}

int main() {
	test01();
	test02();

	system("pause");
	return -1;
}

3、类模板对象做函数参数 

#include<iostream>
using namespace std;

//类模板对象做函数参数
template<class T1, class T2>
class Person {

public:
	Person(T1 name, T2 age) {
		m_name = name;
		m_age = age;
	}

	void showPerson() {
		cout << "姓名: " << m_name << " 年龄: " << m_age << endl;
	}

	T1 m_name;
	T2 m_age;
};

void printPerson1(Person<string, int> &p) {
	p.showPerson();
}

//1、指定传入类型
void test01() {

	Person<string, int> p("孙悟空",100);
	printPerson1(p);


}
template<class T1, class T2>
void printPerson2(Person<T1, T2> &p) {
	p.showPerson();
	cout << "T1的类型为:" << typeid(T1).name() 
		<< "T2的类型为:" << typeid(T2).name() << endl;
}

//2、参数模板化
void test02() {
	
	Person<string, int> p("孙悟空", 100);
	
	printPerson2(p);

}
template<class T>
void printPerson3(T &p) {
	p.showPerson();
}

//3、整个类模板化
void test03() {
	Person<string, int> p("孙悟空", 100);
	printPerson3(p);
}

int main() {
	test01();
	test02();
	test03();


	system("pause");
	return -1;
}

4、类模板遇到继承遇到以下几点问题:

#include<iostream>
using namespace std;

//类模板遇到继承遇到以下几点问题:
//1、当子类继承的父类是一个类模板,子类在声明的时候,要指出父类中的T类型
//2、如果不指定,编译器无法给子类分配内存
//3、如果想灵活指定父类的T类型,子类也需要变为模板

template<class T>
class Base {
	T m;
};

class Son :public Base<int> {

};

void test01() {
	
	Son s1;
}

//如果想灵活指定父类的T类型,子类也需要变为模板

template<class T1, class T2>
class Son1 :public Base<T2> {
public:
	Son1() {
		cout << "T1类型为:" << typeid(T1).name() << endl; 
	}


	T1 obj;
};
void test02() {
	Son1<int, char> s1;
}

int main() {
	test02();
	return -1;
}

5、类模板成员函数类外实现

#include<iostream>
using namespace std;
#include<string>

//类模板成员函数类外实现
template<class T1,class T2>
class Person {
public:
	Person(T1 name, T2 age); /*{
		m_name = name;
		m_age = age;
	}*/

	void showPerson();/* {
		cout << "name: " << m_name << " age: " << m_age << endl;
	}*/

	T1 m_name;
	T2 m_age;
	
};
template<class T1,class T2>
Person<T1, T2>::Person(T1 name, T2 age) {
	m_name = name;
	m_age = age;
}

template<class T1, class T2>
void Person<T1, T2>::showPerson() {
	cout << "name: " << m_name << " age: " << m_age << endl;
}

void test01() {
	Person<string, int> p("Tom", 11);
	p.showPerson();
	
	
}

int main() {
	test01();
	return -1;
}

6、类模板的友元函数的类内类外实现

#include<iostream>
using namespace std;
#include<string>

template<class T1, class T2>
class Person;

template<class T1, class T2>
void printPerson1(Person<T1, T2> p) {
	cout << "name: " << p.m_name << " age: " << p.m_age << endl;
}

template<class T1, class T2>
class Person {
public:
	// 类内实现
	friend void printPerson(Person<T1, T2> p) {
		cout << "name: " << p.m_name << " age: " << p.m_age << endl;
	};

	//类外实现
	//加上一个模板空参,还得声明,让编译器知道

	friend void printPerson1<>(Person<T1, T2> p);

	Person(T1 name, T2 age) {
		this->m_name = name;
		this->m_age = age;
	}
	
	

private:
	T1 m_name;
	T2 m_age;
};




void test01() {
	Person<string, int> p("Tom", 11);
	printPerson(p);
}

void test02() {
	Person<string, int> p("Jack", 12);
	printPerson1(p);
}

int main() {
	test01();
	test02();
	return -1;
}

7、类模板的案例一

实现一个通用的数组类,要求如下:

  1. 可以对内置数据类型以及自定义数据类型的数据进行存储

  2. 将数组中的数据存储到堆区

  3. 构造函数中可以传入数组的容量

  4. 提供对应的拷贝构造函数以及operator=防止浅拷贝问题

  5. 提供尾插法和尾删法对数组中的数据进行增加和删除

  6. 可以通过下标的方式访问数组中的元素

  7. 可以获取数组中当前元素个数和数组的容量

main()

#include<iostream>
using namespace std;


#include "MyArray.hpp"
#include<string>

void test01() {
	
	MyArray<int> arr(5);
	
	MyArray<int> arr1(arr);

	MyArray<int> arr2(10);
	arr2 = arr1;

}

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

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

	for (int i = 0; i < 10; i++) {
		arr.push_Back(i);
	}

	printMyArray(arr);

	cout <<"数组容量: "<<arr.getCapacity()<<endl;

	cout << "元素数量: " << arr.getSize() << endl;

	MyArray<int> arr2(arr);
	
	cout << "arr2的打印输出为:" << endl;
	printMyArray(arr2);

	arr2.pop_Back();
	
	cout << "arr2的打印输出为:" << endl;
	printMyArray(arr2);
	
	cout << "数组容量: " << arr2.getCapacity() << endl;

	cout << "元素数量: " << arr2.getSize() << endl;

}
//测试自定义类
class Person {

public:
	Person(){}

	Person(string name, int age) {
		m_name = name;
		m_age = age;
	}

	string m_name;
	int m_age;
};

void printArray(MyArray<Person> &arr) {

	for (int i = 0; i < arr.getSize(); i++) {
		
		cout <<"姓名:" << arr[i].m_name << " 年龄:" << arr[i].m_age<<endl;
	}

}

void test03() {
	MyArray<Person> arr(10);
	
	Person p1("111", 11);
	Person p2("222", 22);
	Person p3("333", 33);
	Person p4("444", 44);
	Person p5("555", 55);
	
	arr.push_Back(p1);
	arr.push_Back(p2);
	arr.push_Back(p3);
	arr.push_Back(p4);
	arr.push_Back(p5);

	printArray(arr);

	cout << "数组容量: " << arr.getCapacity() << endl;

	cout << "元素数量: " << arr.getSize() << endl;
	

}

int main() {

	//test01();
	//test02();
	test03();
	return -1;
}

MyArray.hpp文件

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

template<class T>
class MyArray {
public:
	MyArray(int capacity) {

		cout << "MyArray 构造函数" << endl;
		this->m_capacity = capacity;
		this->m_size = 0;
		this->pAddress = new T[this->m_capacity];
		
	}

	MyArray(const MyArray &p) {

		cout << "MyArray 拷贝构造函数" << endl;
		this->m_capacity = p.m_capacity;
		this->m_size = p.m_size;
		
		this->pAddress = new T[this->m_capacity];

		for (int i = 0; i < this->m_size; i++) {
			this->pAddress[i] = p.pAddress[i];
		}

	}
	
	MyArray& operator=(const MyArray &p) {
		
		cout << "operator运算符 重载" << endl;
		if (this->pAddress != NULL) {
			delete[] this->pAddress;
			this->pAddress = NULL;
			this->m_capacity = 0;
			this->m_size = 0;
		}
		
		this->m_capacity = p.m_capacity;
		this->m_size = p.m_size;
		
		this->pAddress = new T[this->m_capacity];
		 
		for (int i = 0; i < this->m_capacity;i++) {
			this->pAddress[i] = p.pAddress[i];
		}
		
		return *this;
	}

	void push_Back(const T &val) {
		
		if (this->m_capacity <= this->m_size) {
			cout << "容量满足,不能再添加";
			return;
		}

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

	void pop_Back() {
		
		if (this->m_size == 0) {
			cout << "数组为空,不能删除" << endl;
			return;
		}
		
		this->m_size--;

	}
	//通过下标方式访问数组的元素
	T& operator[] (int index) {
		/*if (index >= 0 && index < this->m_size &&index < this->m_capacity) {
			
			return this->pAddress[index];
		}
		else {
			cout << "下标越界" << endl;
			return ;
		}*/

		return this->pAddress[index];

	}

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

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

	~MyArray() {

		cout << "MyArray 析构函数" << endl;
		if (this->pAddress != NULL) {
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}

	


private:
	T* pAddress; //指针指向堆区开辟的真实数组

	int m_capacity; // 数组容量

	int m_size; //数组大小


};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值