Unit-V Oop RBK
Unit-V Oop RBK
• Resource Failure
• User Actions
• Erroneous Statement
Need of Exception Handling
• When exception occurs at any statement, the
program shows error message and terminate at
the same statement.
throw demo();
else if (i == 2)
#include<iostream> throw demo2();
using namespace std; }
class demo1 catch (demo1 d1)
{ {
}; cout << "Caught exception of demo1
class demo2 class \n";
{ }
};
int main() catch (demo2 d2)
{ {
cout << "Caught exception of demo2
for (int i = 1; i <= 2; i++)
class \n";
{ }
try }
{ }
if (i == 1)
throw demo1(); Output:
Caught exception of demo1 class
Caught exception of demo2 class
C++ provides a mechanism to ensure that
a given function is limited to throw only a
specified list of exceptions.
An exception specification at the
beginning of any function acts as a
guarantee to the function's caller that
the function will throw only the
exceptions contained in the exception
specification.
Exception specification syntax
A function with no exception
specification allows all exceptions.
A function with an exception specification
that has an empty type_id_list, throw(),
does not allow any exceptions to be
thrown.
Suppose that class A is one of the types in
the type_id_list of an exception
specification of a function.
That function may throw exception objects
of class A, or any class publicly derived
from class A.
When a function with an exception
specification throws an exception that is
not listed in its exception specification,
the C++ run time does the following:
1. The unexpected() function is called.
Constructing an object of
Test Destructing an object of
Test Caught 10
Output:-
Constructing an Object of
Test1 Constructing an Object
of Test2 Destructing an Object
of Test1 Caught 20
If both base and derived classes are caught
as exceptions then catch block of derived
class must appear before the base class.
If we put base class first then the
derived class catch block will never be
reached
catch(base b)
#include<iostream>
{
using namespace std;
cout<<"Caught base exception \n";
class base
}
{
catch(derived d)
};
{
class derived:public base
cout<<"Caught derived exception \n";
{
}
};
return 0;
int main()
}
{
derived d;
try
{
Output:
throw d;
}
Caught base exception
catch(derived d)
#include<iostream>
{
using namespace std;
cout<<"Caught derived exception \n";
class base
}
{
catch(base b)
};
{
class derived:public base
cout<<"Caught base exception \n";
{
}
};
return 0;
int main()
}
{
derived d;
try
{
Output:
throw d;
}
Caught derived exception
TEMPLATE
S
Template: The Power of Templates
Template is a simple and yet very powerful
tool in C++.
The simple idea is to pass data type as a
parameter so that we don’t need to write
the same code for different data types.
For example, a software company may need
sort() for different data types. Rather than
writing and maintaining the multiple codes, we
can write one sort() and pass data type as a
parameter.
C++ adds two new keywords to support
templates: ‘template’ and ‘typename’.
The second keyword can always be
replaced by keyword ‘class’.
Templates are expanded at compiler time.
Compiler does type checking before
template expansion.
Source code contains only function/class,
Compiled code may contain multiple
copies of same function/class.
Data type is mentioned at runtime.
Generic Programming
Templates are the foundation of generic
programming, which involves writing code
in a way that is independent of any
particular type.
In c++ template classes and template
function means of generic programming.
A template is a blueprint or formula for
creating a generic class or a function. The
library containers like iterators and
algorithms are examples of generic
programming and have been developed
using template concept with variety of
datatypes.
Function Templates: We can define a template for a
function. For example, if we have an add() function, we
can create versions of the add function for adding the
int, float or double type values.
Class Template: We can define a template for a class.
For example, a class template can be created for the
array class that can accept the array of various types
such as int array, float array or double array.
A function template works in a similar to
a normal function, with one key
difference.
Non-Template
Template
Template
Non-Template
The function call f(1, 2) could match the
argument types of both the template
function and the non-template function.
The non-template function is called because
a non-template function takes precedence
in overload resolution.
The function call f('a', 'b') can only match
the argument types of the template
function. The template function is called.
Argument deduction fails for the function call
f(1, 'b'); the compiler does not generate any
template function specialization and overload
resolution does not take place.
The non-template function resolves this
function call after using the standard
conversion
from char to int for the function argument 'b'.
Like function templates, class templates
are useful when a class defines something
that is independent of the data type.
template<class T, class U =
char> class A {
public:
T x;
U y;
A() { cout<<"Constructor
Called"<<endl; }
};
int main() {
A<char> a; // This will call
A<char, char>
return 0;
}
A template non-type parameter is a special
type of parameter that does not substitute for
a type, but is instead replaced by a value.
A non-type parameter can be any of
the following:
1. A value that has an integral type
or enumeration
2. A pointer or reference to a class
object
3. A pointer or reference to a
function
4. A pointer or reference to a class
member function
5. std::nullptr_t
Non-type parameters are mainly used
for specifying max or min values or any
other constant value for a particular
instance of a template.
The important thing to note about non-
type parameters is, they must be const.
The compiler must know the value of
non- type parameters at compile time.
Because compiler needs to create
functions/classes for a specified non-
type value at compile time
#include <iostream>
using namespace std;
Example: template<class T, int max>
int arrmin(T arr[], int n)
{
int m=max;
for(int i=0; i<n; i++)
if(arr[i]<m)
m=arr[i];
return m;
Output: }
10 int main()
1 {
int arr1[]={10,20,15,12};
int n1=sizeof(arr1)/sizeof(arr1[0]);
char arr2[]={1,2,3};
int n2=sizeof(arr2)/sizeof(arr2[0]);
cout<<arrmin<int, 10000>(arr1, n1)<<endl;
cout<<arrmin<char, 256>(arr2, n2);
return 0;
}
A template function instantiated with one set of
template arguments may be a friend to one
template class instantiated with the same set
of template arguments. This is also the
relationship between a regular non-template
class and a regular non-template friend
function.
There are four kinds of relationships between
classes and their friends when templates are
involved:
One-to-many: A non-template function may be a
friend to all template class instantiations.
Many-to-one: All instantiations of a template
function may be friends to a regular non-template
class.
One-to-one: A template function instantiated with
one set of template arguments may be a friend to
one template class instantiated with the same set of
template arguments. This is also the relationship
between a regular non-template class and a regular
non-template friend function.
Many-to-many: All instantiations of a template
function may be a friend to all instantiations of the
#include<iostream> void show(test<U> t)
using namespace std; {
template<class T> cout<<"The value of x
class test is:"<<t.x;
{ }
T x; int main()
public: {
void setx() test<int> t;
{ t.setx();
cin>>x; show(t);
} }
template<class U>
friend void show(test<U>t); Output:
}; 5
template<class U> The value of x is:5
The keyword export applies to function
templates, member functions of class
templates, member function templates,
and static data members of class
templates.
export can also be applied to a class
template declaration.
Syntax:-
export template <typename
T> Note:-
keyword export cannot be
combined with inline function.
THANK
YOU !