Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introducing Templates and Generics in C++

Similar presentations


Presentation on theme: "Introducing Templates and Generics in C++"— Presentation transcript:

1 Introducing Templates and Generics in C++
CSC

2 Overview Value of templates Generic functions Generic classes

3 Value of templates Added to original C++ specification
Valuable in the creation of reusable code Used to create generic functions and classes In which the type of data acted upon is specified as a parameter Allows one function or class to act on several different types of data without having to code specific versions for each type.

4 GENERIC FUNCTIONS (a.k.a. template functions)

5 Generic function Defines a set of operations that are applied to various types of data. Requires the keyword “template” A pattern for what could be a function.

6 General form of header:
Generic function General form of header: template <class Ttype> ret-type func-name (parameter list) Body: { // body of function } Example of header: template class X void swapargs(X &a, X &b)

7 Example generic function swapargs()
template <class X> void swapargs (X &a, X &b) { X temp; temp = a; a=b; b=temp; }

8 Example main using swapargs()
int main() { int I =10, j=20; double x=10.1, y=23.3; char a=‘x’, b=‘z’; cout<<“Original i, j “<<i<<‘ ‘<<j<<‘\n’; cout<<“Original x,y “<<x<<‘ ‘<<y<<‘\n’; cout<<“Original a,b “<<a<<‘ ‘<<b<<‘\n’; swapargs(i, j); swapargs(x, y); swapargs(a, b); cout<<“Swapped i, j “<<i<<‘ ‘<<j<<‘\n’; cout<<“Swapped x,y “<<x<<‘ ‘<<y<<‘\n’; cout<<“Swapped a,b “<<a<<‘ ‘<<b<<‘\n’; return 0; }

9 What happens? “template” preceding the function definition tells the compiler to create a generic (or template) function. “X” is a placeholder for the generic type. In main(), swapargs() is called using 3 different types of data. This causes the compiler to generate 3 different versions of swapargs(), each for a different type. Called “generic functions” or “specialized functions.” Act of generating referred to as “instantiating.”

10 More about generic functions
More than one placeholder type may be declared for a function. The keyword class to specify a generic type is traditional, but keyword typename may also be used. Generic functions may be overridden for “explicit specialization.” Example: void swapargs(int &a, int &b) This “hides” the generic swapargs() only for ints. Alternative syntax: template<> void swapargs<int> (int &a, int &b) Generic functions may be overloaded (different parameter lists).

11 GENERIC CLASSES

12 Generic class Defines all the algorithms used by that class
Actual type of data being manipulated is specified as a parameter. Useful when the class uses logic that can be generalized.

13 Example (2 generic data types)
Generic class heading General form template <class Ttype> class class-name Example template <class Qtype> class queue Example (2 generic data types) template <class Type1, class Type2> class myClass

14 Generic class: object creation
To create a specific instance of a generic class: class-name <type> ob; For previous examples: queue<int> a, b; // creates 2 integer queues myClass<int, double> ob1(10, 0.23);

15 Example generic class #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', "This is a test"); ob1.show(); // show int, double ob2.show(); // show char, char * system("pause"); return 0; }


Download ppt "Introducing Templates and Generics in C++"

Similar presentations


Ads by Google