Unit-V
Unit-V
Unit-V
Catch − A program catches an exception with an exception handler at the place in a program
where you want to handle the problem. The catch keyword indicates the catching of an
exception.
Syntax
try {
// protected code
} catch( ExceptionName e1 ) {
// catch block
} catch( ExceptionName e2 ) {
// catch block
} catch( ExceptionName eN ) {
// catch block
Throw Exception
if( b == 0 ) {
return (a/b);
}
Catch Exception
try {
// protected code
} catch( ExceptionName e ) {
Example
if( b == 0 ) {
return (a/b);
int main () {
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
return 0;
}
Standard Exception
Template
It allows you to define the generic classes and generic functions and thus provides support for
generic programming. Generic programming is a technique where generic types are used as
parameters in algorithms so that they can work for a variety of data types.
Templates can be represented in two ways:
Function templates
Class templates
Function Template
Generic functions use the concept of a function template. Generic functions define
a set of operations that can be applied to the various types of data.
The type of the data that the function will operate on depends on the type of the
data passed as a parameter.
For example, Quick sorting algorithm is implemented using a generic function, it
can be implemented to an array of integers or array of floats.
A Generic function is created by using the keyword template. The template
defines what function will do.
Syntax:
template < class Ttype> ret_type func_name(parameter_list)
{
// body of function.
}
#include <iostream>
using namespace std;
template<class T> T add(T &a,T &b)
{
T result = a+b;
return result;
}
int main()
{
int i =2;
int j =3;
float m = 2.3;
float n = 1.2;
cout<<"Addition of i and j is :"<<add(i,j);
cout<<'\n';
cout<<"Addition of m and n is :"<<add(m,n);
return 0;
}
O/P
Addition of i and j is :5
Addition of m and n is :3.5
#include <iostream>
using namespace std;
template<class X,class Y> void fun(X a,Y b)
{
std::cout << "Value of a is : " <<a<< std::endl;
std::cout << "Value of b is : " <<b<< std::endl;
}
int main()
{
fun(15,12.3);
return 0;
}
O/P
Value of a is : 15
Value of b is : 12.3
O/P:
Value of a is : 10
Value of b is : 20
Value of c is : 30.5
CLASS TEMPLATE
template<class Ttype>
class class_name
{
.
.
}
#include <iostream>
using namespace std;
template<class T>
class A
{
public:
T num1 = 5;
T num2 = 6;
void add()
{
std::cout << "Addition of num1 and num2 : " << num1+num2<<std::endl;
}
};
int main()
{
A<int> d;
d.add();
return 0;
}
O/P:
Addition of num1 and num2 : 11
#include <iostream>
using namespace std;
template<class T1, class T2>
class A
{
T1 a;
T2 b;
public:
A(T1 x,T2 y)
{
a = x;
b = y;
}
void display()
{
std::cout << "Values of a and b are : " << a<<" ,"<<b<<std::endl;
}
};
int main()
{
A<int,float> d(5,6.5);
d.display();
return 0;
}
O/P:
Values of a and b are : 5,6.5
STREAM CLASSES
Stream classes in C++ are used to input and output operations on files and io devices. These
classes have specific features and to handle input and output of the program.
The iostream.h library holds all the stream classes in the C++ programming language.
FILE HANDLING
File handling is used to store data permanently in a computer. Using file handling we can
store our data in secondary memory.
STEP 1-Naming a file
STEP 2-Opening a file
STEP 3-Writing data into the file
STEP 4-Reading data from the file
STEP 5-Closing a file.
1. ios:-
ios stands for input output stream.
This class is the base class for other classes in this class hierarchy.
This class contains the necessary facilities that are used by all the other derived
classes for input and output operations.
2. istream:-
istream stands for input stream.
This class is derived from the class ‘ios’.
This class handle input stream.
The extraction operator(>>) is overloaded in this class to handle input streams from
files to the program execution.
This class declares input functions such as get(), getline() and read().
3. ostream:-
ostream stands for output stream.
This class is derived from the class ‘ios’.
This class handle output stream.
The insertion operator(<<) is overloaded in this class to handle output streams to files
from the program execution.
This class declares output functions such as put() and write().
4. streambuf:-
This class contains a pointer which points to the buffer which is used to manage the
input and output streams.
5. fstreambase:-
This class provides operations common to the file streams. Serves as a base for
fstream, ifstream and ofstream class.
This class contains open() and close() function.
6. ifstream:-
This class provides input operations.
It contains open() function with default input mode.
Inherits the functions get(), getline(), read(), seekg() and tellg() functions from the
istream.
7. ofstream:-
This class provides output operations.
It contains open() function with default output mode.
Inherits the functions put(), write(), seekp() and tellp() functions from the ostream.
8. fstream:-
This class provides support for simultaneous input and output operations.
Inherits all the functions from istream and ostream classes through iostream.
9. filebuf:-
Its purpose is to set the file buffers to read and write.
We can also use file buffer member function to determine the length of the file.
// Driver Code
int main()
{
// Creation of ofstream class object
ofstream fout;
string line;
return 0;
}