8-1日复习 模板函数 模板类
生活随笔
收集整理的這篇文章主要介紹了
8-1日复习 模板函数 模板类
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
函數(shù)的重載:
//函數(shù)重載 感覺還是太繁瑣 引入函數(shù)模板的概念#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; }模板函數(shù):
#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>//模板函數(shù)的重載 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)//會(huì)優(yōu)先調(diào)用基礎(chǔ)的函數(shù) 模板其次 除非顯示調(diào)用了 {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; //隱式調(diào)用cout << add(c, d) << endl;cout << add<int>(a, b) << endl; //顯示調(diào)用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; }//模板類 只能顯示調(diào)用///(類內(nèi)實(shí)現(xiàn)
#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);//指定函數(shù)的參數(shù)類型c.print();system("pause");return 0; }//類外實(shí)現(xiàn)
#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);//指定函數(shù)的參數(shù)類型c.print();system("pause");return 0; }//友元函數(shù)模板 類內(nèi)聲明和類外聲明
#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)//友元函數(shù)類內(nèi)聲明{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);//指定函數(shù)的參數(shù)類型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)//顯式調(diào)用 {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)//顯式調(diào)用 {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; }//類模板中的靜態(tài)成員變量
//同一種數(shù)據(jù)類型的類 ?公用一個(gè)靜態(tài)成員變量
#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; }?
總結(jié)
以上是生活随笔為你收集整理的8-1日复习 模板函数 模板类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。