Unit-V

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Exception

Exception is a problem that arises during the execution of a program.


Exceptions provide a way to transfer control from one part of a program to another. C++
exception handling is built upon three keywords: try, catch, and throw.
Try − A try block identifies a block of code for which particular exceptions will be activated.
It's followed by one or more catch blocks.
Throw − A program throws an exception when a problem shows up. This is done using
a throw keyword.

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

double division(int a, int b) {

if( b == 0 ) {

throw "Division by zero condition!";

return (a/b);

}
Catch Exception

try {

// protected code

} catch( ExceptionName e ) {

// code to handle ExceptionName exception

Example

double division(int a, int b) {

if( b == 0 ) {

throw "Division by zero condition!"; }

return (a/b);

int main () {

int x = 50;

int y = 0;

double z = 0;

try {

z = division(x, y);

cout << z << endl;

} catch (const char* msg) {

cerr << msg << endl;

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

Function Templates with Multiple Parameters


template<class T1, class T2,.....>
return_type function_name (arguments of type T1, T2....)
{
// body of function.
}

#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

Overloading a Function Template


#include <iostream>
using namespace std;
template<class X> void fun(X a)
{
std::cout << "Value of a is : " <<a<< std::endl;
}
template<class X,class Y> void fun(X b ,Y c)
{
std::cout << "Value of b is : " <<b<< std::endl;
std::cout << "Value of c is : " <<c<< std::endl;
}
int main()
{
fun(10);
fun(20,30.5);
return 0;
}

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

CLASS TEMPLATE WITH MULTIPLE PARAMETERS


template<class T1, class T2, ......>
class class_name
{
// Body of the class.
}

#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.

ofstream: Stream class to write on files


ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.
/* File Handling with C++ using ifstream & ofstream class object*/
/* To write the Content in File*/
/* Then to read the content of file*/
#include <iostream>

/* fstream header file for ifstream, ofstream,


fstream classes */
#include <fstream>

using namespace std;

// Driver Code
int main()
{
// Creation of ofstream class object
ofstream fout;

string line;

// by default ios::out mode, automatically deletes


// the content of file. To append the content, open in ios:app
// fout.open("sample.txt", ios::app)
fout.open("sample.txt");

// Execute a loop If file successfully opened


while (fout) {

// Read a Line from standard input


getline(cin, line);
// Press -1 to exit
if (line == "-1")
break;

// Write line in file


fout << line << endl;
}

// Close the File


fout.close();

// Creation of ifstream class object to read the file


ifstream fin;

// by default open mode = ios::in mode


fin.open("sample.txt");

// Execute a loop until EOF (End of File)


while (getline(fin, line)) {

// Print line (read from file) in Console


cout << line << endl;
}

// Close the file


fin.close();

return 0;
}

You might also like