IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++_类模板 -> 正文阅读

[C++知识库]C++_类模板

学习目标:

1.类模板语法
2.类模板和函数模板的区别
3.类模板中的成员函数创建时机
4.类模板对象做函数参数
5.类模板与继承
6.类模板成员函数类外实现
7.类模板分文件编写
8.类模板与友元
9.类模板案例

学习内容:

**

1).类模板语法代码

**

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

//类模板
template<class NameType,class AgeType>
class Person
{
public:
	Person(NameType name,AgeType age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	NameType m_Name;
	AgeType m_Age;
	void showPerson()
	{
		cout << "name: " << this->m_Name << " age:" << this->m_Age << endl;
	}
};
void test01()
{
	Person<string, int> p1("孙悟空", 999);
	p1.showPerson();
}
void test02()
{

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

**

2).类模板和函数模板的区别

**

#include<iostream>
#include<string>

using namespace std;

//类模板与函数模板区别
template<class NameType, class AgeType=int>
class Person
{
public:
	Person(NameType name, AgeType age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	void showPerson()
	{
		cout << this->m_Name << this->m_Age << endl;
	}
	NameType m_Name;
	AgeType m_Age;
};
//1.类模板没有自动类型推导使用方式
void test01()
{
	//Person p1("孙悟空", 18); 错误的
	Person<string, int> p1("孙悟空", 18);//正确的
	p1.showPerson();
}
//2.类模板在模板参数列表中可以有默认参数
void test02()
{
	Person<string> p2("猪八戒", 999);
	p2.showPerson();
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

**

3).类模板中的成员函数创建时机

**

#include<iostream>
#include<string>

using namespace std;

//类模板中成员函数创建时机
//类模板中成员函数在调用时才创建
class Person1
{
public:
	void showPerson1()
	{
		cout << "Person1 show" << endl;
	}
};

class Person2
{
public:
	void showPerson2()
	{
		cout << "Person2 show" << endl;
	}
};
template<class T>
class MyClass
{
public:
	T obj;
	//类模板中的成员函数
	void func1()
	{
		obj.showPerson1();
	}
	void func2()
	{
		obj.showPerson2();
	}
};

void test01()
{
	MyClass<Person1> m;
	m.func1();		//函数调用才会去创建成员函数
}

void test02()
{
	MyClass<Person2> m;
	m.func2();
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

**

4).类模板对象做函数参数

**

#include<iostream>
#include<string>

using namespace std;

//类模板对象做函数参数

template<class 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(Person<string, int>& p)
{
	p.showPerson();
}
void test01()
{
	Person<string, int> p("张三", 25);
	printPerson1(p);
}
//2.参数模块化
template<class T1,class T2>
void printPerson2(Person<T1,T2> &p)
{
	p.showPerson();
}
void test02()
{
	Person<string, int> p("李四", 32);
	printPerson2(p);
}
//3.整个类模块化
template<class T>
void printPerson3(T &p)
{
	p.showPerson();
}
void test03()
{
	Person<string, int> p("王五", 35);
	printPerson3(p);
}
int main()
{
	test01();
	test02();
	test03();
	system("pause");
	return 0;
}

**

5).类模板与继承

**

#include<iostream>
#include<string>

using namespace std;

//类模板与继承
template<class T>
class Base
{
public:

	T m;

};

//class Son:public Base//错误的
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()
{
	test01();
	test02();

	system("pause");
	return 0;
}

**

6).类模板成员函数的类外实现

**

#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_Name = name;
	this->m_Age = age;
}
//成员函数的类外实现
template<class T1,class T2>
void Person<T1,T2>::showPerson()
{
	cout << this->m_Name << this->m_Age << endl;
}
void test01()
{
	Person<string, int> p("张三", 18);
	p.showPerson();
}

int main()
{
	test01();

	system("pause");
	return 0;
}

学习时间:

提示:这里可以添加计划学习的时间

例如:

  • 周一至周五晚上 7 点—晚上9点
  • 周六上午 9 点-上午 11 点
  • 周日下午 3 点-下午 6 点

**

7).类模板分文件编写

**

//Person.h
#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;
};
//Person.cpp
#include"Person.h"
//构造函数的类外实现
template<class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
	this->m_Name = name;
	this->m_Age = age;
}
//成员函数的类外实现
template<class T1, class T2>
void Person<T1, T2>::showPerson()
{
	cout << this->m_Name << this->m_Age << endl;
}
//main()函数
#include "Person.cpp"//第一种解决方式直接包含源文件

//第二种解决方式:将.h和.cpp中的内容写到一起,将后缀名改为.hpp文件,主流采用这种


void test01()
{
	Person<string, int> p("张三", 18);
	p.showPerson();//类模板中的成员函数创建时机是在调用阶段,导致分文件编写时链接不到
}

int main()
{
	test01();

	system("pause");
	return 0;
}

**

8).类模板与友元

**

#include<iostream>
#include<string>

using namespace std;

//通过全员函数打印Person信息
// 
//提前让编译器知道Person类存在
template<class T1, class T2>
class Person;
//类外实现
template<class T1, class T2>
void printPerson2(Person<T1, T2> p)
{
	cout << p.m_Name << p.m_Age << endl;
}
template<class T1,class T2>
class Person
{
	//全局函数 类内实现
	friend void printPerson(Person<T1,T2> p)
	{
		cout << p.m_Name << p.m_Age << endl;
	}
	//全局函数 类外实现
	//加空模板参数列表
	//如果全局函数是类外实现,需要让编译器提前知道这个函数的存在
	friend void printPerson2<>(Person<T1,T2> p);

public:
	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("张三",19);
	printPerson(p);

}
void test02()
{
	Person<string, int> p("李四", 23);
	printPerson2(p);
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

**

9).类模板案例

**

学习产出:

提示:这里统计学习计划的总量

例如:

  • 技术笔记 2 遍
  • CSDN 技术博客 3 篇
  • 习的 vlog 视频 1 个
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-09-30 00:33:07  更:2022-09-30 00:36:26 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 7:06:48-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码