Created
August 31, 2011 17:57
-
-
Save gennad/1184205 to your computer and use it in GitHub Desktop.
templates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // function template | |
| #include <iostream> | |
| using namespace std; | |
| template <class T> | |
| T GetMax (T a, T b) { | |
| T result; | |
| result = (a>b)? a : b; | |
| return (result); | |
| } | |
| template <class T, class U> | |
| T GetMin (T a, U b) { | |
| return (a<b?a:b); | |
| } | |
| template <class T> | |
| class mypair { | |
| T values [2]; | |
| public: | |
| mypair (T first, T second) | |
| { | |
| values[0]=first; values[1]=second; | |
| } | |
| }; | |
| #include <iostream> | |
| using namespace std; | |
| template <class T> | |
| class mypair2 { | |
| T a, b; | |
| public: | |
| mypair (T first, T second) | |
| {a=first; b=second;} | |
| T getmax (); | |
| }; | |
| template <class T> | |
| T mypair2<T>::getmax () | |
| { | |
| T retval; | |
| retval = a>b? a : b; | |
| return retval; | |
| } | |
| // class template: | |
| template <class T> | |
| class mycontainer { | |
| T element; | |
| public: | |
| mycontainer (T arg) {element=arg;} | |
| T increase () {return ++element;} | |
| }; | |
| // class template specialization: | |
| template <> | |
| class mycontainer <char> { | |
| char element; | |
| public: | |
| mycontainer (char arg) {element=arg;} | |
| char uppercase () | |
| { | |
| if ((element>='a')&&(element<='z')) | |
| element+='A'-'a'; | |
| return element; | |
| } | |
| }; | |
| template <class T, int N> | |
| class mysequence { | |
| T memblock [N]; | |
| public: | |
| void setmember (int x, T value); | |
| T getmember (int x); | |
| }; | |
| template <class T, int N> | |
| void mysequence<T,N>::setmember (int x, T value) { | |
| memblock[x]=value; | |
| } | |
| template <class T, int N> | |
| T mysequence<T,N>::getmember (int x) { | |
| return memblock[x]; | |
| } | |
| int main () { | |
| int i=5, j=6, k; | |
| long l=10, m=5, n; | |
| k=GetMax<int>(i,j); | |
| n=GetMax<long>(l,m); | |
| cout << k << endl; | |
| cout << n << endl; | |
| k=GetMax(i,j); | |
| n=GetMax(l,m); | |
| cout << k << endl; | |
| cout << n << endl; | |
| int i,j; | |
| long l; | |
| i = GetMin<int,long> (j,l); | |
| i = GetMin (j,l); | |
| mypair<int> myobject (115, 36); | |
| mypair<double> myfloats (3.0, 2.18); | |
| mypair2 <int> myobject (100, 75); | |
| cout << myobject.getmax(); | |
| mycontainer<int> myint (7); | |
| mycontainer<char> mychar ('j'); | |
| cout << myint.increase() << endl; | |
| cout << mychar.uppercase() << endl; | |
| mysequence <int,5> myints; | |
| mysequence <double,5> myfloats; | |
| myints.setmember (0,100); | |
| myfloats.setmember (3,3.1416); | |
| cout << myints.getmember(0) << '\n'; | |
| cout << myfloats.getmember(3) << '\n'; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment