UNIT 4.docx-c++
UNIT 4.docx-c++
1. ios class is topmost class in the stream classes hierarchy. It is the base class for
istream, ostream, and streambuf class.
2. istream and ostream serves the base classes for iostream class. The class istream is
used for input and ostream for the output.
3. Class ios is indirectly inherited to iostream class using istream and ostream. To
avoid the duplicity of data and member functions of ios class, it is declared as virtual
base class when inheriting in istream and ostream as
Syantax
{
};
};
4. The withassign classes are provided with extra functionality for the assignment
operations that’s why _withassign classes.
5. The ios class: The ios class is responsible for providing all input and output
facilities to all other stream classes.
6.The istream class: This class is responsible for handling input stream. It provides
number of function for handling chars, strings and objects such as get, getline, read,
ignore, putback etc..
Example Program
#include <iostream>
using namespace std;
int main()
{
char x;
cout << x;
}
2.C++ stream
C++ comes with libraries which provides us with many ways for performing input and
output. In C++ input and output is performed in the form of a sequence of bytes or
more commonly known as streams.
The most basic stream types are the standard input/output streams: istream cin.
built-in input stream variable; by default hooked to keyboard. ostream cout built-in
output stream variable; by default hooked to console.
● Input Stream: If the direction of flow of bytes is from the device(for example,
Keyboard) to the main memory then this process is called input.
● Output Stream: If the direction of flow of bytes is opposite, i.e. from main
memory to device ( display screen ) then this process is called output.
3.Unformatted I/O
It is a method of cin object used to input a single character from keyboard. But
its main property is that it allows wide spaces and newline character. It is a method of
cout object and it is used to print the specified character on the screen or monitor.
Unformatted Input/Output is the most basic form of
input/output. Unformatted input/output transfers the internal binary representation of
the data directly between memory and the file. Formatted output converts the internal
binary representation of the data to ASCII characters which are written to the output
file
1.void get()
It is a method of cin object used to input a single character from keyboard. But its
main property is that it allows wide spaces and newline character.
Syntax:
char c=cin.get();
2.void put()
It is a method of cout object and it is used to print the specified character on the screen
or monitor.
Syntax:
cout.put(variable / character);
This is a method of cin object and it is used to input a string with multiple spaces.
Syntax:
char x[30];
cin.getline(x,30);
It is a method of cout object. This method is used to read n character from buffer
variable.
Syntax:
cout.write(x,2);
5.cin
Syntax:
cin>>variable / character / String / ;
6.cout
Syntax:
1.width(n)
Syntax:
cout<<setw(int n);
2.fill(char)
Syntax:
cout<<setfill('character')<<variable;
3.precison(n)
Syntax:
cout<<setprecision('int n')<<variable;
4.setflag(arg 1, arg,2)
Syntax:
5. unsetflag(arg 2)
Syntax:
resetiosflags(argument 2);
6.setbase(arg)
Syntax:
setbase(argument);
5.Exception Handling
try {
// protected code
} catch( ExceptionName e1 ) {
// catch block
} catch( ExceptionName e2 ) {
// catch block
} catch( ExceptionName eN ) {
// catch block
Throwing Exceptions
Try
{
// protected code
}
catch()
{
// code to handle ExceptionName exception
}
Example Program
#include <iostream>
if( b == 0 ) {
return (a/b);
int main ()
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
return 0;
Command-line arguments are arguments that are passed to a program when it is executed
from the command line or terminal. They are provided in the command-line shell of
operating systems with the program execution command.
The main function of C++ generally can have the following signature:
int main(){
// Suitable Code
return 0;
But to pass command-line arguments, we typically define main() with two arguments, where
the first argument is the number of command-line arguments and the second is the list of
command-line arguments.
What is argc?
The variable argc (ARGument Count) is an integer that stores the number of command line
arguments passed to the main function. It also includes the count for the name of the
program, so if we pass a value to a program, the value of argc would be 2 (one for argument
and one for program name).
What is argv?
The array argv (ARGument Vector) is an array of C-style strings like ('char*') where every
element points to a command line argument. argv does not store the actual argument, but the
pointer to that argument. The argv[0] will always contain the name of the program.
// arguments
#include <iostream>
int i = 0;
cout << "Argument " << i + 1 << ": " << argv[i]
<< endl;
i++;
return 0;
Input
./program1 hello geeks
Output
The argc is an integer that stores the number of command line arguments.
The argv store is an array of pointers to characters that stores the command line
arguments.
The first element in argv (argv[0]) contains the name of the program itself.
Only string values are passed through the command line argument.
The argv[1] points to the first command line argument and argv[argc-1] points to the
last argument. argv[argc] is NULL.
The following programs illustrate the behaviour of C++ program for different kinds of
command line arguments.
In the following program, we pass three arguments to the main function from the command
line.
#include <iostream>
if (argc >= 2) {
// printing number of arguments
<< endl;
"Arguments Passed----"
<< endl;
<< endl;
return 0;
Terminal Command:
Output:
In C++ program, multiple command line arguments are passed to the function by separating
them by a whitespace but what happens if you have to pass an argument string that already
contains spaces. In such case, we can enclose that string in the double quotes to pass it as a
single argument.
// C++ program to illustrate the spaces seperated string as
// a single argument
#include <iostream>
cout << "Program Name Is: " << argv[0] << endl;
if (argc >= 2) {
<< endl;
"Arguments Passed----"
<< endl;
return 0;
Terminal Command:
Output: