Professional Documents
Culture Documents
1 Pre-Defined Streams
2 Stream Classes
4 Namespace
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
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
Stream Classes II II
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:
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.
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;
}
Examples
Examples:
delete p;
delete q;