Object Oriented Programming With C++: Click To Add Text

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 17

Object Oriented

Programming With C++

Click to add Text


Managing console
I/O operations
Click to add Text
Stream
 Stream is an interface to the programmers
that is independent of the actual device
being accessed.
 A stream is a sequence of bytes which can
act either as a source from which data can
be obtained or a destination to which
output data can be sent.
Example
IOS
Program

istream ostream
stream

Device
iostream
Character manipulation
char c;
cin.get(c); //it can read ‘\n’ and blankspace
cout.put(c);
cout.put(65); // prints A
String manipulation
char st[30];
cin.getline(st,6); // c++ is fun
cout<<st<<endl; // c++ i (including the terminating null character)
strcpy(st,”bangla”);
cout.write(st,4); //bang
cout.write(st,10); //bangla_ _ _ _
cout<<st<<endl; // bangla
Output formatting
 C++ supports 3 ways of formatting
1. ios class functions & flags.
2. Manipulator
3. User defined output functions / manipulators.
ios class format function: width

int main() Output:


{ _ _ _5252
int x=52; ***52
cout.width(5);
cout<<x<<x<<endl;
cout.width(5);
cout.fill('*');
cout<<x<<endl;
return 0;
}
ios class format function: Precision
int main() Output:
{ 15.32
double f=15.317451; 15.32
cout.precision(4); 15.3175
cout<<f<<endl; 15
cout<<f<<endl;
cout.setf(ios::fixed,ios::floatfield);
cout.precision(4);
cout<<f<<endl;
cout.precision(0);
cout<<f<<endl;

return 0;
}
ios class format function: setf
int main() Output:
{ +_ _275.500
//display trailing zero
cout.setf(ios::showpoint);
// display + sign
cout.setf(ios::showpos);
cout.precision(3);
//fixed point notation
cout.setf(ios::fixed, ios::floatfield);
//padding after sign
cout.setf(ios::internal,ios::adjustfield);
cout.width(10);
cout<<275.5<<endl;
return 0;
}
Unsetting format flag
#include <iostream>
using namespace std;

int main()
{
cout.setf(ios::uppercase | ios::scientific);
cout << 100.12; // displays 1.001200E+02
cout.unsetf(ios::uppercase); // clear uppercase
cout << " \n" << 100.12; // displays 1.001200e+02
return 0;
}
Manipulator
 Manipulators are special functions that can
be included in the I/O statements.
 The header file iomanip contains
manipulator functions.
Manipulator Example
#include<iostream> Output:
#include<iomanip> _ _ _52
using namespace std;

int main()
{
int x=52;
cout<<setw(5)<<x<<endl;
return 0;
}
Manipulator Example
int main()
{
cout.fill(‘*’)
cout<<setw(5)<<setprecision(2)<<2.1678<<endl
<<setw(10) <<setprecision(4) <<2.1678<<endl
<<setw(15)<< setiosflags(ios::scientific)<<2.1678<<endl;
return 0;
} Output:
**2.2
*****2.168
****2.1678e+000
Manipulator / ios member
 There is an important difference between
manipulator and ios member function.
 The ios member function return the previous
format state which can be saved and stored
later on. This cannot be done with
manipulators.

Example:
cout.precision(2);
int p = cout.precision(4);
cout.precision(p);
Writing user defined manipulator
#include<iostream> Output:
#include<iomanip>
using namespace std; ###52

ostream & mymanip(ostream &dout)


{
dout.width(5);
dout.fill('#');
return dout;
}
int main()
{
int x=52;
cout<<mymanip<<x<<endl;
return 0;
}
Exercise
1. Write down a program which reads lines of
input from keyboard until you enter a blank line.
After reading the lines the program will tell how
many characters, words and sentences were there
in the input.

You might also like