April 24, 2015

Week of 04/19/15 - C++ Part 2: Compiling, Building, and Running

    I began Monday by installing the GCC Compiler for C++. Code::Blocks started up and I selected GCC to be the compiler. I followed the tutorial for a "Hello World" program, and apparently Code::Blocks starts off the command-line based program with a pre-made "Hello World" program. The program was "built" and run, and it opened a command window, said "Hello world!", and returned 0 at the end. However, I wanted to get a feel for the language, so I re-typed it entirely by hand.

    The tutorial went through build configurations, which are compilation settings, and the 2 default ones are debug and release. The debug configuration adds some stuff on compilation that lets you debug the program more easily, while the release configuration is more optimized and lacks any debugging add-ons. The release configuration is used to release code for download and running on computers, while the debug configuration is for development.

    Next, I learned about C++ structure. A statement conveys to the compiler that a task needs to be done. Statements are terminated by semicolons, and come in several different type. There are: declaration statements, which declare variables; assignment variables, which assign values to variables; and output statements, which outputs the value of a variable to the screen. Then there are expressions, which are operations, like addition or a function.

    A function is a collection of statements that run sequentially. Every program in C++ requires a function, called the main() function. Upon execution of the program, the first statement in the main() function is run.

    Libraries are pre-compiled code for reuse in many projects. C++ comes with the C++ Standard Library, for additional functionality. At the beginning of the program, you need to add any headers, like &ltiostream&gt from the standard library, and any headers from any other libraries you need.
    For my own learning purposes, &lt&lt is an output operator, and std::cout &lt&lt prints out whatever the output operator sends its way. At the end of the main function, there needs to be an indicator to the operating system that the code ran successfully or not; a zero means the code ran successfully. Anything non-zero is usually some sort of error.

     Comments can be made in 2 ways, the double slash (//comment) or the multiline (/*comment*/). Unlike html tags, multi-line comments do not nest, meaning that on the first */, the comment ends, and you shouldn't be nesting them anyways. Comments on functions, libraries, or programs, should be what they do. Comments on the code inside of functions and programs should be how the code is doing what it's doing. At the statement level, there needs to be even more detail, telling why you do something, and about any decisions you made on how the program functions. You can also comment out code, so that the code doesn't compile, in the event the code doesn't work and needs to be fixed, you're replacing code.

No comments: