Understanding C++ Templates: Implementation and Examples
This document offers a comprehensive introduction to C++ templates, showcasing practical implementations. It covers the creation of class templates for various data types, such as FloatVector and IntVector, and explicitly specialized templates. The text explores generic programming concepts, demonstrating the use of templates with different data types through classes like MyClass and array. By walking through well-commented examples, both beginners and intermediate programmers can gain insights into the power of templates for reducing code duplication and enhancing flexibility in software design.
Understanding C++ Templates: Implementation and Examples
E N D
Presentation Transcript
2012, Spring Pusan National University Ki-Joune Li http://isel.cs.pnu.edu/~lik C++ Programming: chapter 8 – Templates 1 1
PNU • Class Template class FloatVector { float* values; int num; public: FloatVector(int); ~FloatVector() { delete[] values;} float operator[](int i) { return values[i]; } }; FloatVector::FloatVector(int n): num(n) { values=new int[n]; } class IntVector { int* values; int num; public: IntVector(int); ~IntVector() { delete[] values;} int operator[](int i) { return values[i]; } }; IntVector::IntVector(int n): num(n) { values=new int[n];} Template으로 중복 예방 T를 int 로 간주 int main() { Vector <int> iV(4); int i; for(i=0;i<4;i++) iV[i] = i*i; for(i=0;i<4;i++) cout<<iV[i] << " "; cout << endl; Vector <float> fV(4); for(i=0;i<4;i++) fV[i] = sqrt(i); for(i=0;i<4;i++) cout<<fV[i] << " "; cout << endl; return 0; } template<class T> class Array { int num; public: Vector(T); ~Vector() { delete[] values;} T& operator[](int i) { return values[i]; } }; Vector<T>::Vector(int n): num(n) { values=new int[n]; } T를 float 으로 간주 2
PNU • Class Template #include <iostream> using namespace std; template <class T> class MyClass { T x; public: MyClass(T a) { cout << "Inside generic MyClass\n"; x = a; } T getx() { return x; } }; // Explicit specialization for int. template <> class MyClass<int> { int x; public: MyClass(int a) { cout << "Inside MyClass<int> specialization\n"; x = a * a; } int getx() { return x; } }; int main() { MyClass<double> d(10.1); cout << "double: " << d.getx() << endl; MyClass<int> i(5); cout << "int: " << i.getx() <<endl; return 0; }
PNU • Class Template #include <iostream> using namespace std; template <class T, int N> class array { T memblock[N]; public: void setMember (int x, T value) { memblock[x]=value;} T getMember (int x) { return memblock[x]; } }; int main () { array <int,5> myInts; array <float,5> myFloats; myInts.setMember (0,100); myFloats.setMember(3,3.1); cout << myInts.getMember(0) << endl; cout << myFloats.getMember(3) <<endl; return 0; } 4
PNU • Class Template #include <iostream> using namespace std; template <class Type1, class Type2> class MyClass { Type1 i; Type2 j; public: MyClass(Type1 a, Type2 b) { i = a; j = b; } void show() { cout << i << ' ' << j << '\n';} }; int main() { MyClass<int, double> ob1(10, 0.23); MyClass<char, char*> ob2('X', "AAAAAAA"); ob1.show(); ob2.show(); return 0; }