unit v - mycsvtu notes
TRANSCRIPT
![Page 1: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/1.jpg)
UNIT V
![Page 2: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/2.jpg)
GENERIC PROGRAMMING WITH TEMPLATES
![Page 3: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/3.jpg)
Templates supports generic programming.
Reusable components can be developed.
Template declared for functions
Template declared for class
Function template
Class Template
![Page 4: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/4.jpg)
Function Template
Drawback of a simple function is that they can be used with only a particular data type.
This can be overcome by function template or generic functions.
![Page 5: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/5.jpg)
Syntax of function template
template <class T,……….>
Returntype Functionname(arguments)
{
……………………………………
……………………………………
}
Keyword Template datatype
At least one argument must
be template type
![Page 6: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/6.jpg)
Overriding of function templates Function templates can be overridden by
normal functions.
If the program has both the function and function template with the same name, the compiler first selects the normal function.
![Page 7: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/7.jpg)
Errors while using function template
No-argument template function.
Template-type argument unused.
Usage of partial number of template arguments.
![Page 8: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/8.jpg)
Overloaded Function Template
The function templates can also be overloaded with multiple declarations.
Must differ either in number or type of arguments.
![Page 9: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/9.jpg)
Multiple arguments function template
Multiple generic arguments can also be taken.
![Page 10: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/10.jpg)
User defined template arguments
![Page 11: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/11.jpg)
Class Template
Class can also be declared to work on different data types.
This generic class will support similar operations for different data types.
![Page 12: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/12.jpg)
Syntax of class template
template <class T1,class T2,……>
class ClassName
{
T1 data;
………..
void func1(T1 a, T2 b);
……………..
};
Keyword Template datatypes
T1,T2,….
![Page 13: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/13.jpg)
Syntax for class template instantiation
ClassName <char> object1;
ClassName <int> object2;
Datatype to be substituted for
template datatype
![Page 14: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/14.jpg)
Template arguments
A template can have character strings, function names etc as template type arguments.
Example:
Template <class T1, int size>
Class myClass
{
T arr[size];
};
![Page 15: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/15.jpg)
The object of the class will be created as:
myClass <float,10> new1;
![Page 16: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/16.jpg)
Member function definition outside the class
![Page 17: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/17.jpg)
CONSOLE I/O OPERATION
![Page 18: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/18.jpg)
C++ uses concept of streams and stream classes to implement I/O operation with console and disk files.
![Page 19: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/19.jpg)
C++ Streams
I/O system in C++ supplies an interface to the user that is independent of device being used.
This interface is called streams.
Stream is a sequence of bytes.
![Page 20: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/20.jpg)
Input device
program
Output Device
Input Stream
Output Stream
Extraction
from input
stream
Insertion
into
output
stream
![Page 21: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/21.jpg)
C++ Stream Classes
ios
istream streambuf ostream
iostream
istream_withassign iostream_withassign ostream_withassign
![Page 22: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/22.jpg)
• Base class for istream and ostream
• Declares constants and functions that are necessary for handling formatted input and output operations.
ios
• Inherits ios
• Declares input functions such as get(), getline() and read()
• Conatins overloaded insertion operator
istream
• Inherits ios
• Declares output functions put() and write() ostream
• Inherits ios, istream, ostream
• Contains all the input and output functions iostream
![Page 23: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/23.jpg)
Unformatted I/O operations
put() & get() functions
Handle the single character input//output operations.
getline() & write() functions
Line oriented input/output functions.
![Page 24: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/24.jpg)
Formatted Console I/O Operations Functions
width()
precision()
fill()
setf()
unsetf()
Manipulators
setw()
setprecision()
setfill()
iomanip.h
![Page 25: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/25.jpg)
FILE I/O OPERATION
![Page 26: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/26.jpg)
Data is stored using the concept of files.
A file is a collection of related data stored in a particular area on the disk.
Program contains two type of operations for these files:
Data transfer b/w the console unit and the program.
Data transfer b/w the programs and a disk file.
![Page 27: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/27.jpg)
Data files
External Memory
Program + Data
Internal Memory
Read data
from files Write
data to
files
Console Unit
Put data to
screen Get data from
keyboard screen
Keyboard
![Page 28: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/28.jpg)
program
Input Stream
Output Stream
Disk Files
Read
data
Write
data
Data
input
Data
output
![Page 29: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/29.jpg)
ios
istream streambuf ostream
iostream
ifstream fstream ofstream filebuf
fstreambase
![Page 30: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/30.jpg)
• Base for fstream, ifstream, ofstream.
• Contains open() and close() functions. fstreambase
• Provides input operations.
• Contains open() with default input mode.
• Inherits get(), getline(), read(), seekg() and tellg(). ifstream
• Provide output operations
• Contains open() with default output mode.
• Inherits put(), write(), seekp(), tellp(). ofstream
• Provide support for simultaneous input and output operations.
• Inherits all the functions from istream and ostream through iostream.
fstream
![Page 31: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/31.jpg)
Opening & Closing a File
Opening a file using constructor of the class.
ofstream is used to create output stream.
ifstream is used to create input stream.
Initialize the file object with file name.
Opening a file using open ()
It can be used to open multiple files using the same stream object .
![Page 32: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/32.jpg)
Detecting eof
eof() function can be used. It’s the member function of ios.
Return non-zero value when end-of-file encountered.
![Page 33: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/33.jpg)
File Modes
![Page 34: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/34.jpg)
File pointers and their manipulations Each file has two pointers:
Input pointer (get pointer)
Output pointer (put pointer)
![Page 35: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/35.jpg)
Functions for manipulating file pointers seekg()
seekg(offset, refposition)
seekp()
seekp(offset,refposition)
tellg()
tellp()
ios::beg
ios::cur ios::end
![Page 36: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/36.jpg)
Sequential I/O Operation
put() and get() functions
write() and read() functions:
infile.read((char*) & V, sizeof(v));
outfile.write((char*) & V, sizeof(v));
Reading and writing of class object.
![Page 37: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/37.jpg)
Error handling during file operations A file which we are attempting to open for reading does
not exist.
The fine name used for a new file may already exist.
We may attempt an invalid operation such as reading past the end-of-file.
There may not be any space in the disk for storing more data.
We may use an invalid file name.
We may attempt to perform an operation when the file is not opened for that purpose.
![Page 38: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/38.jpg)
• Non-zero if end-of-file encountered. eof()
• True when I/O operation failed. fail()
• True when unrecoverable error occurred. bad()
• True if no error has occurred. Means all other functions are false. good()
![Page 39: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/39.jpg)
EXCEPTION HANDLING
![Page 40: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/40.jpg)
Two common bugs
Logical error – due to poor understanding of solution & problem procedure.
Syntactical Error – due to poor understanding of language.
![Page 41: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/41.jpg)
Two kinds of exceptions
Synchronous – Errors under control, like out of range index, overflow.
Asynchronous – Errors occurred beyond the control of program.
![Page 42: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/42.jpg)
Steps for error handling
Find the problem( Hit the exception)
Inform that error has occurred(Throw the exception)
Receive the error information (Catch the exception)
Take corrective actions(Handle the exception)
![Page 43: UNIT V - MYcsvtu Notes](https://reader030.vdocuments.site/reader030/viewer/2022040518/624a7064a3bba92d8660bb7c/html5/thumbnails/43.jpg)
Try block
Detects and throws
a exception
catch block
Catches and
handles exception
Exception
Object