CS510_CW12_Nengyin & Kaiqin
Examine the source code in src/streams carefully.
i. The key differences that you see in the file between C++ and C.
ii. Describe the concept of a namespace, and what :: means.
A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.
"::" is the identifier of the specific elements in the namespace. For example, std::cout means using the function cout in the namespace std.
iii. Describe how streams do things differently. In particular, explain the differences in reading from command line arguments, reading and writing to a file, printing to standard output, and printing to standard error.
The same as C, C++ use two arguments in main function, argc and argv to read from command line. But it use fstream/ifstream/ofstream to read/write from files. It also uses istream/ostream/iostream to read/write from standard input/ouput/error.
The following tables are infromation about the class and output/input/error of iostream.
| Form of IO | Input | Output | Input and Output |
|---|---|---|---|
| Standard IO | istream | ostream | iostream |
| File IO | ifstream | ofstream | fstream |
| Array of char IO | istrstream | ostrstream | strstream |
| command | Stream | Type | Buffered Description |
|---|---|---|---|
| cin | istream | Yes | Connected to standard input (e.g., the keyboard) |
| cout | ostream | Yes | Connected to standard output (e.g., the monitor) |
| clog | ostream | Yes | Connected to standard error (e.g., the monitor) |
| cerr | ostream | No | Connected to standard error (e.g., the monitor) |
iv. Explain how a C++ string differs from a C string.
A C-string, which consists of an array of characters terminated by the null character '\0', and which therefore is different from an ordinary array of characters. There is a whole library of functions for dealing with strings represented in this form. Its header file is
A C++ string object, which is an instance of a "class" data type whose actual internal representation you need not know or care about, as long as you know what you can and can't do with variables (and constants) having this data type. There is a library of C++ string functions as well, available by including the
//From: http://cs.stmarys.ca/~porter/csc/ref/c_cpp_strings.html
v. Compile and run the code, and make sure you understand the output.
Output and explanation:
~/cw-12-redyellow/src/streams$ make
g++ streams.cc -o streams
~/cw-12-redyellow/src/streams$ ./streams
Usage: ./streams arg1 # If there is no command line argument, it would return and prompt use format, "./streams arg1".
~/cw-12-redyellow/src/streams$ ./streams 3
arg1 = 3 # return the imput argument, which is 3 here.
This is printing from foo. # call function print() in namespace foo.
This is printing from bar. # call function print() in namespace bar.
This is printing from foo. # after localize foo::print, call print() in main() function directly, it will call foo::print.
Enter a float: 3.5 # ask for enter a float, here I type in 3.5
Input as a float: 3.5 # print the input float, which is parsed to float.
Input as an int : 3 # print the input float, which is parsed to int. It never rounds but just keeps the integer part.