SlideShare uma empresa Scribd logo
1 de 52
Exception Handling In worst case there must  be emergency exit Lecture slides by: Farhan Amjad
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],Exceptions 16-
Exception Types: ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Exception handling  (II) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Exception Handling ,[object Object],[object Object],[object Object]
Exception Handling in C++ ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The  try  Block ,[object Object],[object Object],[object Object],[object Object]
The  try  Block ,[object Object],[object Object]
The  throw  Point ,[object Object],[object Object],[object Object],[object Object]
The  catch  Blocks ,[object Object],[object Object],[object Object],[object Object],[object Object]
Exception handler: Catch Block ,[object Object],[object Object],[object Object],[object Object]
Catching Exceptions ,[object Object],[object Object]
Exception Handling Procedure ,[object Object],[object Object],[object Object],[object Object],[object Object]
Exception Handling Procedure ,[object Object],[object Object],[object Object],[object Object]
Exception Syntax ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Int main() { Try { Aclass obj1;  Obj1.Func ();  //may cause error } Catch(Aclass::AnError) { //tell user about error; }  Return 0; }
Exception Handling - Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Exception Handling - Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Exception Handling - Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Exception Handling - Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
How it works! ,[object Object],[object Object],[object Object]
Example Discussion ,[object Object],[object Object],[object Object],[object Object]
Exception Syntax ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],int main() { Stack s1; try{  S1.push(11); s1.push(22);  S1.push(33); s1.push(44); } Catch(stack::Range) { Cout<<“Exception: Stack is Full”; }  return 0; }
Nested try Blocks ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],This handler catches the exception in the inner try block This handler catches the exception thrown anywhere in the outer try block as well as uncaught exceptions from the inner try block
[object Object],[object Object],[object Object],Flow of Control 16-
Exception Syntax ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],int pop() { if(top<0) throw Empty(); return st[top--]; } };
Exception Syntax ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Catch(Stack::Empty) { Cout<<“Exception: Stack is Empty; } return  0; }
Exception Exercise: size = 4 Implement the class Box where you store the BALLS thrown by your college. Your program throws the exception  BoxFullException( ) when size reaches to 4.
Introduction to   C++ Templates and Exceptions ,[object Object],[object Object]
C++ Function Templates ,[object Object],[object Object],[object Object],[object Object],[object Object]
Approach 1: Naïve Approach ,[object Object],[object Object],[object Object]
Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],PrintInt (sum); PrintChar (initial); PrintFloat (angle);   To output the traced values, we insert:
Approach 2:Function Overloading (Review) ,[object Object],[object Object],[object Object]
Example of Function Overloading void  Print ( int n ) { cout << &quot;***Debug&quot; << endl; cout << &quot;Value is &quot; << n << endl; } void  Print ( char ch ) { cout << &quot;***Debug&quot; << endl; cout << &quot;Value is &quot; << ch << endl; } void  Print ( float x ) { } Print (someInt); Print (someChar); Print (someFloat); To output the traced values, we insert:
Approach 3: Function Template ,[object Object],Template < TemplateParamList > FunctionDefinition FunctionTemplate TemplateParamDeclaration: placeholder   class  typeIdentifier  typename variableIdentifier
Example of a Function Template   template<class SomeType> void Print( SomeType val ) { cout << &quot;***Debug&quot; << endl; cout << &quot;Value is &quot; << val << endl; } Print<int>(sum); Print<char>(initial); Print<float>(angle);  To output the traced values, we insert: Template parameter (class, user defined type, built-in types) Template   argument
Instantiating a Function Template ,[object Object],Function  <  TemplateArgList  > (FunctionArgList) TemplateFunction Call
A more complex example   template<class T> void sort(vector<T>& v) { const size_t n = v.size(); for (int gap=n/2; 0<gap; gap/=2) for (int i=gap; i<n; i++) for (int j=i-gap; 0<j; j-=gap) if (v[j+gap]<v[j]) { T temp = v[j]; v[j] = v[j+gap]; v[j+gap] = temp; } }
Summary of Three Approaches Naïve Approach Different Function Definitions Different Function Names Function Overloading Different Function Definitions Same Function Name Template Functions One Function Definition (a function template) Compiler Generates Individual Functions
Class Template ,[object Object],Template < TemplateParamList > ClassDefinition Class Template TemplateParamDeclaration: placeholder     class  typeIdentifier    typename variableIdentifier
Example of a Class Template template<class ItemType> class GList { public: bool IsEmpty() const; bool IsFull() const; int  Length() const; void Insert( /* in */  ItemType  item ); void Delete( /* in */  ItemType  item ); bool IsPresent( /* in */  ItemType  item ) const; void SelSort(); void Print() const; GList();  // Constructor private: int  length; ItemType  data[MAX_LENGTH]; }; Template   parameter
Instantiating a Class Template ,[object Object],[object Object],[object Object]
Instantiating a Class Template  // Client code   GList<int> list1; GList<float> list2; GList<string> list3;   list1.Insert(356); list2.Insert(84.375); list3.Insert(&quot;Muffler bolt&quot;); To create lists of different data types GList_int list1; GList_float list2; GList_string list3; template argument Compiler generates 3 distinct class types
Substitution Example class GList_int { public: void Insert( /* in */ ItemType item ); void Delete( /* in */ ItemType item ); bool IsPresent( /* in */ ItemType item ) const; private: int  length; ItemType data[MAX_LENGTH]; }; int int int int
Function Definitions for Members of a Template Class template<class ItemType> void GList< ItemType >::Insert( /* in */  ItemType  item ) { data[length] = item; length++; } //after substitution of float void GList< float >::Insert( /* in */  float  item ) { data[length] = item; length++; }
Another Template Example: passing two parameters ,[object Object],[object Object],[object Object],[object Object],[object Object],non-type parameter
Standard Template Library ,[object Object],[object Object],[object Object]
What’s in STL? ,[object Object],[object Object]
Vector ,[object Object],[object Object],[object Object],[object Object],[object Object]
Example of vectors //  Instantiate a vector vector<int> V; //  Insert elements V.push_back(2); // v[0] == 2 V.insert(V.begin(), 3); // V[0] == 3, V[1] == 2 //  Random access V[0] = 5; // V[0] == 5 //  Test the size int size = V.size(); // size == 2
Take Home Message ,[object Object],[object Object],[object Object]
Suggestion  ,[object Object],[object Object]

Mais conteúdo relacionado

Mais procurados

Inline function
Inline functionInline function
Inline functionTech_MX
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
String and string buffer
String and string bufferString and string buffer
String and string bufferkamal kotecha
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Janki Shah
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Garbage collection
Garbage collectionGarbage collection
Garbage collectionSomya Bagai
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVASURIT DATTA
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)MD Sulaiman
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in javaGoogle
 

