Presentation is loading. Please wait.

Presentation is loading. Please wait.

Templates © Bruce M. Reynolds & Cliff Green 1 // min function template #include using std::cout; using std::endl; // there’s already one of these in the.

Similar presentations


Presentation on theme: "Templates © Bruce M. Reynolds & Cliff Green 1 // min function template #include using std::cout; using std::endl; // there’s already one of these in the."— Presentation transcript:

1 Templates © Bruce M. Reynolds & Cliff Green 1 // min function template #include using std::cout; using std::endl; // there’s already one of these in the std lib template T min( T a, int b=2) { return ( a < b) ? a : b; } int main() { int n1 = 5; int n2 = min( n1, 7 ); cout << "The min value is " << n2 << endl; float f1 = 4.5; float f2 = min( f1 ); cout << "The min value is " << f2 << endl; return 0; }

2 Templates © Bruce M. Reynolds & Cliff Green 2 // printArray function template #include using std::cout; using std::endl; template void printArray(T const* array, int const count ) { for ( int i = 0; i < count; ++i ) cout << array[ i ] << " "; cout << endl; }

3 Templates © Bruce M. Reynolds & Cliff Green 3 // printArray function template -- continued int main() { int const aCount = 5, bCount = 7, cCount = 6; int a[ aCount ] = { 1, 2, 3, 4, 5 }; double b[ bCount ] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 }; char c[ cCount ] = "HELLO"; // 6th position for nul char cout << "Array a contains:" << endl; printArray( a, aCount ); // integer template function cout << "Array b contains:" << endl; printArray( b, bCount ); // double template function cout << "Array c contains:" << endl; printArray( c, cCount ); // character template function return 0; }

4 Templates © Bruce M. Reynolds & Cliff Green 4 // Value class template class Integer { public: Integer( int I = 0 ) : i( I ) { } Integer const operator+(Integer const& rhs ) const { return Integer( i + rhs.i ); } friend ostream& operator<<( ostream& os, Integer const& x ) { return os << x.i; } void display() const { cout << "Integer value is: " << i << endl; } private: int i; };

5 Templates © Bruce M. Reynolds & Cliff Green 5 // Value class template -- continued template class Value { public: Value( T aValue ) : value( aValue ) {} void display() const { cout value is: " << value << endl; } Value const operator+( Value const& rhs ) const { return Value (value + rhs.value); } friend ostream& operator const& x ) { return os << x.value; } private: T value; };

6 Templates © Bruce M. Reynolds & Cliff Green 6 // Value class template -- continued #include using namespace std; #include "Simple.h" int main() { Integer int1, int2(20), int3(30); cout << int2 << " " << int3 << endl; int1 = int2 + int3; int1.display(); cout << int1 << endl; return 0; }

7 Templates © Bruce M. Reynolds & Cliff Green 7 // Value class template -- continued #include using namespace std; #include "Simple.h" int main() { Value ival1(10), ival2(20), ival3(30); cout << ival2 << " " << ival3 << endl; ival1 = ival2 + ival3; ival1.display(); cout << ival1 << endl; Value dval1(10.0), dval2(20.0), dval3( 30.0); cout << dval2 << " " << dval3 << endl; dval1 = dval2 + dval3; dval1.display(); cout << dval1 << endl; Value cval1('a'), cval2('b'), cval3('c'); cval1 = cval2 + cval3; cval1.display(); cout<< cval1 << endl; return 0; }

8 Templates © Bruce M. Reynolds & Cliff Green 8 // Default type parameters // By Jim Peckol #include using namespace std; template class M1 { public: M1(T1 aValue0, T2 aValue1) : myData(aValue0), myData0(aValue1){}; M1(T1 aValue) : myData(aValue), myData0(10){}; M1() : myData((T1)20), myData0((T2)3){}; T1 myData; T2 myData0; void show() const {cout << myData << " " << myData0 << endl;} };

9 Templates © Bruce M. Reynolds & Cliff Green 9 // Default type parameters -- continued // By Jim Peckol int main() { // Declare a couple of instances M1 m1(15.3f); M1 m2(21, 'c'); m1.show(); m2.show(); // Execution will print //15.3 10 //21c return 0; }

10 Templates © Bruce M. Reynolds & Cliff Green 10 // Using a class as a type parameter // By Jim Peckol #include using std::cout; using std::endl; template class M1 { public: M1(T1 aValue) : myData(aValue){}; M1() : myData(2){}; T1 myData; void show() const {cout << "What am I now????" << endl;} }; template class M2 { public: M2(T1 aValue) : myData0(aValue){}; T1 myData0; void show() const {cout << myData0.myData << endl;} T1 get() const {return myData0;} };

11 Templates © Bruce M. Reynolds & Cliff Green 11 // Using a class as a type parameter -- continued // By Jim Peckol int main() { M1 mine(10); M1 mine1; M2 > yours(mine); mine.show(); yours.show(); return 0; }

12 Templates © Bruce M. Reynolds & Cliff Green 12 // Template specialization // By Jim Peckol #include // Declare the template function add template Type add(Type a, Type b) {return a+b;} // Specialize the add template for the string class char* add(char const* aString1, char const* aString2) { char *data = new char [strlen (aString1)+ strlen(aString2) + 1]; strcpy(data, aString1); strcat (data, aString2); return data; };

13 Templates © Bruce M. Reynolds & Cliff Green 13 // Template specialization -- continued // By Jim Peckol int main( ) { int a = 10, b = 20; float c = 10.5, d = 11.7; char e = 'a', f = 'b'; char const* g = "You're just “; char const* h = "stringing me along."; // Execution will print //30, 22.2 the integer sum of a and b //You're just stringing me along. cout << add(a,b) << " " << add(c,d) << " " << add(e,f) << endl; cout << add(g,h) << endl; // memory leak? return 0; }

14 Templates © Bruce M. Reynolds & Cliff Green 14 // A simple fixed size vector / array // By Jim Peckol void vector_test(); template class Vector { public: Vector() {}// default constructor Vector (T v0);// initialize elements to v0 T& operator [](int i);// specify subscripting operator void display() const; private: T vect[n]; }; template Vector ::Vector(T v0) { // initialize the elements of the vector for (int i=0; i< n; ++i) { vect[i] = v0; }

15 Templates © Bruce M. Reynolds & Cliff Green 15 // A simple fixed size vector / array -- continued // By Jim Peckol template T& Vector ::operator [](int i) { return vect[i-1]; } template void Vector ::display() const { // determine the size of the vector int j = (sizeof (vect))/(sizeof (T)); for (int i = 0; i< j; ++i) { cout << vect[i] << " "; } cout << endl; return; }


Download ppt "Templates © Bruce M. Reynolds & Cliff Green 1 // min function template #include using std::cout; using std::endl; // there’s already one of these in the."

Similar presentations


Ads by Google