c++------类模板

目录

一、类模板基本语法

二、类模板中成员函数的创建时机

三、类模板做函数参数

四、类模板碰到继承问题

五、类模板中的成员函数类外实现

六、类模板碰到友元问题


一、类模板基本语法

#define _CRT_SECURE_NO_WARNINTGS
#include <iostream>
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;
};

void test01()
{
	//类模板和函数模板的区别
	//1、类模板不可以使用自动类型推导	只能用显示指定类型
	//2、类模板中可以有默认参数
	Person<string> p("Tom", 20);
	p.ShowPerson();
}

int main()
{
	test01();

	system("pause");
	return EXIT_SUCCESS;
}

二、类模板中成员函数的创建时机

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

class Person1 {
public:
	void P1()
	{
		cout << "Person1函数调用" << endl;
	}
};
class Person2 {
public:
	void P2()
	{
		cout << "Person2函数调用" << endl;
	}
};
//类模板中的成员函数	并不是一开始就创建的	而是在运行阶段确定出T的类型才创建的
template<class T>
class MyClass {
public:
	void func1()
	{
		this->obj.P1();
	}
	void func2()
	{
		this->obj.P2();
	}

	T obj;
};

void test01()
{
	MyClass<Person2> m;
	//m.func1();
	m.func2();
}
int main()
{
	test01();

	system("pause");
	return EXIT_SUCCESS;
}

三、类模板做函数参数

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>  
using namespace std;

template<typename T1,typename T2>
class Person {
public:
    Person(T1 name, T2 age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
    void ShowPerson()
    {
        cout << "姓名" << this->m_Name << endl;
        cout << "年龄" << this->m_Age << endl;
    }

    T1 m_Name;
    T2 m_Age;
};

//1、指定类型
void DoWork01(Person<string, int>& p)
{
    p.ShowPerson();
}
void test01()
{
    Person<string, int> p("孙悟空", 80);
    DoWork01(p);
}
//2、参数模板化
template<class T1,class T2>
void DoWork02(Person<T1, T2>& p)
{
    cout << "T1的类型" << typeid(T1).name() << endl;
    cout << "T2的类型" << typeid(T2).name() << endl;
    p.ShowPerson();
}
void test02()
{
    Person<string, int> p("猪八戒", 90);
    DoWork02(p);
}
//3、整个类 模板化
template<class T>
void DoWork03(T p)
{
    cout << "T的类型" << typeid(T).name() << endl;
    p.ShowPerson();
}
void test03()
{
    Person<string, int> p("沙和尚", 100);
    DoWork03(p);
}
int main()
{
    //test01();
    //test02();
    test03();

    system("pause");
    return EXIT_SUCCESS;

}

四、类模板碰到继承问题

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

template <class T>
class Base {
public:
	void ShowT()
	{
		cout << typeid(T).name() << endl;
	}

	T m_A;
};
//1、指定模板参数类型
class Son : public Base <int>{
	
};

//2、
template <class T1, class T2>
class Son2 : public Base <T2>{
public:
	Son2()
	{
		cout << typeid(T1).name() << endl;
		cout << typeid(T2).name() << endl;
	}

	T1 m_B;
};

void test01()
{
	Son2 <int, double> s;
	s.ShowT();
}

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

五、类模板中的成员函数类外实现

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
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;
};

template<class T3, class T4>
Person<T3, T4>::Person(T3 name, T4 age)
{
	this->m_Name = name;
	this->m_Age = age;
}

template<class T5, class T6>
void Person<T5, T6>::ShowPerson()
{
	{
		cout << "姓名:" << this->m_Name << " 年龄:" << this->m_Age << endl;
	}
}

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

int main()
{
	test01();

	system("pause");
	return EXIT_SUCCESS;
}

六、类模板碰到友元问题

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>  
using namespace std;

template <class T1, class T2>
class Person;

template <class T1, class T2>
void ShowPerson2(Person<T1, T2>& p)
{
    cout << "类外实现:" << endl;
    cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
}

template <class T1, class T2>
class Person {
    //1、友元函数类内实现
    friend void ShowPerson(Person<T1, T2>& p)
    {
        cout << "类内实现:" << endl;
        cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
    }
    //2、友元函数类外实现
    friend void ShowPerson2<>(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("Tom", 18);
    ShowPerson(p);
    ShowPerson2(p);
}

int main()
{
    test01();

    system("pause");
    return EXIT_SUCCESS;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值