April 30, 2015

Week of 04/26/15 - C++ Part 3: Experimentation

    The std::cout << object from iostream is used to output text to the console, and the output operator can be used to print multiple things on the same line. From what I see, to make your code look clean, you can also just have multiple lines of std::cout << without having a << endl; statement at the end, though I don't see why someone would need an output that long, or why they wouldn't use a string variable instead. However, when printing from the console, the spacing in the string is important, like when you're trying to print "Your name is X" on the console. You don't want to make it say "Your name isX", so you need to add a space at the end of the string so that the variable isn't conjoined with the string.
    std::cin >> is similar to std::cout, except it works in the opposite direction, and it requires input. It takes input from the console and then puts it in a variable.
    std is a special container, or a namespace, for everything inside the standard library. Putting std:: before cin, cout, endl, and probably other functions makes the compiler check for the function in the standard library's namespace. However, for some people, this becomes a chore. This is why we have the using directive. With a using directive, you can write using namespace std and then it would look for all the functions in your code in the standard library. However, doing this could cause a naming collision between the standard library's function and a function you wrote yourself. You can also do this for individual functions like using std::cout, which maps only cout to the standard library.
    I also did an experiment today. I wanted to see if an executable compiled with std:: would differ from an executable compiled with a using std::cout. I compiled them both, and ran them through an md5 hash checker, which takes the data of the file and based on the data, and returns a checksum. This checksum is constant if the data does not change, and the checksum changes greatly if the data is changed. Also, two files with the same data return the same checksum. And when I ran the two files that were compiled with different code through the md5 hash checker, they had the same checksum. Which means that both of the files were the same, even with different code. I think this is because doing the namespace thing just tells the compiler where to look for the functions like cout, cin, and endl.

No comments: