类模板基础知识(2)

知识框图
类模板基础知识(2)

1.类模板与继承

//需要解决的问题:当继承的父类是一个类模板时如何正确调用?
//1.当继承的父类是类模板时,必须指定父类中的类型。
//2.指定方式一:直接指定一个确定数据类型。
//3.指定方式二:将子类也写成类模板,均采用模板方式处理数据类型

#include<iostream>
using namespace std ;
#include<string>
template<class T>
class Father{
public:
	T m1;
};

//class son:public Father{};			//若无指定,会显示Father类缺少参数列表。

//方式一:
class son1:public Father<int>{           //在继承的时候就指定int类型。
public:
	int m2;
};

//方式二:
template<class T1,class T2>				 //在继承之前将子类也进行模板化。
class son2:public Father<T1>{          
public:
	T2 m2;
};

void test(){

	son1 s1;							//不同指定方式对应不同实例化对象方法。
	son2<int,int> s2;		

}

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

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

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

template<class nametype ,class agetype >
class Person{
public:
	Person(nametype name,agetype age );

	nametype m_name;
	agetype m_age;

	void showname();

	void showage();

};
//类外实现方式
template<class nametype ,class agetype >                        //需声明类模板
Person<nametype,agetype>::Person(nametype name,agetype age){    //在指定作用域下,应写上类模板参数列表即<nametype,agetype>
		this->m_age = age;
		this->m_name = name;
	}

template<class nametype ,class agetype >
void Person<nametype,agetype>::showname(){
		cout<<"姓名:"<<this->m_name<<endl;
	}

template<class nametype ,class agetype >
void Person<nametype,agetype>::showage(){
	cout<<"年龄:"<<this->m_age<<endl;
	}

void test01(){
	Person<string,int> p1("八号程序员",22);
	p1.showname();
	p1.showage();
}


int main () {

	test01();

	system("pause");
		return 0;
}

3.类模板分文件编写

//.hpp下code.
#pragma once
#include<iostream>
using namespace std ;
#include<string>


template<class nametype ,class agetype >
class Person{
public:
	Person(nametype name,agetype age );

	nametype m_name;
	agetype m_age;

	void showname();

	void showage();

};
//类外实现方式
template<class nametype ,class agetype >                        //需声明类模板
Person<nametype,agetype>::Person(nametype name,agetype age){    //在指定作用域下,应写上类模板参数列表即<nametype,agetype>
		this->m_age = age;
		this->m_name = name;
	}

template<class nametype ,class agetype >
void Person<nametype,agetype>::showname(){
		cout<<"姓名:"<<this->m_name<<endl;
	}

template<class nametype ,class agetype >
void Person<nametype,agetype>::showage(){
	cout<<"年龄:"<<this->m_age<<endl;
	}
//在主程序段code。
//常规分文件编写原则:声明留在头文件(.h),实现放在源文件(.cpp)
//类模板因为成员函数创建时机的问题,通常需要将常规的(.h)(.cpp)写到一个文件下,通常记为(.hpp)

#include<iostream>
using namespace std ;
#include<string>
#include"Person.hpp"		//Person.hpp为上述文件的名称。

void test01(){
	Person<string,int> p1("八号程序员",22);
	p1.showname();
	p1.showage();
}


int main () {

	test01();

	system("pause");
		return 0;
}

4.类模板与友元

//全局函数类内实现 - 直接在类内声明友元即可
//全局函数类外实现 - 需要提前让编译器知道全局函数的存在

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

//类外实现全局函数做友元,需提前声明。
//1.提前声明类模板
template<class nametype ,class agetype >class Person;
//2.提前做函数实现
template<class nametype ,class agetype >
void showperson2(Person<nametype,agetype>& p){
	cout<<"类外实现——姓名:"<<p.m_name<<" 年龄:"<<p.m_age<<endl;
	}


template<class nametype ,class agetype >
class Person{
	//类内实现全局函数做友元
	friend void showperson(Person<nametype,agetype>& p){
		cout<<"类内实现——姓名:"<<p.m_name<<" 年龄:"<<p.m_age<<endl;
	}
	//类内声明,类外实现。
	friend void showperson2<>(Person<nametype,agetype>& p);			//注意<>,不要遗漏。

public:
	Person(nametype name,agetype age );

private:
	nametype m_name;
	agetype m_age;
};

template<class nametype ,class agetype >                        //类外实现构造函数
Person<nametype,agetype>::Person(nametype name,agetype age){    //在指定作用域下,应写上类模板参数列表即<nametype,agetype>
		this->m_age = age;
		this->m_name = name;
	}

void test01(){
	Person<string,int> p1("八号程序员",22);
	showperson(p1);
	showperson2(p1);
}

int main () {

	test01();

	system("pause");
		return 0;
}
//推荐:类内实现。
//简单的方法,它不香吗?

本文是根据B站内容:《黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难》:https://www.bilibili.com/video/BV1et411b73Z?p=314 学习整理笔记。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值