Sunday, January 3, 2016

Getting Hardcore: Learning C++

In taking my first real steps with c++, I'm learning from this tutorial. (A friend also suggested this one.) Here are my cliff notes. There's a lot of vocab, and some comparisons to the languages I "know" already -- Java and JS, primarily.

Chapter 0/1

instantiation (int x) --> piece of RAM set aside, and token "x" is assigned a memory location (i.e. location 140)

l-values : a variable is just one type of l-value. L-values are called l-values because they go on the left of assignment statements. (There are r-values too.)

1.3a

The :: in "std::cout" is a namespace symbol, indicating that the "cout" object resides within the "std" namespace. You can save yourself typing std::cout every time by putting "using std::cout" at the beginning of your program. (I wonder if there's a way to undo this entrance into the std namespace?)

"using declarations" vs. "using directives" :
declaration -- "using std::cout"
directive -- "using namespace std"
(Directives are more general, and can result in "ambiguous symbol" errors if you also create functions or variables with names that overlap)

1.4

function calls cause the current function to be interrupted by the other function -- what implication does this have for scope and namespace?

1.4b -- Style guide for functions ("one task" rule)

1.4d -- "The name of a variable, function, class, or other object in C++ is called an identifier." I.e., an "identifier" is the name of an object, and variables, functions, and classes are all just objects.

1.6

Whitespace is ignored, like in Javascript. Unlike in Python and Ruby.
Adjust your whitespace to make your code easier to read. Think spacing/alignment of comments, l- and r-values, etc.

1.7

Hoisting (like in Javascript) is only performed if forward declarations are used. In c++, a forward declaration, when applied to a function, is called a function prototype ("prototype" is also a Javascript term, but it doesn't mean quite the same thing there.) It's kind of like declaring a variable without initializing it, but with a function instead of a variable. You name the function and define its parameters, but you don't give it any logic (you omit the body.)

Forward declarations can be applied to more than just functions. FD's can be applied to variables and user-defined types, for instance. 

FD/FP's are also used to tell the compiler to look for a function outside of the current file but within the current project. In XCode, it looks like the compiler will look within the entire project, since I created a file called "secondary.cpp" (with header "secondary.hpp") that contained a function called "add(int x, int y)". The "main.cpp" file referenced this function with a forward declaration/function prototype, and the project successfully compiled, linked, and ran when I pressed cmd+R.


No comments:

Post a Comment