Managing Console I/O Operations: Unit - Iv
Managing Console I/O Operations: Unit - Iv
UNIT – IV
4. MANAGING CONSOLE I/O OPERATIONS
4.1.2 C++ Streams:
A stream is a sequence of bytes. It acts either as a source from which the
input data can be obtained or as a destination to which the output data
can be sent.
Input stream:
It does read operations through keyboard.
It is uses cin object.
Cin statement uses >> operator before variable name.
Syntax:
Cin >> variable;
Ex:
int weight;
cin >> weight;
Output stream:
It displays the contents of variables on the screen.
It is uses cout object.
cout statement uses << operator before variable name.
Syntax:
Cout << variable;
Ex:
int weight;
cout << weight;
Ex:
#include<iostream.h>
#include<console.h>
Using namespace std;
int main ( )
{
int count = 0;
char c;
UNIT – IV
Managing Console I/O operations
Output:
Input
Object oriented programming
Output
Object oriented programming
Number of characters = 27
Function Task
Width ( ) To specify the required field size for displaying an
output value.
Precision To specify the number of digits to be displayed
() after the decimal point of a float value.
Fill ( ) To specify a character that is used to fill the
unused portion of a field.
Setf ( ) To specify format flags that can control the form of
output display (such as left justification and right
justification).
Unsetf To clear the flags specified.
()
a) Manipulators:
Manipulators are special function that can be included in the I / O
statements to alter the format parameter of a stream.
Some important manipulator functions that are frequently used.
To access these manipulators, the file iomanip should be included
in the program.
Syntax:
Cout.Width(w);
Ex:
Cout.width(5);
Cout<<543<<12<<”\n”;
5 4 3 1 2
The value 543 is printed right – justified in the first columns. The
specification width(5) does not retain the setting for printing the number
12.
Cout.width(5);
Cout<<543;
Cout.width(5);
Cout<<12<<”\n”;
5 4 3 1 2
Ex:
#include<<iostream.h>
Using namespace std;
int main ( )
{
int item[4] = {10, 8, 12, 15};
int cost[4] = {75, 100, 60, 99};
cout.width(5);
cout<<”ITEM”;
cout.width(8);
cout<<”COST”;
cout.width(15);
cout<<”TOTAL VALUE”<<”\n”;
UNIT – IV
Managing Console I/O operations
int sum = 0;
for(int i = 0; i<4; i++)
{
cout.width(5);
cout<<items[i];
cout.width(8);
cout<<cost[i];
int value = item[i] *cost[i];
cout.width(15);
cout<<value<<”\n”;
sum = sum + value;
}
Cout<<”\n Grand Total =”;
cout.width(2);
cout<<sum<<”\n”;
returm 0;
}
OUTPUT:
ITEM COST TOTAL VALUE
10 75 750
8 100 800
12 60 720
15 99 1485
Grand Total = 3755
Syntax:
UNIT – IV
Managing Console I/O operations
Cout.precision(d);
Ex:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double f =3.14159;
cout << setprecision (5) << f << endl;
cout << setprecision (9) << f << endl;
return 0;
}
OUTPUT:
Fill( ) function can be used to fill the unused position by any desired
character.
Syntax:
Cout.fill(ch);
Ex:
Cout.fill(‘*’);
Cout.width(10);
Cout<< 5250 <<”\n”;
OUTPUT
UNIT – IV
Managing Console I/O operations
* * * * * * 5 2 5 0
T A B L E 1 * * * * * * * *
2 5
1 5 . 5
Ex:
UNIT – IV
Managing Console I/O operations
Cout.setf(ios::showpoint);
Cout.setf(ios::showpos);
Cout.precision(3);
Cout.setf(ios::fixed, ios::floatfield);
Cout.setf(ios::internal, ios::adjustfield);
Cout.width(10);
Cout<< 275.5 << “\n”;
OUTPUT:
+ 2 7 5 . 5 0 0
4.2.1 File:
A file is a collection of related data stored in a particular area on the disk.
Programs can be designed to perform the read write operations on these
files.
The following kinds of data communication:
1. Data transfer between the console unit and the program.
2. Data transfer between the program and a disk file.
class functions
It sets the file buffers to read and write.
filebuf Member functions: open( ), close( )
This is the base class for fstream,
fstreambase ifstream and ofstream classes.
Member functions: all input and output
functions, open( ), close( ).
It provides input operations for file.
ifstream Member functions: get( ), getline( ), read(
), seekg( ), tellg( ).
It provide output operations for file.
Ofstream Member functions: put( ), write( ), seekp(
), tellg( )
fstream It is an input – output stream class.
Test.doc
INVENT.ORY
Student
Salary
The input pointer is used for reading the content of given file
location and output pointer is used for writing to a given file
location.
Default action
Open a file in read only mode the input pointer automatically set at
the beginning so that we can read the file from start.
Open file write only mode the input pointer the existing contents are
deleted and the output pointer is set at the beginning.
UNIT – IV
Managing Console I/O operations
Another pair of functions write( ) and read( ) are designed to write and
read block of binary data.
bad( )
good( ). Etc..
4.3 TEMPLATES
4.3.1 Introduction
The template is a feature of the c++ programming language that allows
function and classes to operate with generic types.
The c++ standard library provides many useful functions within a
framework of connected templates.
C++ templates come in two flavors:
Functions templates: C++ function templates are those functions which
can handle different data types without separate code for each of them.
Class templates: C++ class templates are used where we have
multiple copies of code for different data types with the same logic.
Class templates:
A singe code segment represents a set of related classes.
- Called parameterized types.
UNIT – IV
Managing Console I/O operations
Ex:
#include<iostream.h>
Using namespace std;
Template<class T1, class T2>
Class Test
{
T1 a;
T2 b;
Public:
Test(T1 x, T2 y)
{
a=x;
b=y;
void show( )
{
Cout<<a <<”and”
};
Int main( )
{
Test<float, int> test1(1.23, 123);
Test<int, char>test2(100,’w’);
UNIT – IV
Managing Console I/O operations
test1.show( );
test2.show( );
return 0;
}
OUTPUT
1.23 and 123
100 and w
Ex:
#include<iostream.h>
Using namespace std
template<class T>
void swap(T&x, T&y)
{
T temp = x;
X = y;
Y = temp;
}
Void fun(int m, int n, float a, float b)
UNIT – IV
Managing Console I/O operations
{
Cout<<”m and n before swap:”<<m <<” “<<n <<”\n”;
Swap(m, n);
Cout<<”m and n after swap:”<<m <<” “<<n <<”\n”;
OUTPUT:
m and n before swap: 100 200
m and n after swap: 200 100
a and b before swap: 11.22 33.439999
a and b after swap: 33.439999 11.22
Template<class T>
returntype classname<T> :: functionname(arglist)
{
// . . . . . .
// function body
// . . . . . .
}
Ex:
// class template . . . . .
template<class T>
class vector
{
T* v;
Int size;
Public:
Vector(int m)
Vector(T* a);
T operator*(vector &y);
};
template<class T>
vector<T> :: vector(int m);
{
V = new T[size = m];
For(int i = 0; i<size; i++)
V[i]; = 0;
}
template<class T>
vector<T> :: vector(T* a);
{
For(int i = 0; i<size; i++)
V[i]; = a[i];
}
template<class T>
T vector<T> :: operator +(int m);
{
T sum = 0;
For(int i = 0; i<size; i++)
Sum += this -> v[i] * y.v[i];
return sum;
}
It is very rare that a program works correctly fist time. It might have
bugs.
The two most common types of bugs are logic errors and syntactic
errors.
• Exceptions:
Statement:
throw (exception);
throw (exception);
– The type indicates the type of exception the catch block handles.
Statement:
catch(type arg)
{
// statements for
// managing exceptions
}
executed.
Statement:
try
{
//try block
}
Catch (type1 arg)
{
//catch block1
}
Catch (type1 arg)
{
//catch block2
}
Catch (typeN arg)
{
//catch blockN
}
One catch statement can catch all exception instead of a certain type
alone.
Statements:
Catch(. . . . )
{
UNIT – IV
Managing Console I/O operations
Throw;
If you want to prevent a function from throwing any exception, you may do so
by making the type-list empty.
Statement:
type function(arg – list) throw (type – list)
{
......
...... function body
......
}
UNIT – IV
Managing Console I/O operations
Ex:
Write a program to use exception handling with constructor and destructor.
#include<iostream.h>
#include<process.h>
Class number
{
float x;
public:
number(float);
number() {};
~number()
{
Cout<<”\n in destructor”;
}
Void operator++(int) //postfix notation
{
X++;
}
Void operator—() //postfix notation
{
--x;
}
Void show()
{
Cout<<”\n x=”<<x;
}
};
number::number(float k)
UNIT – IV
Managing Console I/O operations
{
if (k==0)
Throw number();
else
x=k;
}
Void main()
{
try
{
number N(2.4);
cout<<”\n Before increasing”;
N.show();
cout<<”\n After increasing”;
N++;
N.show();
cout<<”\n After Decrementation”;
--N;
N.show();
number N1(0);
}
Catch (number)
{
cout<<”\n invalid number”;
exit(1);
}
}
OUTPUT:
Before increasing:
X = 2.4
After increasing:
X = 3.4
After Decrementation:
X = 2.4
In destructor
In destructor
Invalid number
UNIT – IV
Managing Console I/O operations
Ex:
Class template
{
.
Public:
Class FLAG{ }; //abstract class FLAG
.
.
};
Complex complex :: operator +(complex c)
{
If (c.x==0 && c.y==0)
throw FLAG( );
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
Catch (complex :: FLAG)
{
Cout<<”Add Zero Exception”;
}
.