学习目标:
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;
};
void test01()
{
Person<string, int> p1("孙悟空", 18);
p1.showPerson();
}
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;
};
void printPerson1(Person<string, int>& p)
{
p.showPerson();
}
void test01()
{
Person<string, int> p("张三", 25);
printPerson1(p);
}
template<class T1,class T2>
void printPerson2(Person<T1,T2> &p)
{
p.showPerson();
}
void test02()
{
Person<string, int> p("李四", 32);
printPerson2(p);
}
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<int>
{
};
void test01()
{
Son s1;
}
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).类模板分文件编写
**
#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;
};
#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;
}
#include "Person.cpp"
void test01()
{
Person<string, int> p("张三", 18);
p.showPerson();
}
int main()
{
test01();
system("pause");
return 0;
}
**
8).类模板与友元
**
#include<iostream>
#include<string>
using namespace std;
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).类模板案例
**
学习产出:
提示:这里统计学习计划的总量
例如:
|