1.3.5 类模板与继承
当类模板碰到继承时,需要注意以下几点:
》当子类继承父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型
》如果不指定,编译器无法给子类分配内存
》如果想灵活指定出父类中的T类型,子类也需变为类模板
示例:
//类模板与继承
#include <iostream>
using namespace std;
template<class T>
class Base
{
public:
T m;
};
//class Son :public Base //错误,必须要知道父类中的T类型,才能继承
class Son:public Base<int>
{
};
void test01()
{
Son s1;
}
//如果想灵活指定父类中T类型,子类也变成类模板
template<class T1, class T2>
class Son2 :public Base<T2>
{
public:
Son2()
{
cout << "T1的类型为: " << typeid(T1).name() << endl;
cout << "T2的类型为: " << typeid(T2).name() << endl;
}
T1 obj;
};
void test02()
{
Son2<int, char>s2;
}
int main(void)
{
test01();
test02();
system("pause");
return 0;
}
1.3.6 类模板成员函数的类外实现
类模板中成员函数类外实现时,需要加上模板参数列表
示例:
//类模板成员函数类外实现
#include <iostream>
#include <string>
using namespace std;
template<class T1, class T2>
class Person
{
public:
Person(T1 name, T2 age);
/*{
this->m_Age = age;
this->m_Name = name;
}*/
void showPerson();
/*{
cout << "姓名: " << this->m_Name << " "
<< "年龄: " << this->m_Age << endl;
}*/
T1 m_Name;
T2 m_Age;
};
//构造函数类外实现
template<class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
this->m_Age = age;
this->m_Name = name;
}
//成员函数的类外实现
template<class T1, class T2>
void Person<T1,T2>::showPerson()
{
cout << "姓名: " << this->m_Name << " "
<< "年龄: " << this->m_Age << endl;
}
void test()
{
Person<string, int> s1("老王", 35);
s1.showPerson();
}
int main(void)
{
test();
system("pause");
return 0;
}
1.3.7 类模板分文件编写
问题:
类模板中成员函数创建时机是在调用时才创建,导致分文件编写时链接不到
解决:
》解决方式1:直接包含.cpp源文件
》解决方式2:将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制
示例:
-----------------------
person.hpp:
-----------------------
#pragma once
#include <iostream>
#include <string>
using namespace std;
template<class T1, class T2>
class Person
{
public:
Person(T1 name, T2 age);
void showPerson();
T1 m_Name;
T2 m_Age;
};
//构造函数类外实现
template<class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
this->m_Age = age;
this->m_Name = name;
}
//成员函数的类外实现
template<class T1, class T2>
void Person<T1, T2>::showPerson()
{
cout << "姓名: " << this->m_Name << " "
<< "年龄: " << this->m_Age << endl;
}
------------------------------------
main.cpp
------------------------------------
//类模板分文件编写问题及解决
#include <iostream>
//解决方法一,直接包含源文件
//#include "person.cpp"
//第二种解决方式,将.h和.cpp中的内容写到一起,将后缀改为.hpp
#include "person.hpp"
using namespace std;
void test()
{
Person<string, int>p("老五", 25);
p.showPerson();
}
int main(void)
{
test();
system("pause");
return 0;
}
1.3.8 类模板与友元
掌握类模板配合友元函数的类内和类外实现
》全局函数类内实现--直接在类内声明友元即可
》全局函数类外实现--需要提前让编译器知道全局函数的存在
总结:建议全局函数类内实现,用法简单,而且编译器可以直接识别
示例:
#include <iostream>
#include <string>
using namespace std;
//提前让编译器知道
template<class T1, class T2>
class Person;
//通过全局函数打印Person信息
//全局函数类外实现
template<class T1, class T2>
void PrintPerson2(Person<T1, T2> p)
{
cout << "类外实现" << endl;
cout << "姓名:" << p.m_Name << " " << "年龄: " << p.m_Age << endl;
}
template<class T1, class T2>
class Person
{
public:
Person(T1 name, T2 age)
{
this->m_Name = name;
this->m_Age = age;
}
//全局函数类内实现
friend void PrintPerson(Person<T1, T2> p)
{
cout << "姓名:" << p.m_Name << " " << "年龄: " << p.m_Age << endl;
}
//全局函数类外实现 加空模板列表
//提前让编译器知道
friend void PrintPerson2<>(Person<T1, T2> p);
private:
T1 m_Name;
T2 m_Age;
};
//全局函数类内实现
void test01()
{
Person<string, int>p("老王", 30);
PrintPerson(p);
Person<string, int>p1("小李", 20);
PrintPerson2(p1);
}
int main(void)
{
test01();
system("pause");
return 0;
}
1.3.9 类模板案例
实现一个通用的数组类,要求如下:
》可以对内置数据类型以及自定义数据类型的数据进行存储
》将数组中的数据存储到堆区
》构造函数中可以传入数组的容量
》提供对应的拷贝构造函数以及operator=防止浅拷贝问题
》提供尾插法和尾删法对数组中的数据进行增加和删除
》可以通过下标的方式访问数组中的元素
》可以获取数组中当前元素个数和数组的容量
示例:
-------------
MyArray.hpp
-------------
//自己的通用数组类
#pragma once
#include <iostream>
using namespace std;
template<class T>
class MyArray
{
public:
//有参构造 参数 容量
MyArray(int capacity)
{
this->m_Capacity = capacity;
this->m_Size = 0;
this->pAddress = new T[this->m_Capacity];
}
//析构函数
~MyArray()
{
if (this->pAddress != NULL)
{
delete[] this->pAddress;
pAddress = NULL;
}
}
//拷贝构造
MyArray(const MyArray &arr)
{
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
//深拷贝
this->pAddress = new T[m_Capacity];
//将arr中的数据都拷贝过来
for (int i=0; i<this->m_Size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
}
//oprtator=防止浅拷贝问题
MyArray &operator=(const MyArray &arr)
{
//判断原来堆区是否有数据,如果有先释放
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 )
{
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)
{
return this->pAddress[index];
}
//返回数组的容量
int getCapacity()
{
return this->m_Capacity;
}
//返回数组的大小
int getSize()
{
return this->m_Size;
}
private:
T *pAddress; //指针指向堆区开辟的真实数组
int m_Capacity; //数组的容量
int m_Size; //数组的大小
};
-------------
MyArray.c
-------------
#include <iostream>
using namespace std;
#include <string>
#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 << "arr1的打印输出为:" << endl;
PrintIntArray(arr2);
arr2.Pop_Back();
cout << "arr2尾删后的打印输出为:" << endl;
PrintIntArray(arr2);
cout << "arr1的容量为:" << arr2.getCapacity() << endl;
cout << "arr1的大小为:" << arr2.getSize() << endl;
}
//测试自定义的数据类型
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;
}
}
void test02()
{
MyArray<Person>arr(10);
Person p1("孙悟空", 999);
Person p2("猪八戒", 900);
Person p3("唐僧", 29);
Person p4("红孩儿", 600);
Person p5("哪吒", 888);
Person p6("土行孙", 666);
arr.Push_Back(p1);
arr.Push_Back(p2);
arr.Push_Back(p3);
arr.Push_Back(p4);
arr.Push_Back(p5);
arr.Push_Back(p6);
//打印数组
PrintPersonArray(arr);
//输出容量和大小
cout << "arr1的容量为:" << arr.getCapacity() << endl;
cout << "arr1的大小为:" << arr.getSize() << endl;
}
int main(void)
{
//test01();
test02();
system("pause");
return 0;
}