Mais procurados (20)

Inline function
Inline functionInline function
Inline function
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Break and continue
Break and continueBreak and continue
Break and continue
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Control statements
Control statementsControl statements
Control statements
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Exception handling
Exception handlingException handling
Exception handling
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 

Destaque

Exception handling in c++ by manoj vasava
Exception handling in c++ by manoj vasavaException handling in c++ by manoj vasava
Exception handling in c++ by manoj vasavaManoj_vasava
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++Deepak Tathe
 
Exception Handling
Exception HandlingException Handling
Exception HandlingAlpesh Oza
 
Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]ppd1961
 
Exception handling in c
Exception handling in cException handling in c
Exception handling in cMemo Yekem
 
Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2ppd1961
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMSfawzmasood
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingfarhan amjad
 
Improving The Quality of Existing Software
Improving The Quality of Existing SoftwareImproving The Quality of Existing Software
Improving The Quality of Existing SoftwareSteven Smith
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ AdvancedVivek Das
 
Exception handler
Exception handler Exception handler
Exception handler dishni
 
Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...
Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...
Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...Complement Verb
 
exception handling in cpp
exception handling in cppexception handling in cpp
exception handling in cppgourav kottawar
 
Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ ConstructorSomenath Mukhopadhyay
 

Destaque (20)

Exception handling in c++ by manoj vasava
Exception handling in c++ by manoj vasavaException handling in c++ by manoj vasava
Exception handling in c++ by manoj vasava
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]
 
Exception handling in c
Exception handling in cException handling in c
Exception handling in c
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2
 
Idiomatic C++
Idiomatic C++Idiomatic C++
Idiomatic C++
 
The Style of C++ 11
The Style of C++ 11The Style of C++ 11
The Style of C++ 11
 
Distributed Systems Design
Distributed Systems DesignDistributed Systems Design
Distributed Systems Design
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Improving The Quality of Existing Software
Improving The Quality of Existing SoftwareImproving The Quality of Existing Software
Improving The Quality of Existing Software
 
05 c++-strings
05 c++-strings05 c++-strings
05 c++-strings
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
 
Exception handler
Exception handler Exception handler
Exception handler
 
Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...
Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...
Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...
 
Web Service Basics and NWS Setup
Web Service  Basics and NWS SetupWeb Service  Basics and NWS Setup
Web Service Basics and NWS Setup
 
exception handling in cpp
exception handling in cppexception handling in cpp
exception handling in cpp
 
Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ Constructor
 

Semelhante a Exception handling and templates

Exception handling
Exception handlingException handling
Exception handlingWaqas Abbasi
 
12. Exception Handling
12. Exception Handling 12. Exception Handling
12. Exception Handling Intro C# Book
 
Exception Handling
Exception HandlingException Handling
Exception Handlingbackdoor
 
Exception handling
Exception handlingException handling
Exception handlingIblesoft
 
Exception Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLRException Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLRKiran Munir
 
Exception handling
Exception handlingException handling
Exception handlingzindadili
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptionssaman Iftikhar
 
Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVARajan Shah
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentrohitgudasi18
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertionRakesh Madugula
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handlingIntro C# Book
 
Exception Handling.pdf
Exception Handling.pdfException Handling.pdf
Exception Handling.pdflsdfjldskjf
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaPratik Soares
 

Semelhante a Exception handling and templates (20)

Exception handling
Exception handlingException handling
Exception handling
 
12. Exception Handling
12. Exception Handling 12. Exception Handling
12. Exception Handling
 
$Cash
$Cash$Cash
$Cash
 
$Cash
$Cash$Cash
$Cash
 
Java unit3
Java unit3Java unit3
Java unit3
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLRException Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLR
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
 
Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVA
 
17 exceptions
17   exceptions17   exceptions
17 exceptions
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application development
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
 
Exception Handling.pdf
Exception Handling.pdfException Handling.pdf
Exception Handling.pdf
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
java.pptx
java.pptxjava.pptx
java.pptx
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 

Último

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterMateoGardella
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 

Último (20)

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 

Exception handling and templates

  • 1. Exception Handling In worst case there must be emergency exit Lecture slides by: Farhan Amjad
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28. Exception Exercise: size = 4 Implement the class Box where you store the BALLS thrown by your college. Your program throws the exception BoxFullException( ) when size reaches to 4.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34. Example of Function Overloading void Print ( int n ) { cout << &quot;***Debug&quot; << endl; cout << &quot;Value is &quot; << n << endl; } void Print ( char ch ) { cout << &quot;***Debug&quot; << endl; cout << &quot;Value is &quot; << ch << endl; } void Print ( float x ) { } Print (someInt); Print (someChar); Print (someFloat); To output the traced values, we insert:
  • 35.
  • 36. Example of a Function Template   template<class SomeType> void Print( SomeType val ) { cout << &quot;***Debug&quot; << endl; cout << &quot;Value is &quot; << val << endl; } Print<int>(sum); Print<char>(initial); Print<float>(angle); To output the traced values, we insert: Template parameter (class, user defined type, built-in types) Template argument
  • 37.
  • 38. A more complex example   template<class T> void sort(vector<T>& v) { const size_t n = v.size(); for (int gap=n/2; 0<gap; gap/=2) for (int i=gap; i<n; i++) for (int j=i-gap; 0<j; j-=gap) if (v[j+gap]<v[j]) { T temp = v[j]; v[j] = v[j+gap]; v[j+gap] = temp; } }
  • 39. Summary of Three Approaches Naïve Approach Different Function Definitions Different Function Names Function Overloading Different Function Definitions Same Function Name Template Functions One Function Definition (a function template) Compiler Generates Individual Functions
  • 40.
  • 41. Example of a Class Template template<class ItemType> class GList { public: bool IsEmpty() const; bool IsFull() const; int Length() const; void Insert( /* in */ ItemType item ); void Delete( /* in */ ItemType item ); bool IsPresent( /* in */ ItemType item ) const; void SelSort(); void Print() const; GList(); // Constructor private: int length; ItemType data[MAX_LENGTH]; }; Template parameter
  • 42.
  • 43. Instantiating a Class Template // Client code   GList<int> list1; GList<float> list2; GList<string> list3;   list1.Insert(356); list2.Insert(84.375); list3.Insert(&quot;Muffler bolt&quot;); To create lists of different data types GList_int list1; GList_float list2; GList_string list3; template argument Compiler generates 3 distinct class types
  • 44. Substitution Example class GList_int { public: void Insert( /* in */ ItemType item ); void Delete( /* in */ ItemType item ); bool IsPresent( /* in */ ItemType item ) const; private: int length; ItemType data[MAX_LENGTH]; }; int int int int
  • 45. Function Definitions for Members of a Template Class template<class ItemType> void GList< ItemType >::Insert( /* in */ ItemType item ) { data[length] = item; length++; } //after substitution of float void GList< float >::Insert( /* in */ float item ) { data[length] = item; length++; }
  • 46.
  • 47.
  • 48.
  • 49.
  • 50. Example of vectors // Instantiate a vector vector<int> V; // Insert elements V.push_back(2); // v[0] == 2 V.insert(V.begin(), 3); // V[0] == 3, V[1] == 2 // Random access V[0] = 5; // V[0] == 5 // Test the size int size = V.size(); // size == 2
  • 51.
  • 52.

Notas do Editor

  1. For most people, the first and most obvious use of templates is to define and use container classes such as string, vector, list and map. Soon after, the need for template functions arises. Sorting an array is a simple example: template &lt;class T&gt; void sort(vector&lt;T&gt; &amp;); void f(vector&lt;int&gt;&amp; vi, vector&lt;string&gt;&amp; vs) { sort(vi); sort(vs); } template&lt;class T&gt; void sort(vector&lt;T&gt; &amp;v) { const size_t n = v.size(); for (int gap=n/2; 0 &lt; gap; gap /= 2) for (int I = gap; I &lt; n; i++) for (j = I – gap; 0 &lt;= j; j-=gap) if (v[j+gap]&lt;v[j]) { T temp = v[j]; v[j] = v[i]; v[j+gap] = temp; } }
  2. Shell sort
  3. Template &lt;class ItemType&gt; says that ItemType is a type name, it need not be the name of a class. The scope of ItemType extends to the end of the declaration prefixed by template &lt;class ItemType&gt;
  4. The process of generating a class declaration from a template class and a template argument is often called template instantiation. Similarly, a function is generated (“instantiated”) from a template function plus a template argument. A version of a template for a particular template argument is called a specialization.
  5. The name of a class template followed by a type bracketed by &lt;&gt; is the name of a class (as defined by the template) and can be used exactly like other class names.
  6. A template can take type parameters, parameters of ordinary types such as ints, and template parameters. For example, simple and constrained containers such as Stack or Buffer can be important where run-time efficiency and compactness are paramount. Passing a size as a template argument allows Stack’s implementer to avoid free store use. An integer template argument must be constant
  7. STL , is a C++ library of container classes, algorithms, and iterators; it provides many of the basic algorithms and data structures of computer science. The STL is a generic library, meaning that its components are heavily parameterized: almost every component in the STL is a template.
  8. Like many class libraries, the STL includes container classes: classes whose purpose is to contain other objects. The STL includes the classes vector , list , deque , set , multiset , map , multimap , hash_set , hash_multiset , hash_map , and hash_multimap . A list is a doubly linked list. That is, it is a Sequence that supports both forward and backward traversal, and (amortized) constant time insertion and removal of elements at the beginning or the end. A deque is very much like a vector: like vector, it is a sequence that supports random access to elements, constant time insertion and removal of elements at the end of the sequence, and linear time insertion and removal of elements in the middle Set is a c ontainer that stores objects in a sorted manner. Map is also a set, the only difference is that in a map, each element must be assigned a key.