程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++學習筆記7——模板,學習筆記7模板

C++學習筆記7——模板,學習筆記7模板

編輯:C++入門知識

C++學習筆記7——模板,學習筆記7模板


函數模板:

#include <iostream>
using namespace std;


template <typename T>
T max(const T  &lhs, const T &rhs)
{
    return lhs > rhs ? lhs : rhs;
}

template <typename T,class U>//在模板參數列表中,typename和class沒有區別
T min(const T  &lhs, const U &rhs)
{
    return lhs > rhs ? rhs : lhs;
}

//非類型函數模板
template<unsigned N, unsigned M>
int compare(const char(&p1)[N], const char(&p2)[M])
{
    return strcmp(p1, p2);
}

//可變參函數模板 函數模板重載
template <typename T>
void print(const T &t)
{
    cout << t;
}

template <typename T, typename... Args>
void print(const T &t, const Args&... rest)
{
    cout << t << ",";
    print(rest...);
}

int main()
{
    cout << max<int>(1, 2) << endl;
    cout << max<double>(3.1, 4.2) << endl;

    cout << min<int, char>(100, 'a') << endl;

    cout << compare("ab", "a");

    print("a", 12, 1.23);

    system("pause");
    return 0;
}

 類模板:

#pragma once
#ifndef _COMPLEXNUMBER_
#define _COMPLEXNUMBER_

#include <iostream>
using namespace std; 

template <typename T> class complexNum;  //前置聲明
template <typename T> void printCom(complexNum<T> &obj);

template <typename T>
class complexNum
{
    friend ostream& operator<< <T>(ostream &out, complexNum<T> &rhs);
    friend istream& operator>><T>(istream &in, complexNum<T> &rhs);
    friend void printCom<T>(complexNum<T> &obj);

public:
    complexNum(int real = 0, int image = 0);
    complexNum(const complexNum<T> &obj);

public:
    complexNum<T>& operator=(const complexNum<T> &rhs);

    complexNum<T> operator+(const complexNum<T> &rhs);

    complexNum<T>& operator++(void);     //前置++
    complexNum<T> operator++(int);       //後置++
    complexNum<T>& operator+=(const complexNum &rhs);
    bool operator>(const complexNum<T> &rhs);

private:
    T real;
    T image;
};

#endif
#include "complexNumber.h" template <typename T> complexNum<T>::complexNum(int real, int image) :real(real), image(image){} template <typename T> complexNum<T>::complexNum(const complexNum &obj) : real(obj.real), image(obj.image){} template <typename T> std::ostream& operator<<(std::ostream &out, complexNum<T> &rhs) { out << rhs.real; if (rhs.image >= 0) out << "+"; out << rhs.image << "i" << std::endl; return out; } template <typename T> std::istream& operator>>(std::istream &in, complexNum<T> &rhs) { return in >> rhs.real >> rhs.image; } template <typename T> void printCom(complexNum<T> &obj) { operator<<(cout, obj); } template <typename T> complexNum<T>& complexNum<T>::operator=(const complexNum<T> &rhs) { this->real = rhs.real; this->image = rhs.image; return *this; } template <typename T> complexNum<T> complexNum<T>::operator+(const complexNum<T> &rhs) { complexNum tmp; tmp.real = this->real + rhs.real; tmp.image = this->image + rhs.image; return tmp; } template <typename T> complexNum<T>& complexNum<T>::operator++(void) { this->real++; this->image++; return *this; } template <typename T> complexNum<T> complexNum<T>::operator++(int) { complexNum tmp = *this; this->real++; this->image++; return tmp; } template <typename T> complexNum<T>& complexNum<T>::operator+=(const complexNum &rhs) { this->operator+(rhs); return *this; } template <typename T> bool complexNum<T>::operator>(const complexNum<T> &rhs) { if (this->real > rhs.real) return true; else if (this->real < rhs.real) return false; else { if (this->image > rhs.image) return true; else return false; } } View Code
#include <iostream>
#include "complexNumber.hpp"  //需包含.hpp文件而不是.h文件

int main()
{
    complexNum<int> c1(1, 2);
    cout << c1;
    printCom(c1);
    
    system("pause");
    return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved