Tutorial#10 - The Basics of C++ Program (Part - 3)

The Basics of C++ Program (Part-3)

In this section we will be discuss about:
1: using namespace std statement
2: Input using cin
3: Multiple inputs using cin statement
4: Role of namspace in C++ program

Namespace:

In July 1998, ANSI/ISO Standard C++ was officially approved. Most recent compilers are also compatible with ANSI/ISO Standard C++. (To be absolutely sure, check your compiler’s documentation.) The two standards, Standard C++ and ANSI/ISO
Standard C++, are virtually the same. The ANSI/ISO Standard C++ language has some features that are not available in Standard C++, which the remainder of this chapter addresses. In subsequent chapters, unless specified otherwise, the C++ syntax applies to both standards. First, we discuss the namespace mechanism of the ANSI/ISO Standard C++
Earlier, you learned that both cin and cout are predefined identifiers. In ANSI/ISO Standard C++, these identifiers are declared in the header file iostream, but within a namespace. The name of this namespace is std.
There are several ways you can use an identifier declared in the namespace std. One way to use cin and cout is to refer to them as std::cin and std::cout throughout the program.
Another option is to include the following statement in your program:
using namespace std;
This statement should appear after the statement #include <iostream>. You can then refer to cin and cout without using the prefix std::. To simplify the use of cin and cout, this book uses the second form. That is, to use cin and cout in a program,
the programs will contain the following two statements:
#include <iostream>
using namespace std;
In C++, namespace and using are reserved words.

Input with cin:

Now that we’ve seen some variable types in use, let’s see how a program accomplishes input. The next example program asks the user to enter value for variable a and b, and displays the sum on the screen. It uses integer variables.
The keyword cin (pronounced “C in”) is an object, predefined in C++ to correspond to the standard input stream. This stream represents data coming from the keyboard (unless it has been redirected). The >> is the extraction or get from operator. It takes the value from the stream object on its left and places it in the variable on its right.

Comments