SlideShare a Scribd company logo
1 of 7
Templates and Exceptions:
Chapter 14:
Ms. Munazza Mah Jabeen
Assistant Professor of Computer Science
Function Templates:
int abs(int n) //absolute value of ints
{ return (n<0) ? -n : n; //if n is negative, return –n }
long abs(long n) //absolute value of longs
{ return (n<0) ? -n : n; }
float abs(float n) //absolute value of floats
{ return (n<0) ? -n : n; }
A Simple Function Template:
• Function Template Syntax
• Template argument
• Instantiating
Function Templates with Multiple Arguments:
• Template Arguments Must Match
• Syntax Variation
• More Than One Template Argument
• Why Not Macros?
#define abs(n) ( (n<0) ? (-n) : (n) )
Class Templates:
The UML and Templates:
• Templates (also called
parameterized classes in the
UML)
• Dependencies in the UML
It is a relationship between two
elements such that a change in the
independent one may cause a
change in the dependent one
• Stereotypes in the UML
is a way of specifying additional
detail about a UML element e.g.
<<bind>>
Exceptions:
• Exception Syntax
• Sequence of Events:
• Code is executing normally outside
a try block.
• Control enters the try block.
• A statement in the try block causes
an error in a member function.
• The member function throws an
exception.
• Control transfers to the exception
handler (catch block) following the
try block.
• Multiple Exceptions

More Related Content

More from Munazza-Mah-Jabeen

More from Munazza-Mah-Jabeen (20)

Files and Exceptions
Files and ExceptionsFiles and Exceptions
Files and Exceptions
 
Functions
FunctionsFunctions
Functions
 
Pointers
PointersPointers
Pointers
 
Repitition Structure
Repitition StructureRepitition Structure
Repitition Structure
 
Inheritance
InheritanceInheritance
Inheritance
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Memory Management
Memory ManagementMemory Management
Memory Management
 
Arrays and Strings
Arrays and StringsArrays and Strings
Arrays and Strings
 
Objects and Classes
Objects and ClassesObjects and Classes
Objects and Classes
 
Functions
FunctionsFunctions
Functions
 
Structures
StructuresStructures
Structures
 
Loops and Decisions
Loops and DecisionsLoops and Decisions
Loops and Decisions
 
C++ programming basics
C++ programming basicsC++ programming basics
C++ programming basics
 
The Big Picture
The Big PictureThe Big Picture
The Big Picture
 
Introduction To operating System:
Introduction To operating System:Introduction To operating System:
Introduction To operating System:
 
Chapter 1 - Overview of C
Chapter 1 - Overview of CChapter 1 - Overview of C
Chapter 1 - Overview of C
 
Case Study 2: WINDOWS VISTA
Case Study 2: WINDOWS VISTACase Study 2: WINDOWS VISTA
Case Study 2: WINDOWS VISTA
 
Multiprocessor Systems
Multiprocessor SystemsMultiprocessor Systems
Multiprocessor Systems
 
Case Study 1: Linux
Case Study 1: LinuxCase Study 1: Linux
Case Study 1: Linux
 
Multimedia
MultimediaMultimedia
Multimedia
 

Recently uploaded

Recently uploaded (20)

Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

Templates and Exceptions

  • 1. Templates and Exceptions: Chapter 14: Ms. Munazza Mah Jabeen Assistant Professor of Computer Science
  • 2. Function Templates: int abs(int n) //absolute value of ints { return (n<0) ? -n : n; //if n is negative, return –n } long abs(long n) //absolute value of longs { return (n<0) ? -n : n; } float abs(float n) //absolute value of floats { return (n<0) ? -n : n; }
  • 3. A Simple Function Template: • Function Template Syntax • Template argument • Instantiating
  • 4. Function Templates with Multiple Arguments: • Template Arguments Must Match • Syntax Variation • More Than One Template Argument • Why Not Macros? #define abs(n) ( (n<0) ? (-n) : (n) )
  • 6. The UML and Templates: • Templates (also called parameterized classes in the UML) • Dependencies in the UML It is a relationship between two elements such that a change in the independent one may cause a change in the dependent one • Stereotypes in the UML is a way of specifying additional detail about a UML element e.g. <<bind>>
  • 7. Exceptions: • Exception Syntax • Sequence of Events: • Code is executing normally outside a try block. • Control enters the try block. • A statement in the try block causes an error in a member function. • The member function throws an exception. • Control transfers to the exception handler (catch block) following the try block. • Multiple Exceptions