作用
函数分文件编写一般有4个步骤
- 1.创建后缀名为.h的头文件,放在头文件的文件夹,swap.h
- 2.创建后缀名为.cpp的源文件,放在源文件的文件夹中,swap.cpp
- 3.在头文件中写函数的声明,swap.h
- 4.在源文件中写函数的定义,swap.cpp
- #include "swap.h"
- void swap(int a,int b) { }
- 5.使用
- #include "swap.h"
- swap就可以使用了
类模板分文件编写
- 将类模板成员函数写到一起放在头文件文件夹,并将后缀名改为.hpp
- person.hpp中代码
#pragma once
#include <iostream>
using namespace std;
#include <string>
template<class T1, class T2>
class Person {
public:
Person(T1 name, T2 age);
void showPerson();
public:
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;
}
#include "person.hpp" 引用