函数的重载:
//函数重载 感觉还是太繁琐 引入函数模板的概念
#include <iostream>
using namespace std;
int add(int x , int y)
{
return x + y;
}
double add(double x, double y)
{
return x + y;
}
int main()
{
int a = 1, b = 2;
double c = 3.00, d = 4.00;
cout << add(a, b) << endl;
cout << add(c, d) << endl;
return 0;
}
模板函数:
#include <iostream>
#include <windows.h>
using namespace std;
template <typename T>
T add(T x, T y)
{
return x + y;
}
template <typename T1, typename T2>
void print(T1 x, T2 y)
{
cout << "void print(T1 x, T2 y)";
cout << x << " " << y << endl;
}
template <typename T1, typename T2>//模板函数的重载
void print(T1 x, T2 y , T2 z)
{
cout << "void print(T1 x, T2 y , T2 z)";
cout << x << " " << y << " "<< z << endl;
}
void print(int x , double y)//会优先调用基础的函数 模板其次 除非显示调用了
{
cout << "void print(int x , double y)";
cout << x << " " << y << endl;
}
int main()
{
int a = 1, b = 2;
double c = 3.00, d = 4.00;
cout << add(a, b) << endl; //隐式调用
cout << add(c, d) << endl;
cout << add<int>(a, b) << endl; //显示调用
print(a , c);
print<int, double>(a, c);
print('a' , 'b' ,'c');
system("pause");
return 0;
}
//模板 排序
#include <iostream>
#include <windows.h>
using namespace std;
template <typename T>
void readnumber(T *a)
{
for (int i = 0; i < 5; i++)
{
cin >> a[i];
}
}
template <typename T>
void maopao(T *a)
{
T tmp;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (a[j] < a[j + 1])
{
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
}
template <typename T>
void showresult(T *a)
{
for (int i = 0; i < 5; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
int main()
{
int a[5] = {0};
//double b[5] = {0.0};
cout << "please input 5 numbers" << endl;
readnumber(a);
//cout << "please input 5 numbers" << endl;
//readnumber(b);
maopao(a);
cout << "the result is :" << endl;
showresult(a);
system("pause");
return 0;
}
//模板类 只能显示调用///(类内实现
#include <iostream>
#include <windows.h>
using namespace std;
template <typename T>
class Complex
{
private:
T m_a;
T m_b;
public:
Complex(T a, T b)
{
m_a = a;
m_b = b;
}
void print()
{
cout << "m_a is " << m_a << " m_b is " << m_b << endl;
}
};
int main()
{
Complex<int>c(1 , 2);//指定函数的参数类型
c.print();
system("pause");
return 0;
}
//类外实现
#include <iostream>
#include <windows.h>
using namespace std;
template <typename T>
class Complex
{
private:
T m_a;
T m_b;
public:
Complex(T a, T b);
void print();
};
template <typename T>
Complex<T>::Complex(T a, T b)
{
m_a = a;
m_b = b;
}
template <typename T>
void Complex<T>::print()
{
cout << "m_a is " << m_a << " m_b is " << m_b << endl;
}
int main()
{
Complex<int>c(1, 2);//指定函数的参数类型
c.print();
system("pause");
return 0;
}
//友元函数模板 类内声明和类外声明
#include <iostream>
#include <windows.h>
using namespace std;
template <typename T>
class Complex;
template <typename T>
friend ostream & operator << (ostream &out, const Complex<T> & c1);
template <typename T>
class Complex
{
private:
T m_a;
T m_b;
public:
Complex(T a, T b);
void print();
friend ostream & operator << <T>(ostream &out, const Complex & c1);
/*
friend ostream & operator << (ostream &out, const Complex & c1)//友元函数类内声明
{
out << c1.m_a << " + " << c1.m_b << " i " << endl;;
return out;
}
*/
};
template <typename T>
friend ostream & operator << (ostream &out, const Complex<T> & c1)
{
out << c1.m_a << " + " << c1.m_b << " i " << endl;;
return out;
}
template <typename T>
Complex<T>::Complex(T a, T b)
{
m_a = a;
m_b = b;
}
template <typename T>
void Complex<T>::print()
{
cout << "m_a is " << m_a << " m_b is " << m_b << endl;
}
int main()
{
Complex<int>c(1, 2);//指定函数的参数类型
c.print();
system("pause");
return 0;
}
//模板类
#include <iostream>
#include <windows.h>
using namespace std;
template <typename T>
class A
{
private:
T m_a;
public:
A(T a);
};
template <typename T>
A<T> ::A(T a)
{
m_a = a;
}
class B :public A<int>
{
private:
int m_b;
public :
B(int b);
};
B::B(int b) : A<int>(b)//显式调用
{
m_b = b;
}
template <typename T, typename T2>
class C :public A<T2>
{
private:
T m_c;
public:
C(T c , T2 a);
};
template <typename T , typename T2>
C<T , T2>::C(T c, T2 a) : A<T2>(a)//显式调用
{
m_c = c;
}
template <typename T,typename T2>
class D : public A<T2>
{
private:
T m_d;
public:
D(T d, T2 a);
};
template <typename T,typename T2>
D<T, T2>::D(T d,T2 a) : A<T2>(a)
{
m_d = d;
}
int main()
{
B b(1);
C<int ,double> c(1 , 20.0);
D<int, double> d(12 , 2.1);
system("pause");
return 0;
}
//类模板中的静态成员变量
//同一种数据类型的类 公用一个静态成员变量
#include <iostream>
#include <windows.h>
using namespace std;
template <typename T>
class A
{
private:
T m_a;
public:
A()
{
count++;
}
static int count;
};
template <typename T>
int A<T>::count = 0;
int main()
{
A<int> a1;
A<int> a2;
A<int> a3;
A<double> a4;
A<double> a5;
cout << a1.count << endl;
cout << a4.count << endl;
system("pause");
return 0;
}