SlideShare ist ein Scribd-Unternehmen logo
1 von 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]

Weitere ähnliche Inhalte

Was ist angesagt?

Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++Jayant Dalvi
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handlingNahian Ahmed
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Sameer Rathoud
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaARAFAT ISLAM
 
Operator Overloading & Function Overloading
Operator Overloading & Function OverloadingOperator Overloading & Function Overloading
Operator Overloading & Function OverloadingMeghaj Mallick
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingMoutaz Haddara
 

Was ist angesagt? (20)

Interface in java
Interface in javaInterface in java
Interface in java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Operator Overloading & Function Overloading
Operator Overloading & Function OverloadingOperator Overloading & Function Overloading
Operator Overloading & Function Overloading
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Operators in java
Operators in javaOperators in java
Operators in java
 

Andere mochten auch

Exception Handling
Exception HandlingException Handling
Exception HandlingReddhi Basu
 
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
 

Andere mochten auch (20)

Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception handling
Exception handlingException handling
Exception handling
 
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
 

Ähnlich wie 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
 
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
 
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
 

Ähnlich wie Exception handling and templates (20)

Exception handling
Exception handlingException handling
Exception handling
 
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
 
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++
 
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
 

Kürzlich hochgeladen

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
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
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
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
 

Kürzlich hochgeladen (20)

Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
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
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
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 ...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
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.

Hinweis der Redaktion

  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.