RJ OOP Unit 5
RJ OOP Unit 5
C++ exception handling mechanism makes use of three keywords - try, catch and
throw. The try represents the block of statements in which there are chances of
occurring some exceptional conditions.
When exception detected it is thrown using the throw statement. There exists a
block of statements in which the exception thrown is handled appropriately. This
block is called catch block.
For example
#include <iostream>
using namespace std;
int main ()
{
try
{
throw 100; Output
} An expection at 100
catch (int x)
{
cout << “An exception at "<< x << endl;
}
return 0;
}
Q2. explain the exception handaling mechanism in c++ explain by program to
handle "divide by zero"?
#include <iostream>
int main() {
int numerator, denominator, result;
std::cout << "Enter the numerator: ";
std::cin >> numerator;
std::cout << "Enter the denominator: ";
std::cin >> denominator;
try {
if (denominator == 0) {
throw "Divide by zero exception";
}
return 0;
}
--- In this program, the user is asked to enter the numerator and denominator. We
then use a try-catch block to handle the "divide by zero" exception. Inside the try
block, we check if the denominator is zero. If it is, we throw an exception using the
throw statement, passing a string literal as the exception message.
Q3.what is generic programing ? How it is impliment in c++ ??
--- > Generic programming is a programming paradigm that aims to create reusable and
flexible code by abstracting away specific data types and focusing on algorithms and data
structures that can work with different types. It allows you to write code that is
independent of specific data types, increasing code reusability and reducing redundancy.
C++ supports generic programming through a feature called templates. Templates in C++
enable you to create generic classes and functions that can operate on different data types.
#include <iostream>
// Generic function to find the maximum of two values
template <typename T>
T findMax(T a, T b) {
return (a > b) ? a : b;
}
int main() {
int num1 = 10, num2 = 20;
int maxNum = findMax(num1, num2);
std::cout << "Max number: " << maxNum << std::endl;
double value1 = 3.14, value2 = 2.71;
double maxValue = findMax(value1, value2);
std::cout << "Max value: " << maxValue << std::endl;
return 0;
}
In this code, we define a generic function called findMax() using a template. The template
parameter <typename T> represents a placeholder for a type. The function takes two
parameters (a and b) of type T and returns the maximum value among them using the
conditional operator (?:).
Inside the main() function, we demonstrate the use of the generic findMax() function with
both integers and doubles. First, we find the maximum of two integers (num1 and num2),
and then we find the maximum of two doubles (value1 and value2).
By using templates, we can write a single function implementation that works with different
types. The compiler generates the appropriate code for each specific type when the
function is called, allowing for code reuse and flexibility.
Q.4When do we rethrow an Q.5 What is function template ?
exception ? Explain the way of Ans.: To perform identical operations
rethrowing the exception. for each type of data compactly and
conveniently, the function templates
Ans. : When the exception is thrown are used. One can write a single
inside the try block it is propagated to function template definition. Based on
the catch block that immediately the argument types provided in
follows it. But sometimes handler may calls to the function, the compiler
decide to rethrow it to propagate to automatically instantiates separate
next catch statement. In such a object code functions to handle each
Situation the exception can be type of call appropriately. Function
rethrown by simply writing throw templates are implemented like regular
without any argument. functions, except they are prefixed with
#include <iostream> the keyword template.
using namespace std;
void function() template <class T>
{ T min(T a, T b)
try {
{ if (a<b)
throw “myworld”; return a;
} else
catch(char*) return b;
{ }
cout< <"\nInside the catch statement int main()
of function()"; {
throw; //rethrowing the exception cout << “min(10, 20) = " << min(10,
The exception which is rethrown 20) << endl;
is handled here by this catch cout << “min(‘p’, ‘t’) = "<< min(‘p’, ‘t')
block. <<-endl;
cout << “min(10.3, 67.2) = "
function(); <<min(10.3,67.2)<<endl;
} return 0;
catch(char*)
{
cout<<"\n\nInside the catch statement
of main()";
}
return 0;
}
Q.6 What is overloading template ?
Explain it with example.
Ans.: It is possible to overload the function
template by some
non-template function or by template
function.. This non template function
has the same name as the template
function but it is not related to the
template function.
template <class T>
void display(T x)
{
cout << x;
}
void display(int a)
{
cout << "\n Inside the non template
function":
}
int main()
{
cout << "\n Displaying string: ";
display('Hello');
cout << "\n Displaying floating number: ";
display(10,55);
display(10); //calls overloaded non
template function
return 0;
Q7. Distinguish btween overload function anf function templete with sutable example?
--- > Overloaded Functions:
1)Overloaded functions are multiple functions with the same name but different parameter
lists.
2)Each overloaded function can have a different implementation, return type, or number
and types of parameters.
3)The appropriate overloaded function to be called is determined at compile-time based on
the arguments provided.
4)Overloaded functions are resolved based on static or compile-time polymorphism.
Function Templates:
Function templates-: allow you to define a generic function that can operate on multiple
types.
1)The function template serves as a blueprint or a parameterized function definition.
2)When the function template is used, the compiler generates a specific function
implementation based on the provided types.
3)Function templates are resolved based on template or compile-time polymorphism.
#include <iostream>
// Function template to add two values of any #include <iostream>
type // Overloaded function to add two integers
template <typename T> int add(int a, int b) {
T add(T a, T b) { return a + b;
return a + b; }
std::cout << "Sum of integers: " << sum1 << int main() {
std::endl; int sum1 = add(5, 10);
double sum2 = add(3.14, 2.71); std::cout << "Sum of integers: " << sum1 <<
std::endl;
std::cout << "Sum of doubles: " << sum2 <<
std::endl; double sum2 = add(3.14, 2.71);
il) export : The export keyword can be used to export the template definitions to
other files. The keyword export is preceded the template keyword in the template
definition.
For example -
export template <typename T>
void display(T “list, T key, int count);
can be placed in some header file say test.h. This test.h can then be included in the
main application program which uses the templates.
Q9. explain calss templetes using maltiple parameters with the help of
programing ??
--- > Each instance of the class can have its own set of template arguments,
providing flexibility and reusability.
void setFirst(T1 f) { first = f; }
#include <iostream> void setSecond(T2 s) { second = s; }
// Class template with multiple };
parameters
template <typename T1, typename int main() {
T2> // Creating instances of the Pair class
class Pair { with different types
private: Pair<int, double> p1(5, 3.14);
T1 first; Pair<std::string, char> p2("Hello",
T2 second; 'X');