C ++ study notes 7-templates and study notes 7 templates
Function template:
# 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> // in the template parameter list, there is no difference between typename and class T min (const T & lhs, const U & rhs) {return lhs> rhs? Rhs: lhs;} // non-type function template <unsigned N, unsigned M> int compare (const char (& p1) [N], const char (& p2) [M]) {return strcmp (p1, p2);} // Variable Parameter Function template function template overload 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 ("", 12, 1.23); system ("pause"); return 0 ;}Class template:
# Pragma once # ifndef _ COMPLEXNUMBER _ # define _ COMPLEXNUMBER _ # include <iostream> using namespace std; template <typename T> class complexNum; // pre-declaration 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); // frontend ++ complexNum <T> operator ++ (int ); // post ++ 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>: 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 >:: 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 "// must contain. hpp file instead. h file int main () {complexNum <int> c1 (1, 2); cout <c1; printCom (c1); system ("pause"); return 0 ;}