Sams Teach Yourself C++ in 21 Days

(singke) #1
The Anatomy of a C++ Program 31

2


LISTING2.3 Using the usingKeyword


1: // Listing 2.3 - using the using keyword
2: #include <iostream>
3: int main()
4: {
5: using std::cout;
6: using std::endl;
7:
8: cout << “Hello there.\n”;
9: cout << “Here is 5: “ << 5 << “\n”;
10: cout << “The manipulator endl “;
11: cout << “writes a new line to the screen.”;
12: cout << endl;
13: cout << “Here is a very big number:\t” << 70000;
14: cout << endl;
15: cout << “Here is the sum of 8 and 5:\t”;
16: cout << 8+5 << endl;
17: cout << “Here’s a fraction:\t\t”;
18: cout << (float) 5/8 << endl;
19: cout << “And a very very big number:\t”;
20: cout << (double) 7000 * 7000 << endl;
21: cout << “Don’t forget to replace Jesse Liberty “;
22: cout << “with your name...\n”;
23: cout << “Jesse Liberty is a C++ programmer!\n”;
24: return 0;
25: }

Hello there.
Here is 5: 5
The manipulator endl writes a new line to the screen.
Here is a very big number: 70000
Here is the sum of 8 and 5: 13
Here’s a fraction: 0.625
And a very very big number: 4.9e+007
Don’t forget to replace Jesse Liberty with your name...
Jesse Liberty is a C++ programmer!
You will note that the output is identical to the previous listing. The only differ-
ence between Listing 2.3 and Listing 2.2 is that on lines 5 and 6, additional state-
ments inform the compiler that two objects from the standard library will be used. This is
done with the keyword using. After this has been done, you no longer need to qualify
the coutand endlobjects.
The second way to avoid the inconvenience of writing std::in front of coutand endlis
to simply tell the compiler that your listing will be using the entire standard namespace;
that is, any object not otherwise designated can be assumed to be from the standard

OUTPUT


ANALYSIS
Free download pdf