Вы находитесь на странице: 1из 12

Session Objectives

Define Templates
Types of Templates Explain Function Templates Explain Class Templates

A generic function defines a general set of operations that will be applied to various types of data that a function will operate upon. A generic class defines the algorithm that specifies the actual type of data when objects of that class are created A generic class and a generic function is created using the keyword Template

ADVANTAGES : It helps to define classes that are general in nature.


It makes the process simpler and safer

// TEMPLATES DEFINITION #include<iostream.h> #include<conio.h> template <class T> T max(T x,T y) { return(x>y)?x:y; }; void main() { clrscr(); cout<<max(17,19)<<endl; cout<<max(1.5,6.7)<<endl; cout<<max('A','B')<<endl; getch(); }

Output : 19 6.7

function template Example ===================== #include<iostream.h> #include<conio.h> template<class T1,class T2> void good(T1 x,T2 y) { clrscr(); cout<<x<<" "<<y<<endl; } void main() { good(456,"hello"); getch(); }

Output :
456 hello

For Example #include<iostream.h> : void function1(int i) { cout<<"The value of i is"<<i<<endl; } void function2(double d) { cout<<"The integer part is"<<int(d); d - = int(d); cout<<"The factorial part is"<<d; } Output : void main() The value of i is 2 { function1 (2); The integer part is 23 function2 (23.5); The factorial part is 0.5 }

A program to define the function template for swapping two items of the various data type as integer and floating point number #include<iostream.h> #include<conio.h> template<class T>T swap( &second) { T temp; temp = first; first = second; T &first, T

void main() { int ix,iy; float fx,fy; cout<<"Enter any twoNo \n"; cin>>ix>>iy; cout<<"Enter any 2 Real No"; cin>>fx>>fy; swap(ix,iy); cout<<"after swapping; cout<<ix <<iy; swap(fx,fy); cout<<"After swapping : cout<<fx <<

second = temp;
return(0); } int swap(int &a,int &b);

fy<<endl;
getch(); }

Default Constructor
#include<iostream.h> #include<conio.h> template<class T> class sample { private: T value; public: sample(T=0) { } void display() { cout<<"default constructor is called"<<endl; cout<<"Content of the value "<<value<<endl; } };

void main() { clrscr(); sample<int> obj1; obj1.display(); sample<int> obj2; obj2.display(); getch(); } //it prints the ascii value of the int and float. Overloading -> We have to pass a value for the variable and have to implement Overridding -> To overwrite the method or function.

CLASS TEMPLATE
#include<iostream.h> template<class q> class sample { private: q value,value1,value2; public: get(); sum(); template <class q> { void sample<q>::get() { cout<<"Enter a value for the value1"<<endl; cin>>value1; cout<<"Enter a value for the value2"<<endl; cin>>value2;

} } template<class q> void sample<q>::sum() { q value; value=value1+value2; cout<<"The added value is"<<value<<endl; } }; void main() { clrscr(); sample<int>obj1; sample<float>obj2;

cout<<"Enter the integer value"<<endl; obj1.get(); obj1.sum(); cout<<"Enter the floating value"<<endl; obj2.get(); obj2.sum(); getch(); }

A generic function defines a general set of operations


that will be applied to various types of data that a function will operate upon.

A generic class defines the algorithm that specifies the


actual type of data when objects of that class are created A generic class and a generic function is created using the keyword template

Rules to be followed for all programs written in C

EXERCISES

1. Explain generic functions with an example? 2. Explain generic class with an example?

Вам также может понравиться