AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Templates
Function Templates To write invers function that Receive different type we write: Function templates: allow you to create generic functions that have the same bodies but can take different data types as parameters It’s a powerful feature supported by C++ language
Using function templates Rewrite invers function using template we can : In a function template, at least one parameter is generic, or parameterized, meaning that one parameter can stand for any number of C++ types. T is simply a placeholder for the type that will be used at each location in the function definition where T appears. keyword class in the template definition does not necessarily mean that T stands for a programmer-created class type, but it may. The compiler generates code for as many different functions as it needs, depending on the function calls that are made
Example The following Swap function can be used to interchange between two integer numbers void swap (int &x, int &y){ int temp=x; x=y; y=temp;} The following Swap function can be used to interchange between two character items void swap (char &x, char &y){ char temp=x; x=y; y=temp;} To avoid repetition of function we can use template function: template void swap (R &x, R &y){ R temp=x; x=y; y=temp;}
Using more than one parameterized type in a function template To create a function template that employs multiple generic types, you simply use a unique type identifier for each type. For example, suppose you want to create a display () function that displays two values template void display (T val1, U val2) { cout << "First is " << val1 << " Second is " << val2 << " "; } val1 and val2 can be different types.
Using class template Class template allow you to generate a class in which at least one type is generic or parameterized. To create a class template, you begin with the template definition, just as you do with a function template. Then you write the class definition using the generic type Example: template class Number { private: T Number; public: Number(T n): Number(n){}; void displayNumber(){cout<< Number;} }; To defined object myValue from template class we use: Number myValue(25); // ClassName ObjectName
Example #include template class Number { private: T theNumber; public: Number(T n); void display (); }; template Number ::Number(T n) { theNumber = n;} template Void Number ::display () { cout << "Number # "; cout << theNumber << endl; } int main() { Number anInt(65); Number aDouble(3.46); Number aChar('K'); Number aCharAsAnInt('K'); Number anIntAsAChar(65); anInt.display (); aDouble.display (); aChar.display (); aCharAsAnInt.display (); anIntAsAChar.display (); return 0; }
Creating an Array template Class #include template class Array{ private: T *data; // T is the type of the array int size; public: Array(T *d, int s); void showList(); void showFirst(); }; template Array ::Array(T *d, int s){ data = d; size = s; } template void Array ::showList(){ cout << "Entire list:" << endl; for(int x = 0; x < size; ++x) cout << data[x] << endl; cout << " " << endl;} template void Array ::showFirst(){ cout << "First element is "; cout << data[0] << endl;} int main(){ int nums[4]={1,2,3,4}; Array MyIntArray(nums, 4); MyIntArray.showList(); MyIntArray.showFirst(); char ch[5]={'A','B','C','D'}; Array MyCharArray(ch, 5); MyCharArray.showList(); return 0;}
Defined classes In c++ we can save classes implementation in.h file. And in man function we can import it #include #include “XClass.h" class Yclass { int i; public: X x; Yclass() { i = 0; } void f(int b) { i = b; } int read() { return i; } }; int main() { Yclass y; y.f(47); y.x.set(37); cout<<y.x.read(); cout<<y.read(); } #ifndef XCLASS_H #define XCLASS_H class X { int i; public: X() { i = 0; } void set(int ii) { i = ii; } int read() const { return i; } int permute() { return i = i * 47; } }; #endif Save it as XClass.h in same working folder
Exercise Class in UML Write C++ program To implement classes