该内容可以测试模板函数优先选择那个函数,在模板类中如何继承,如何定义友元函数,如何重载。
模板类cpp
#include "leimoban.h"
template<class T, class T1>
person<T,T1>::person(T id, T1 age)
{
m_id = id;
m_age = age;
}
template<class T, class T1>
void person<T, T1>::show()
{
std::cout << m_id << " " << m_age << std::endl;
}
template<class T, class T1>
hhhdsjk<T, T1>::hhhdsjk(int jhcdu, T id, T1 age):person<T,T1>(id,age)
{
m_id = id;
m_age = age;
jkhuhd = jhcdu;
}
template<class T, class T1>
std::ostream& operator<<(std::ostream& os, hhhdsjk<T, T1 >& p)
{
os << p.m_id << "" << p.m_age << " " << p.jkhuhd;
return os;
}
template<class T, class T1>
void hhhdsjk<T, T1>::show()
{
std::cout << m_id << " " << m_age << " " << jkhuhd<<std::endl;
}
template<class T, class T1>
void hdsh(const hhhdsjk<T, T1>& p)
{
std::cout << "友元函数"<<p.m_id << " " << p.m_age << " " << p.jkhuhd << std::endl;
}
template<class T, class T1>
int hhhdsjk<T, T1>::hhdhjk = 520;
模板类h
#pragma once
#include <iostream>
#include <string>
using namespace std;
template<class T, class T1>
class person
{
public:
person(T id, T1 age);
void show();
public:
T m_id;
T1 m_age;
};
template<class T, class T1> class hhhdsjk;
template<class T, class T1> void hdsh(const hhhdsjk<T, T1>& p);
template<class T, class T1>
class hhhdsjk :public person<T , T1 >
{
public:
template<class T, class T1>
friend ostream& operator <<(ostream& o, const hhhdsjk<T, T1>& p);
friend void hdsh<T, T1>(const hhhdsjk<T, T1>& p);
hhhdsjk(int jhcdu,T id, T1 age);
static int hhdhjk;
void show();
public:
T m_id;
T1 m_age;
int jkhuhd=0;
};
main.cpp
#include <iostream>
#include "leimoban.cpp"
template<class T2, class T1>
int ADD(T2 A, T1 B)
{
return A + B;
}
int ADD(int A, char B)
{
return A + B;
}
template <class T>
void paixu(T* hjgh, int a)
{
for (int i = 0; i <a ; i++)
{
for (int j = i; j < a; j++)
{
if (hjgh[j] < hjgh[i])
{
T tem = hjgh[i];
hjgh[i] = hjgh[j];
hjgh[j] = tem;
}
}
}
}
template <class T>
void shuchu(T* hjgh, int a)
{
for (int i = 0; i < a; i++)
{
std::cout << hjgh[i] << std::endl;
}
}
int main()
{
int a = 10;
int h = 30;
int hui = 80;
char sdjds = 'k';
std::cout << ADD(a,h)<< std::endl;
std::cout << ADD(hui, sdjds) << std::endl;
std::cout << ADD(sdjds, h) << std::endl;
std::cout << ADD(sdjds, sdjds) << std::endl;
hhhdsjk<int, char> person1(245,20, 52);
person1.show();
person1.hhdhjk = 56;
std::cout << person1.hhdhjk << std::endl;
hdsh(person1);
hhhdsjk<int, char> person2(2,20, 's');
person2.hhdhjk = 560;
std::cout << person2.hhdhjk << std::endl;
person2.show();
std::cout << person2 << std::endl;
int hdkjsha[] = { 52,452,5426,258,325,96,2563 };
char cdjksc[] = { 's','a','d','s','q','r','u' };
int intlen = sizeof(hdkjsha) / sizeof(int);
int charlen= sizeof(cdjksc) / sizeof(char);
paixu(hdkjsha, intlen);
paixu(cdjksc, charlen);
shuchu(hdkjsha, intlen);
shuchu(cdjksc, charlen);
}