1 / 5

C++ Programming: chapter 8 – Templates

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;}

wyman
Download Presentation

C++ Programming: chapter 8 – Templates

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2012, Spring Pusan National University Ki-Joune Li http://isel.cs.pnu.edu/~lik C++ Programming: chapter 8 – Templates 1 1

  2. 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

  3. 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;  }

  4. 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

  5. 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; }

More Related