Unit 4 Notes
Unit 4 Notes
Templates
Exceptions &
STL
Templates, Exceptions and STL Weightage: 15%
What is template?
Function templates and class templates
Introduction to exception
Try-catch, throw
I like C++ so much
Multiple catch
Catch all
I like
Rethrowing exception
Rupesh sir
Implementing user defined exceptions
Overview and use of Standard Template Library(STL)
1/0
Introduction to Exception
int main()
{
int a,b,c;
cout<<"Enter value a=";
cin>>a;
I like C++ so much
cout<<"Enter value b=";
cin>>b;
c=a/b;
cout<<"answer="<<c;
I like Rupesh sir
}
Output: Output:
Enter value a=5 Enter value a=5
Enter value b=2 Enter value b=0
answer=2 Abnormal Termination occur
Templates, Exceptions and STL 4
Introduction to Exception(Cont…)
try
Syntax:
catch(...) I like C++ so much
{
I like Rupesh sir
//statements for processing all exceptions
}
}
I like Rupesh sir
if(x==1) throw 5.15;
catch(...)
{ Output:
cout<<"Caught an exception\n"; Caught an exception
} Caught an exception
} Caught an exception
}
User defined Exception
We need a single function that will work for int, float, double etc…
Function Class
Template Template
Templates, Exceptions and STL 22
Function Template
Syntax:
template<class Ttype>
template<typename Ttype>
map
STL- Standard Template Library
The C++ STL (Standard Template Library) is a powerful set of C++
template classes to provides general-purpose templatized classes
and functions that implement many popular and commonly used
algorithms and data structures like vectors, lists, queues, and
stacks.
I like C++ so much
There are three core components of STL as follows:
I like
1. Containers Rupesh
(an object to store data)sir
2. Algorithms (procedure to process data)
3. Iterators (pointer object to point elements in container)
IO & File
Management
I/O and File Management Weightage: 15%
Concept of streams
cin and cout objects
C++ stream classes
Unformatted and formatted I/O
Manipulators I like C++ so much
File stream
I like
C++ File stream classes
Rupesh sir
File management functions
File modes
Binary and random Files
File I like
Input C++ so much
Stream
Output
Stream
C++ Program
File
Input stream
Input
device
I likeiostream
C++ so much input/output stream class
Example: output:
output:
cout.precision(6);
cout.fill('*');
cout.width(6);
cout.setf(ios::left,ios::adjustfield);
* * * 5 42 3. 6 4 5 7 5
cout.width(10);
cout.width(6);
cout<<"543";
cout.width(6);
cout<<sqrt(7);
cout<<"543"; output:
cout.fill('#');
cout<<"543"; 5 4 3 # # #
I/O and File Management 10
Flags and bit fields
Format required Flag (arg1) Bit-field (arg2)
Left justified output ios::left ios::adjustfield
Right justified output ios::right ios::adjustfield
Scientific notation ios::scientific ios::floatfield
Fixed point notation
I likeios::fixed
C++ so muchios::floatfield
Decimal base I likeios::dec
Rupesh sir ios::basefield
Octal base ios::oct ios::basefield
Hexadecimal base ios::hex ios::basefield
setf(arg1, arg2)
arg-1: one of the formatting flags.
arg-2: bit field specifies the group to which the formatting flag belongs.
I/O and File Management 11
Manipulators for formatted I/O operations
Manipulators are special functions that can be included in the I/O
statements to alter the format parameters of a stream.
To access manipulators, the file <iomanip> should be included in
the program.
Function I like C++
Manipulator Meaningso much
width() setw() Set the field width.
precision()
I like
setprecision()
Rupesh sir
Set the floating point precision.
fill() setfill() Set the fill character.
setf() setiosflags() Set the format flag.
unsetf() resetiosflags() Clear the flag specified.
“\n” endl Insert a new line and flush stream.
Input stream
read data data
input
I like C++ so much
Disk Files Program
I like Rupesh
Output stream sir data
output
write data
File input output streams
fstream
I like Rupesh
ifstream fstream sir
ofstream filebuf
file
fstream base
filebuf Its purpose is to set the file buffers to read and write.
Read contentI of
like Rupesh
file using sir
ifstream object
rcv>>name; rcv.getline(name);
ofstream send;
3 send.open("abc.txt",ios::out); //open()
function with mode
I/O and File Management 22
File opening modes
Parameter Meaning
ios :: in Open file for reading only
ios :: out Open file for writing only
ios :: app Append to end-of-file
ios :: ate I likeGoC++ so much
to end-of-file on opening
ios :: binary Binary file
ios :: trunc
I likeDelete
Rupesh sir
content of file if exists
ios :: nocreate Open fails if the file does not exists
ios :: noreplace Open fails if the file already exists
cout<<product<<endl;
cout<<price;
}
File handling Program
Write a program that opens two text files for reading data.
It creates a third file that contains the text of first file and then
that of second file
(text of second file to be appended after text of the first file, to
I like C++ so much
produce the third file).
ifstream rcv;
I like Rupesh sir
ofstream send;
Function Meaning
ios::beg Ioffset
likecounted
C++fromsothemuch
beginning of the stream
ios::cur Ioffset
like Rupesh
counted
stream pointer
sir
from the current position of the
fstream file;
file.open("stock.txt",ios::in | ios::app);
ob1.readdata();
file.write((char *)&ob1,sizeof(ob1));
file.read((char *)&ob1,sizeof(ob1));
ob1.displaydata();
file.close();
}
Thank You