You are on page 1of 14

I/0 in C++

September 14, 2022

I/0 in C++ September 14, 2022 1 / 14


Outline

1 Pre-Defined Streams

2 Stream Classes

3 Scope Access Operator

4 Namespace

5 Memory Management Operators

I/0 in C++ September 14, 2022 2 / 14


Pre-Defined Streams

Pre-Defined Streams
Streams in C++
Stream is a flow of data in bytes in sequence.
If input data is received from input device in sequence is called as
source stream.
When the data is passed to output devices then its called Destination
stream.
Predefined Streams in C++ Predefined streams are those streams that
are embedded in the system by default. These streams get activated, when
the program execution starts.
The predefined streams that are available in C++ are
1 cin
2 cout
3 cerr
4 clog
I/0 in C++ September 14, 2022 3 / 14
Pre-Defined Streams

Pre-Defined Streams II

cin:cin is an input object, which takes input through standard input


device, keyboard. It is as similar to stdin in C.
cout: cout is an output object, which gives output to standard output
device i.e., monitor. It is as similar to stdout in C
cerr:cerr is a standard error object, which is used to manage the errors in
unbuffered data. This object passes the errors that are occurred in
unbuffered data to standard output device i.e., monitor.
clog:clog is often a standard error object, but it manages the errors that
takes place in buffered data.

I/0 in C++ September 14, 2022 4 / 14


Stream Classes

Stream Classes

I/0 in C++ September 14, 2022 5 / 14


Stream Classes

Stream Classes II I

C++ has number of classes that work with console and file operations.
These classes are known as stream classes

ios class is topmost class in the stream classes hierarchy.


It is the base class for istream, ostream, and streambuf class.
istream and ostream serves the base classes for iostream class.The
class istream is used for input and ostream for the output.
Class ios is indirectly inherited to iostream class using istream and
ostream.
The istream withassign class is a variant of istream . The predefined
object cin is an object of this class and thus may be reassigned at run
time to a different istream object. For example, a program that
normally expects input from stdin could be temporarily directed to
accept its input from a disk file.
I/0 in C++ September 14, 2022 6 / 14
Stream Classes

Stream Classes II II

The ostream withassign class is a variant of ostream . The predefined


objects cout, cerr, and clog are objects of this class and thus may be
reassigned at run time to a different ostream object. For example, a
program that normally sends output to stdout could be temporarily
directed to send its output to a disk file.

I/0 in C++ September 14, 2022 7 / 14


Scope Access Operator

Scope access operator

There are many situations when the name of local and global
variables are same. so if programmer wants to access global variable
in block then scope resolution operator is used.
It defines the member function outside of the class using the scope
resolution.
It is used to access the static variable and static function of a class.
The scope resolution operator is used to override function in the
Inheritance.
Example:

1 Program to access global variable


2 Demonstrate the standard namespace using the scope resolution (::)
operator

I/0 in C++ September 14, 2022 8 / 14


Namespace

Namespace

Consider a situation, when we have two persons with the same name
For example, you might be writing some code that has a function
called xyz() and there is another library available which is also having
same function xyz(). Now the compiler has no way of knowing which
version of xyz() function you are referring to within your code.
A namespace is designed to overcome this difficulty and is used as
additional information to differentiate similar functions, classes,
variables etc. with the same name available in different libraries.

I/0 in C++ September 14, 2022 9 / 14


Namespace

Namespace Creation
Syntax
namespace namespacename{
// code declarations i.e. variable (int a;)
method (void add();)
classes ( class student{};)
}
Namespace declarations appear only at global scope.
Namespace declarations can be nested within another namespace.
Namespace declarations don’t have access specifiers (Public or
Private).
No need to give a semicolon after the closing brace of the definition
of namespace.
To call the namespace-enabled version of either a function or a variable
namespacename::code;
I/0 in C++ September 14, 2022 10 / 14
Namespace

Example

#include <iostream>
using namespace std;
// Variable created inside namespace
namespace first
{
int val = 500;
// need of namespace
}
int main()
{
// Global variable
int value;
int val = 100;
value = 0;
int main()
double value; // Error here
{
value = 0.0;
int val = 200;
}
//These variables can be accessed from
//outside the namespace using the
//scope operator ::
cout << first::val << '\n';
return 0;
}

I/0 in C++ September 14, 2022 11 / 14


Namespace

Examples

1 name space entities using functions


2 nested namespace
3 using directive

I/0 in C++ September 14, 2022 12 / 14


Memory Management Operators

Memory Management Operators I


C uses the malloc() and calloc() function to allocate memory
dynamically and uses a free() function to free dynamically allocated
memory. C++ supports these functions
C++ has two operators new and delete , that perform the task of
allocating and freeing the memory in a better and easier way.
new operator: This denotes a request for memory allocation.
If sufficient memory is available, a new operator initializes the memory and
returns the address of the newly allocated and initialized memory to the
pointer variable.
Syntax to use new operator
pointer-variable = new data-type;
Example:
int *p = NULL;
p = new int;
(or)
int *p = new int;
I/0 in C++ September 14, 2022 13 / 14
Memory Management Operators

Memory Management Operators II

delete operator Since it is the programmer’s responsibility to deallocate


dynamically allocated memory, programmers are provided delete operator
in C++ language.
Syntax:
delete pointer-variable;

Examples:

delete p;
delete q;

I/0 in C++ September 14, 2022 14 / 14

You might also like