C++
: Ch1 : 3 |
Objectives : Input and Output Stream in
C++
- File Name : iostream_cin_cout1.cpp

- Introducing "#include <iostream>",
a preprocessor directive which support input and output
operation in C++ programming
- C++ language, uses an objects like ,"cin” or "cout", are
predefined stream-object, and to use these object you must
include the "iostream" file . The name "cin" and "cout" are
suffixed for "console input", and "console output"
respectively.
- Special Notes on
Namespaces.
- Operator
Overloading:
- bitwise left shift
(<<) operator, also known as insertion operator.
- bitwise right shift
(>>) operator, also known as extraction operator.
|
|
Step 1:
Create a source file, or use a template and replace the codes,
whatever suits you the best. In this example I used an existing
project , created with Eclipse IDE, and then edited the source code.
The
header file “iostream” of C++ language defines “cout” and “cin”
objects which handle input and output operations.
In the
image below, I remarked the line, “//using namespace std; “and used
those two objects with scope “::” operator. You won't have to

|
Step 2:
Now
edit this code, as shown below and then run. This application will
process an integer input with object “cin” and C++
extraction/input operator “ >>” (data extraction steps, shift to
right) , then display the data with the object “cout” and “<<”
insertion/output operator (insertion operations steps, shift to
left).

|
Step: 3 Brief Discussion: The
iostream library of C++, allows streaming data input and output. The
screenshot below, demonstrate that cin object with right-shift (>>)
operator was expecting an integer, but encountered a non-integral
data; an error expected and "0" will displayed as an output.
 |
Special Notes : using namespace std:
When we add a reference of this
namespace, " using namespace std;" we don't have to use the
object-scope resolution operator .

The label "namespace" allows to group
up classes, objects and functions as a single entity under a name.
C++ has some fixed namespaces like std, but it also allow the user
create new namespaces.
In ANSI C++ the standard library members (like cin, cout, string,
etc) are enclosed within a namespace called std. When you cite a
namespace before the "int main()" it becomes a global entity, and
you don't have to use "::" operator. The name of the
namespace must be unique.
We will look at this example,
scope_namespace1.htm,
later in this chapter.
|
|