Object Oriented Programming · Unit 7 · 4 hrs
Function Templates and Exception Handling
Exam-focused notes for Function Templates and Exception Handling (Object Oriented Programming, CSC166): what the TU syllabus asks and how it has actually been tested, with 9 solved past questions from this unit.
What this unit covers
- Function templates
- Function templates with multiple arguments
- Class templates
- templates and inheritance
- Exceptional Handling (Try, throw and catch)
- Use of exceptional handling
Exceptional Handling
List any two operators that cannot be overloaded. Write a program to handle the possible exception in taking 'age' as input. Here the exception must be thrown in the input is negative or greater than 200. [5]
The following operators cannot be overloaded in C++: Operator Name ---------------- :: Scope Resolution Operator ?: Ternary (Conditional) Operator Other examples include: . (member access), . (pointer to member), sizeof. --- The program throws an exception ...
Full solved answer →What is exception? Why exception handling is better to use? Explain exception handling with try.... catch by using suitable example. [5]
An exception is an unusual or abnormal event (error condition) that occurs during the execution of a program and disrupts the normal flow of the program's instructions. Examples include division by zero, file not found, invalid memory access, and array inde...
Full solved answer →How can define catch statement that can catch any type of exception? Illustrate the use of multiple catch statement with example. [5]
To catch any type of exception regardless of its type, we use the ellipsis (...) inside the catch statement's parentheses. This acts as a catch-all handler and must be placed as the last catch block. Syntax: The catch(...) block will handle every exception ...
Full solved answer →Explain how exceptions are used for handling C++ error in a systematic and OOP-oriented way with the design that includes multiple exceptions. [5]
Exception handling is a mechanism in C++ that allows a program to detect and respond to runtime errors in a structured, OOP-oriented way. As stated in the notes, it is used "to develop a reliable and robust system" and "prevents the separation of error hand...
Full solved answer →Function templates with multiple arguments
Write a program to implement function template with multiple arguments. [5]
A function template allows writing a generic function that works with different data types. When a template uses more than one parameter, it is called a function template with multiple arguments. Syntax: --- --- --- Template Call T1 T2 Description ---------...
Full solved answer →Class templates
When class templates are useful? How can you define a class that can implement stack with integer as well as sack of strings? Illustrate with example. [5]
Class templates are useful when: - You need to create a class that performs the same operations on different data types (int, float, string, etc.) - You want to avoid code duplication by writing a single generic class instead of separate classes for each da...
Full solved answer →Function templates
What is meant by template? How can you define function template to return maximum of two integers, floats, or characters? Explain with example. [5]
A template is a feature in C++ that allows writing generic programs. A template defines a blueprint for a function or class that can work with any data type. Instead of writing separate functions for each data type (int, float, char, etc.), a single templat...
Full solved answer →Create a function called swaps() that interchanges the values of the two arguments sent to it (pass these arguments by reference), make the function into a template, so it can be used with all numerical data types (char, int, float, and so on), write a main() program to exercise the function with several types. [5]
A function template allows writing a single generic function that works with multiple data types. The compiler automatically generates the appropriate version of the function for each data type used. Syntax: Passing arguments by reference (using &) means th...
Full solved answer →Explain function template overloading with suitable example. [5]
Function template overloading means defining more than one template function with the same name but with different numbers of parameters or different logic, so that the compiler selects the appropriate version based on the arguments passed at the call site....
Full solved answer →Make Unit 7 stick
Practice CSC166 with flashcards & quizzes