presentation on template and exception
DESCRIPTION
presentation on template and exception.. by syed sajid aliTRANSCRIPT
![Page 1: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/1.jpg)
1
![Page 2: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/2.jpg)
2
Template and
exception
![Page 3: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/3.jpg)
3
Template
Template make it possible to use one function or class to handle different
data types syntax:
template <class T>
![Page 4: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/4.jpg)
4
HOW TEMPLATE CAN USE
Template concept can be use in two different conceptsi. Function templateii. Class template
![Page 5: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/5.jpg)
5
FUNCTION TEMPLATE
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.
![Page 6: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/6.jpg)
6
EXAMPLE 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);
}
Function Template
![Page 7: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/7.jpg)
7
OUTPUT Function Template: Simple Function
Continued…
![Page 8: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/8.jpg)
8
TEMPLATE RETURN TYPE
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();}
![Page 9: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/9.jpg)
9
Function template with multiple arguments
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); }
![Page 10: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/10.jpg)
10
EXAMPLE 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:
Function Template
![Page 11: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/11.jpg)
11
CLASS TEMPLATE
Class template definition does not changeClass template work for variable of all types instead of single basic type
![Page 12: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/12.jpg)
12
Example 1template <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:
83.3
Class Template
![Page 13: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/13.jpg)
13
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
Continued…
![Page 14: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/14.jpg)
14
Example 2template <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()<<end
l;system ("pause");}
Output:
84.1
Class Template
![Page 15: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/15.jpg)
15
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);
}
Continued…
![Page 16: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/16.jpg)
16
workingint Basket<int>::Big(){return (first > second?first:second);}float Basket<float>::Big(){return (first > second?first:second);}
Continued…
![Page 17: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/17.jpg)
17
LINKED LIST CLASS USING TEMPLATES
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 )
![Page 18: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/18.jpg)
18
{
cout << endl << current->data; current = current->next; } }
int main() { linklist<double> ld; ld.additem(151.5); ld.additem(262.6); ld.additem(373.7); ld.display();
linklist<char> lch;
lch.additem('a'); lch.additem('b'); lch.additem('c'); lch.display(); cout << endl; system("pause");
}OUTPUT
373.7262.6151.5
cba
Continued…
![Page 19: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/19.jpg)
19
EXCEPTIONAn 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
![Page 20: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/20.jpg)
20
EXCEPTION HANDLING
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.
![Page 21: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/21.jpg)
21
EXCEPTION HANDLING IN C++
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
![Page 22: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/22.jpg)
22
TRY BLOCK 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
![Page 23: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/23.jpg)
23
THE THROW POINT Used to indicate that an exception has
occurred Will be caught by closest exception
handler Syntax:-
if ( // error) { Throw error(); }
![Page 24: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/24.jpg)
24
THE CATCH BLOCK Contain the exception handler. These know what to do with the
exception-typically 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() { ….. }
![Page 25: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/25.jpg)
25
EXCEPTION MECHANISM
![Page 26: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/26.jpg)
26
Exampleconst int DivideByZero = 10;double divide(double x,
double y){ if(y==0) { throw DivideByZero; } return x/y;} int main(){try{ divide(10, 0);}
catch(int i){ if(i==DivideByZero) { cout<<"Divide by zero
error"; }cin.get();}}
Output:-Divide by zero error
Exceptions
![Page 27: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/27.jpg)
27
EXCEPTION SYNTAX Class Aclass {
Public: Class Anerror {
}; Void func() { if(/*error condition*/)
Throw Anerror(); } };
Int main(){Try{Aclass object1;Object1.fun();
}Catch (Aclass ::Anerror)
//may cause error{//tell user about error}Return 0;}
![Page 28: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/28.jpg)
28
MULTIPLE CATCH STATEMENT
try{
//try block}catch(type1 arg){
//catch block1}catch(type2 arg){
//catch block2}
…….…….catch(typeN arg){
//catch blockN}
![Page 29: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/29.jpg)
29
Exampleconst int Max=3;class stack{private:int st[Max],top;public:class full{};Class empty{};stack(){top=-1;}
void push(int var){if(top>=Max-1)throw full();st[++top]=var;}Int pop(){if(top<0)Throw empty();Return st [top--];}};
Multiple Exceptions
![Page 30: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/30.jpg)
30
int main(){stack s1; try{s1.push(11); s1.push(22);s1.push(33); s1.push(44);S1.pop(); s1.pop();S1.pop(); s1.pop(); }catch(stack::full){cout<<"exception :stack
full"<<endl;}
Catch(stack::empty){Cout<<“exception: stack
empty”<<endl;}cin.get(); }
Continued…
![Page 31: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/31.jpg)
31
Exceptions with the Distance Class
class Distance{private:int feet;float inches;public:class InchesEx { };Distance(){ feet = 0; inches = 0.0; }Distance(int ft, float in) {if(in >= 12.0) throw InchesEx(); feet = ft;inches = in;}
void getdist(){cout << "\nEnter feet: "; cin
>> feet;cout << "Enter inches: "; cin
>> inches;if(inches >= 12.0) throw InchesEx();}void showdist(){ cout << feet << "\’-" <<
inches; }};
![Page 32: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/32.jpg)
32
int main(){try{Distance dist1(17, 3.5); Distance dist2; dist2.getdist();cout << "\ndist1 = "; dist1.showdist();cout << "\ndist2 = "; dist2.showdist();}catch(Distance::InchesEx){cout << "\nInitialization error: ";cout<< "inches value is too large.";}cout << endl;return 0;}
Continued…
![Page 33: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/33.jpg)
33
Exceptions with Arguments
class Distance {private:int feet;float inches;public:class InchesEx {public:string origin; float iValue; InchesEx(string or, float in){origin = or; iValue = in; }};
Distance() { feet = 0; inches = 0.0; }Distance(int ft, float in){if(in >= 12.0)throw InchesEx("2-arg
constructor", in);feet = ft;inches = in;}void getdist(){cout << "\nEnter feet: "; cin
>> feet;cout << "Enter inches: "; cin
>> inches;
![Page 34: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/34.jpg)
34
if(inches >= 12.0)throw InchesEx("getdist()
function", inches);}void showdist() { cout << feet << "\’-" <<
inches; }};void main(){try{Distance dist1(17, 3.5); Distance dist2;
dist2.getdist();cout << "\ndist1 = ";dist1.showdist();cout << "\ndist2 = ";dist2.showdist();}catch(Distance::InchesEx ix){cout << "\nInitialization
error in " << ix.origin<< ".\n Inches value of " <<
ix.iValue<< " is too large.";}
Continued…
![Page 35: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/35.jpg)
35
SPECIFYING DATA IN EXCEPTION
CLASSclass InchesEx {public:string origin; float iValue; InchesEx(string or, float in){origin = or; iValue = in; }};
![Page 36: Presentation on template and exception](https://reader035.vdocument.in/reader035/viewer/2022070319/558385d0d8b42a282c8b4646/html5/thumbnails/36.jpg)
36
Bad_alloc Extracting data from the exception object
int main(){const unsigned long SIZE = 10000; char* ptr; try{ptr = new char[SIZE]; }catch(bad_alloc){cout << “\nbad_alloc exception: can’t allocate memory.\n”;return(1);}delete[] ptr; //deallocate memorycout << “\nMemory use is successful.\n”;return 0;}