网站建设 推神网络,asp网站相册,四川建设厅官方网站九大员通知,wordpress阿里云主机名目录
类模板的使用方法
1.类模板语法
2.类模板和函数模板区别
3.类模板中成员函数创建时机
4.类函数对象做函数参数
5.类模板和继承
6.类模板成员函数类外实现
7.类模板分文件编写
person.hpp
实现cpp文件#xff1a;
8.类模板与友元
9.类模板案例
MyArray.hpp
…目录
类模板的使用方法
1.类模板语法
2.类模板和函数模板区别
3.类模板中成员函数创建时机
4.类函数对象做函数参数
5.类模板和继承
6.类模板成员函数类外实现
7.类模板分文件编写
person.hpp
实现cpp文件
8.类模板与友元
9.类模板案例
MyArray.hpp
在Cpp文件中展示方式 类模板的使用方法
1.类模板语法
类模板作用
建立一个通用类类中的成员 数据类型可以不具体制定用一个虚拟的类型来代表。语法: templatetypename T 类 解释:
template - 声明创建模板
typename - 表面其后面的符号是一种数据类型可以用class代替
T - 通用的数据类型名称可以替换通常为大写字母 #includeiostream using namespace std; #includestring templateclass NameType,class AgeType class Person { public: Person(NameType name,AgeType age) { this-m_name name; this-m_age age; } void ShowPerson() { cout name: m_name ,age: m_age endl; } NameType m_name; AgeType m_age; }; void test01() { Personstring, int p1(猴子, 500); p1.ShowPerson(); } //类模板和函数模板相似在声明模板template后面加类此类称为类模板 int main() { test01(); system(pause); return 0; } 2.类模板和函数模板区别
类模板与函数模板区别主要有两点:
1.类模板没有自动类型推导的使用方式
2.类模板在模板参数列表中可以有默认参数 #include iostream using namespace std; #includestring templateclass NameType,class AgeType int class Person { public: Person(NameType name, AgeType age) { this-m_name name; this-m_age age; } void PrintPeson() { cout name: m_name ,age: m_age endl; } NameType m_name; AgeType m_age; }; //1、类模板没有自动类型推导使用方式 void test01() { //Person p(八戒, 900); 错误无法用自动类型推导 Personstring, int p1(八戒, 900); p1.PrintPeson(); } //2、类模板在模板参数列表中可以有默认参数 void test02() { Personstring p1(八戒, 900); p1.PrintPeson(); } int main() { test02(); return 0; } 3.类模板中成员函数创建时机
类模板中成员函数和普通类中成员函数创建时机是有区别的
普通类中的成员函数一开始就可以创建
类模板中的成员函数在调用时才创建
#include iostream
using namespace std;class Person1
{
public:Person1(){}void showPerson1(){cout 调用的Person1 endl;}
};class Person2
{
public:void showPerson2(){cout 调用的Persona2 endl;}
};templateclass T
class MyClass
{
public:T obj;void fun1() { obj.showPerson1(); }void fun2() { obj.showPerson2(); }
};
//类模板中的成员函数并不是一开始就创建的在调用时才会去创建
void Test01()
{MyClassPerson1 m;m.fun1();// m.fun2();//编译会出错说明函数调用才会去创建成员函数
}
int main()
{Test01();
}
4.类函数对象做函数参数
类模板实例化出的对象向函数传参的方式
一共有三种传入方式:
1.指定传入的类型!——直接显示对象的数据类型
2.参数模板化——将对象中的参数变为模板进行传递
3.整个类模板化——将这个对象类型 模板化进行传递
#include iostream
using namespace std;
#includestringtemplateclass T1,class T2
class Person
{
public:Person(T1 name,T2 age){this-m_name name;this-m_age age;}void ShowPerson(){cout 姓名 this-m_name 年龄 this-m_age endl;}T1 m_name;T2 m_age;
};//1、指定传入类型
void printPerson1(Personstring,intp)
{p.ShowPerson();
}void test01()
{Personstring, int p1(高手, 2);printPerson1(p1);
}//2、参数模板化
templateclass T1,class T2
void printPerson2(PersonT1, T2 p)
{p.ShowPerson();cout T1 的类型为 typeid(T1).name() endl;cout T2 的类型为 typeid(T2).name() endl;
}void test02()
{Personstring, int p2(高高手, 2);printPerson2(p2);
}//3、整个类模板化
templateclass T
void printPerson3(T p)
{p.ShowPerson();cout T的数据类型为 typeid(T).name() endl;
}void test03()
{Personstring, int p3(高高高手, 5);printPerson3(p3);
}//通过类模板创建的对象向函数中进行传参使用比较广泛的是第一种指定传入的类型
int main()
{test03();
}
5.类模板和继承
当子类继承的父类是一个类模板时子类在声明的时候要指定出父类中T的类型
如果不指定编译器无法给子类分配内存
如果想灵活指定出父类中T的类型子类也需变为类模板
#include iostream
using namespace std;//类模板与继承
templateclass T
class Base
{T m;
};//class Son1 :Base 错误必须知道父类中的T类型才能继承给子类
class Son:Baseint
{};void test01()
{// Son S1;
}//如果想灵活指定父类中T类型子类也需要变类模板
templateclass T1,class T2
class Son2 :public BaseT2
{
public:Son2(){cout T1的类型为 typeid(T1).name() endl;cout T2的类型为 typeid(T2).name() endl;}T1 obj;
};void test02()
{Son2int,charS2;}
//总结如果父类是类模板子类需要指定出父类中T的数据类型
int main()
{test02();
}6.类模板成员函数类外实现
#include iostream
#includestring
using namespace std;//类模板成员函数类外实现
templateclass T1,class T2
class Person
{
public:Person(T1 name, T2 age);/* {this-m_name name;this-m_age age;}*/void showPerson();/* {cout 姓名 this.m_name ,年龄 this.m_age endl;}*/T1 m_name;T2 m_age;
};//构造函数类外实现
templateclass T1,class T2
PersonT1, T2::Person(T1 name, T2 age)
{this-m_name name;this-m_age age;
}//模板成员函数类外实现
templateclass T1,class T2
void PersonT1,T2::showPerson()
{cout 姓名 this-m_name ,年龄 this-m_age endl;
}void test01()
{Personstring,intp1(猴子,200);p1.showPerson();
}//总结类模板中成员函数类外实现时需要加上模板参数列表
int main()
{test01();
}
7.类模板分文件编写
类模板中成员函数创建时机是在调用阶段导致分文件编写时链接不到
解决方式1直接包含.cpp源文件
解决方式2将声明和实现写到同一个文件中并更改后缀名为.hpphpp是约定的名称并不是强制
person.hpp
#pragma once
#includeiostream
#includestring
using namespace std;
templateclass T1, class T2
class Person
{
public:Person(T1 name, T2 age);void showPerson();T1 m_name;T2 m_age;
};templateclass T1, class T2
PersonT1, T2::Person(T1 name, T2 age)
{this-m_name name;this-m_age age;
}templateclass T1, class T2
void PersonT1, T2::showPerson()
{cout 姓名 this-m_name ,年龄 this-m_age endl;
}
实现cpp文件
#include iostream
#includestring
using namespace std;//类模板分文件编写问题以及解决//第一种解决方法,直接包含源文件
//#includeperson.cpp//第二种解决方法将.h和.cpp中的内容写到一起将后缀名改为.hpp文件
#includeperson.hppvoid test01()
{Personstring, int p1(沙僧, 200);p1.showPerson();
}
// 总结主流的解决方式是第二种将类模板成员函数写到一起并将后缀名改为.hpp
int main()
{test01();
}8.类模板与友元
全局函数类内实现——直接在类内声明友元即可
全局函数类外实现——需要提前让编译器知道全局函数的存在
#include iostream
#includestring
using namespace std;templateclass T1,class T2
class Person;
//全局函数类外实现
templateclass t1, class t2
void printPerson2(Persont1, t2 p)
{cout 类外实现——姓名 p.m_Name 类外实现——年龄 p.m_Age endl;
}templateclass T1,class T2
class Person
{//全局函数 类内实现friend void printPerson(PersonT1, T2 p){cout 姓名 p.m_Name 年龄 p.m_Age endl;}//全局函数 类外实现//加空模板参数列表//如果全局函数 是类外实现需要让编译器提前知道这个函数的存在friend void printPerson2(PersonT1, T2);public:Person(T1 name, T2 age){this-m_Name name;this-m_Age age;}
private:T1 m_Name;T2 m_Age;
};//1.全局函数在类内实现
void test01()
{Personstring, intp(僧人, 21);printPerson(p);
}
//2.全局函数类外实现
void test02()
{Personstring, intp(僧人2, 22);printPerson2(p);
}
//总结建议全局函数做类内实现用法简单而且编译器可以直接识别int main()
{test02();
}9.类模板案例
具体实现功能
1.可以对内置数据类型以及自定义数据类型的数据进行存储 2.将数组中的数据存储到堆区 3.构造函数中可以传入数组的容量 4.提供对应的拷贝构造函数以及operator 防止浅拷贝问题 5.提供尾插法和尾删法对数组中的数据进行增加和删除 6.可以通过下标的方式访问数组中的元素 7.可以获取数组中当前元素个数和数组的容量
MyArray.hpp
//自己的通用的数组类
#pragma once
#includeiostream
using namespace std;/*1.可以对内置数据类型以及自定义数据类型的数据进行存储
2.将数组中的数据存储到堆区
3.构造函数中可以传入数组的容量
4.提供对应的拷贝构造函数以及operator 防止浅拷贝问题
5.提供尾插法和尾删法对数组中的数据进行增加和删除
6.可以通过下标的方式访问数组中的元素
7.可以获取数组中当前元素个数和数组的容量*/
templateclass 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 arr){//cout MyArray的拷贝构造调用 endl;this-m_Capacity arr.m_Capacity;this-m_Size arr.m_Size;//this-pAddress arr.pAddress; 浅拷贝会导致堆区的值冲突//深拷贝this-pAddress new T[m_Capacity];//将arr中的数据都拷贝过来for (int i 0; i arr.m_Size; i){this-pAddress[i] arr.pAddress[i];}}//operator 防止浅拷贝问题 abcMyArray operator(const MyArray arr){//cout MyArray的 operator 函数调用 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 i0;iarr.m_Size;i){this-pAddress[i] arr.pAddress[i];}return *this;}//尾插法void Push_Back(const T val){//判断容量是否等于大小if (this-m_Capacitythis-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]100T operator[](int index){return this-pAddress[index];}//返回数组容量int getCapacity(){return this-m_Capacity;}//返回数组大小int getSize(){return this-m_Size;}//析构函数~MyArray(){if (this-pAddress!NULL){//cout MyArray的析构函数调用 endl;delete[] this-pAddress;this-pAddress NULL;}}private:T* pAddress;//指针指向堆区开辟的真实数组int m_Capacity;//数组容量int m_Size;//数组大小
};
在Cpp文件中展示方式
#include iostream
using namespace std;
#includeMyArray.hpp
#includestring
void printArray(MyArrayint arr)
{for (int i 0; i arr.getSize(); i){cout arr[i] endl;}
}void Test01()
{MyArrayint arr1(5);//有参构造for (int i 0; i 5; i){//利用尾插法向数组中插入数据arr1.Push_Back(i);}cout arr1的打印输出为: endl;printArray(arr1);cout arr1的容量为: arr1.getCapacity() endl;cout arr1的大小为: arr1.getSize() endl;MyArrayint arr2(arr1); //深拷贝cout arr2的打印输出为: endl;printArray(arr2);arr2.Pop_Back();cout arr2尾删后: endl;cout arr2的容量为: arr2.getCapacity() endl;cout arr2的大小为: arr2.getSize() endl;// MyArrayintarr3(100);//有参构造// arr3 arr1; //operator 调用
}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(MyArrayPerson arr)
{for (int i 0; i arr.getSize(); i){cout 姓名 arr[i].m_Name ,年龄: arr[i].m_Age endl;}
}
void Test02()
{MyArrayPerson arr(10);Person p1(开天, 1);Person p2(裂地, 2);Person p3(劈天, 3);Person p4(裂天, 4);Person p5(封天, 5);Person p6(震地, 6);//将数据插入到数组中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 arr容量为 arr.getCapacity() endl;//输出大小cout arr大小为 arr.getSize() endl;}
int main()
{Test02();
}//总结能够利用所学知识点实现通用的数组 文章转载自: http://www.morning.yymlk.cn.gov.cn.yymlk.cn http://www.morning.mlbdr.cn.gov.cn.mlbdr.cn http://www.morning.qqpg.cn.gov.cn.qqpg.cn http://www.morning.snzgg.cn.gov.cn.snzgg.cn http://www.morning.xcjwm.cn.gov.cn.xcjwm.cn http://www.morning.xfyjn.cn.gov.cn.xfyjn.cn http://www.morning.dwxqf.cn.gov.cn.dwxqf.cn http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.nqmwk.cn.gov.cn.nqmwk.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.lhygbh.com.gov.cn.lhygbh.com http://www.morning.rqxhp.cn.gov.cn.rqxhp.cn http://www.morning.krhkb.cn.gov.cn.krhkb.cn http://www.morning.elbae.cn.gov.cn.elbae.cn http://www.morning.ryfq.cn.gov.cn.ryfq.cn http://www.morning.xckdn.cn.gov.cn.xckdn.cn http://www.morning.qwbtr.cn.gov.cn.qwbtr.cn http://www.morning.rxkq.cn.gov.cn.rxkq.cn http://www.morning.tyklz.cn.gov.cn.tyklz.cn http://www.morning.bnylg.cn.gov.cn.bnylg.cn http://www.morning.rfyk.cn.gov.cn.rfyk.cn http://www.morning.gdljq.cn.gov.cn.gdljq.cn http://www.morning.xkyqq.cn.gov.cn.xkyqq.cn http://www.morning.tdhxp.cn.gov.cn.tdhxp.cn http://www.morning.trhrk.cn.gov.cn.trhrk.cn http://www.morning.brwwr.cn.gov.cn.brwwr.cn http://www.morning.skmpj.cn.gov.cn.skmpj.cn http://www.morning.sgnjg.cn.gov.cn.sgnjg.cn http://www.morning.dkbsq.cn.gov.cn.dkbsq.cn http://www.morning.whothehellami.com.gov.cn.whothehellami.com http://www.morning.taojava.cn.gov.cn.taojava.cn http://www.morning.pwgzh.cn.gov.cn.pwgzh.cn http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.yqqgp.cn.gov.cn.yqqgp.cn http://www.morning.mxnfh.cn.gov.cn.mxnfh.cn http://www.morning.tllhz.cn.gov.cn.tllhz.cn http://www.morning.tpnxr.cn.gov.cn.tpnxr.cn http://www.morning.ymqfx.cn.gov.cn.ymqfx.cn http://www.morning.elbae.cn.gov.cn.elbae.cn http://www.morning.mwnch.cn.gov.cn.mwnch.cn http://www.morning.fqhbt.cn.gov.cn.fqhbt.cn http://www.morning.knmby.cn.gov.cn.knmby.cn http://www.morning.pfnwt.cn.gov.cn.pfnwt.cn http://www.morning.mmplj.cn.gov.cn.mmplj.cn http://www.morning.tqqfj.cn.gov.cn.tqqfj.cn http://www.morning.mpscg.cn.gov.cn.mpscg.cn http://www.morning.qfths.cn.gov.cn.qfths.cn http://www.morning.c7617.cn.gov.cn.c7617.cn http://www.morning.hrnrx.cn.gov.cn.hrnrx.cn http://www.morning.nrll.cn.gov.cn.nrll.cn http://www.morning.ypjjh.cn.gov.cn.ypjjh.cn http://www.morning.dxqfh.cn.gov.cn.dxqfh.cn http://www.morning.rdmz.cn.gov.cn.rdmz.cn http://www.morning.hyryq.cn.gov.cn.hyryq.cn http://www.morning.dbdmr.cn.gov.cn.dbdmr.cn http://www.morning.bfrsr.cn.gov.cn.bfrsr.cn http://www.morning.pqyms.cn.gov.cn.pqyms.cn http://www.morning.lwhsp.cn.gov.cn.lwhsp.cn http://www.morning.rtsd.cn.gov.cn.rtsd.cn http://www.morning.tytly.cn.gov.cn.tytly.cn http://www.morning.rbgqn.cn.gov.cn.rbgqn.cn http://www.morning.qhmhz.cn.gov.cn.qhmhz.cn http://www.morning.cfynn.cn.gov.cn.cfynn.cn http://www.morning.fblkr.cn.gov.cn.fblkr.cn http://www.morning.hfytgp.cn.gov.cn.hfytgp.cn http://www.morning.qytby.cn.gov.cn.qytby.cn http://www.morning.gqtxz.cn.gov.cn.gqtxz.cn http://www.morning.rqbkc.cn.gov.cn.rqbkc.cn http://www.morning.pshpx.cn.gov.cn.pshpx.cn http://www.morning.hptbp.cn.gov.cn.hptbp.cn http://www.morning.wkkqw.cn.gov.cn.wkkqw.cn http://www.morning.smhtg.cn.gov.cn.smhtg.cn http://www.morning.xxrwp.cn.gov.cn.xxrwp.cn http://www.morning.dtmjn.cn.gov.cn.dtmjn.cn http://www.morning.wnqbf.cn.gov.cn.wnqbf.cn http://www.morning.khclr.cn.gov.cn.khclr.cn http://www.morning.tcsdlbt.cn.gov.cn.tcsdlbt.cn http://www.morning.wfykn.cn.gov.cn.wfykn.cn http://www.morning.jzykq.cn.gov.cn.jzykq.cn http://www.morning.pjftk.cn.gov.cn.pjftk.cn