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.

  1. Source files in C use .c as file name extension, but C++ changes to use .cc.
  2. Header files in C use .h for both default and self-defined header files, but in C++ default header files do not have extension and self-defined header files still use .h.
  3. C++ uses namespaces to "organize symbols into logical groupings, which helps keep things tidy." (quote from .cc comments by instructor) It can be used for all types, functions, and variables. Namespace is a higher abstract object. But it does not exist in C.
  4. C++ uses iostream, fstream, strstream for input and output strings, while C uses function printf, sprintf, and so on.
  5. cout, cin, and cerr are new in C++. When using cout, there must be an endl at the end. "endl is the cross-platform stream version of an end-of-line character \n." (quote from comments)
  6. Operators "<<" and ">>" are also new in C++. Later I learned how to define operators, which is very convenient and powerful. "The stream operator << automatically converts types so here the C-string (char *) of argv[0] is converted to a true C++ string, then passed as a stream to cout." (quote from comments)
  7. Underline _ is wildly used to name variables and funtions in C++.
  8. "::" is used accompany with namespace, and . is combined with class. Both No.7 and No.8 are also common in Python, but do not exist in C.
  9. Function getline() is very useful to pause and interact with users. The function "gets entire line until a carriage return". (quote from comments)
  10. The way how to create a new .txt file and how to edit it is very fancy.

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.

  1. 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 . In some implementations this library may be automatically included when you include other libraries such as the library. Note that the null character may very well not be the very last character in the C-string array, but it will be the first character beyond the last character of the actual string data in in that array. For example if you have a C-string storing "Hello" in a character array of size 10, then the letters of the word "Hello" will be in positions with indices 0 to 4, there will be a null character at index 5, and the locations with indices 6 to 9 will contain who-knows-what. In any case, it's the null character at index 5 that makes this otherwise ordinary character array a C-string.

  2. 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 header file.

//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.