5. The
body of the function template is
written in the same way in each case
as function is written but the difference
is that they can handle arguments and
return value of different types.
5
6. Function Template
Template function:
template <class T>
void get(T a)
{
cout <<" value : "<<a;
cin.get();
}
void main()
{
int a=5;
float b=4.3;
get(a);
get(b);
}
Simple function:
void get(int a)
{
cout <<" value : "<<a;
cin.get();
getch();
}
void main()
{
int a=5;
float b=4.3;
get(a);
get(b);
}
6
8. template <class T>
T square(T value)
{
return value*value;
}
int main()
{
int a=5;
float b=7.5;
long c=50000900000;
double d=784848.33;
cout<<"square of int variable is :
"<<square(a)<<endl;
cout<<"square of long variable is :
"<<square(b)<<endl;
cout<<"square of long variable is : "<<square
(c)<<endl;
cout<<"square of double variable is :
"<<square(d)<<endl;
cin.get();
getch();
8
9. template<class t >
void get( t x, t y , int c)
{
int sum;
sum=x+y+c;
cout<<sum;
cin.get();
getch();
}
void main()
{
int a=6;
int b=9;
int c=8;
get(a,b,c);
}
9
10. Function Template
template<class t, class v >
void get(t x, v y ,int c)
{
v sum;
sum=x+y+c;
cout<<sum;
cin.get();
getch();
}
void main()
{
int a=6;
float b=9.9;
int c=8;
get(a,b,c);
}
Output:
10
12. Class Template
template <class t>
class Basket
{
t first;
t second;
public:
Basket (t a, t b)
{
first = a;
second = b;
}
t Bat()
{
return (first > second?first:second);
}
}; //class end
void main()
{
Basket <int> bo(6,8);
cout<<bo.Bat()<<endl;
Basket <float> b1(1.1,3.3);
cout<<b1.Bat()<<endl;
system ("pause");
}
Output:
8
3.3
12
13. Continued…
If we write
Basket <int> b(6.99,8.88);
Instead of
Basket <int> b(6,8);
Output will be in integers i.e
8
13
14. Class Template
template <class t>
class Basket
{
t first;
t second;
public:
Basket (t a, t b)
{
first = a;
second = b;
}
t Big();
}; //class end
template <class t>
t Basket <t>::Big()
{
return (first > second?first:second);
}
void main()
{
Basket <int> b(6,8);
cout<<b.Big()<<endl;
Basket <float> b1(4.1,1.1);
cout<<b1.Big()<<endl;
system ("pause");
}
Output:
8
4.1
14
15. Continued…
template <class t>
t Basket <t>::Big()
{
return (first > second?first:second);
}
The
name Basket<t> is used to identify the
class of which Big() is a member function . In
a normal non-template member function the
name Basket alone would suffice.
Void Basket :: Big()
{
return (first > second?first:second);
}
15
17. template<class TYPE>
struct link
{
TYPE data;
link* next;
};
template<class TYPE>
class linklist
{
private:
link<TYPE>* first;
public:
linklist()
{ first = NULL; }
void additem(TYPE d);
void display();
};
template<class TYPE>
void
linklist<TYPE>::additem(TYPE
d)
{
link<TYPE>* newlink = new
link<TYPE>;
newlink->data = d;
newlink->next = first;
first = newlink;
}
template<class TYPE>
void linklist<TYPE>::display()
{
link<TYPE>* current = first;
while( current != NULL )
17
18. Continued…
{
linklist<char> lch;
cout << endl << current->data;
current = current->next;
}
}
lch.additem('a');
lch.additem('b');
lch.additem('c');
lch.display();
cout << endl;
system("pause");
int main()
{
linklist<double> ld;
ld.additem(151.5);
ld.additem(262.6);
ld.additem(373.7);
ld.display();
}
OUTPUT
373.7
262.6
151.5
c
b
a
18
19. An
exception is a condition that
occurs at execution time and make
normal continuation of program
impossible.
When
an exception occurs, the
program must either terminate or jump
to special code for handling the
exception.
Divide by zero errors.
Accessing the element of an array beyond its
range
Invalid input
Hard disk crash
Opening a non existent file
19
20. The
way of handling anomalous situations in a
program-run is known as exception handling.
Its advantage are:
Exception handling separate error-handling code from normal code.
It clarifies the code and enhances readability
Catch error s before it occurs.
It makes for clear, robust and fault -tolerant program s.
20
21.
Tries a block of code that may contain
exception
Throws an exception when one is detected
Catches the exception and handles it
Thus there are three concepts
i. The try block
ii.The throwing of the exception
iii.The catch block
21
22.
A block which includes the code that may
generate the error(an exception)
try { ….
}
Can be followed by one or more catch blocks
which handles the exception
Control of the program passes from the
statements in the try block ,to the appropriate
catch block.
Functions called try block, directly or
indirectly, could test for the presence of the
error
22
23.
Used to indicate that an exception has occurred
Will be caught by closest exception handler
Syntax:if ( // error)
{
Throw error();
}
23
24. Contain the exception handler.
These know what to do with the exceptiontypically print out that a type of error has
occurred.
Catch blocks are typically located right after
the try block that could throw the exception
Syntax:catch()
{
…..
}
24