July 17, 2009

C++

http://www.learncpp.com/cpp-tutorial/07-a-few-common-cpp-problems/

1. Statements are terminated by semi-colons. An ex. is 'int x;'
2. All variables in a program must be declared, also know as a declaration statement. An ex. is 'x=5;'
3. An asssignment statement assigns a value to a variable.
4. An output statement outputs the value of x to the screen. An ex. is 'cout <<expression is a mathematical entity that equates to a value. Expressions can contain values, variables, and operators.
6. Functions are a group of statements. C++, statements are usually grouped into functions.
7. Every C++ program contains special functions called main (). Once the program runs, the first statement in main () executes firest.
8. Functions are created to do a specific job. An ex. would be Max() used to find the maximum number.
9. Libraries are group of functions grouped to be used on various projects.
10. A comment is a line of text inserted into the code to explain what the code is doing.
11. A single line comment beings with "//", which tells the compiler to ignore everything to the end of the line.
12. The single line comments should go about the line line of code.
13. For muli-line of comments, C-style multi-line comment is used. This is denoted by "/*" at the beginning of the line and "*/" at the end of the line.
14. Never nest comments inside other comments.
15. For library, program, or functional level, comments should be used to describe what the library, program, or function does.
16. Within the library, program, or functional level, the comments should be used to describe how the code is going to accomplish it's goal.
17. The comments should make sense for someone who has not idea of what the code does.
18. A variable is C++ is a name for a piece of memory used to store the information.
19. Integer is a whole number.
20. An integer value is variable that holds an integer value.
21. To declare a variable, a declaration statement is used. For ex., "int x;".
23. In C++, variables are known as l_values (or ell-values). An l-value has an address in memory.
24. The l-value orginally got it's name because they are the only values that can be on the left side of an assignment statement.
25. The r-value is the value that could be assigned to an l-value.
26. The r-values are single numbers, variables, or expressions.
27. There is no guarantee that the variables will be assigned the same memory address each time in the program.
28. A variable that has not been assigned a value is called an uninitialized variable.
29. Uninitialized variables are dangerous because they cause intermittent problems.
30. Good rule of thumb is to always assign values to variables when they are declared. For ex., "int x=3;".
31. Cout prints data to the console.
32. Cin reads data from the console.
33. A function is a sequence of statements designed to do a particular job.
34. A function call is an expression that tells the CPU to interrupt the current function and execute another function.
35. When using the print to console and 'end1' command, use 'using namespace std;' in the function.
36. The 'return 0' is used in the main function. It returns a value back to the operating system(the caller) by using a return statement.
37. When main function finish execution, it returns a value back to the operating system (the caller) by using the return statement.
38. Function you write can return a single value to their caller as well. This is done by changing the return type of the function in the function's declaration.
39. A return type of void means that the function does not return a value.
40. A return type of int means the function returns an integer value to the caller.
41. It is not valid to pass void to cout. The compiler will give you an error when tried to compile this line.
42. Functions can only return a single value using a return statement.
43. When main finishes executing, it returns a value back to the operating system (caller) by using the return statement.
44. Functions you write can return a single value to their caller well. Do this by changing the return type of the function in the function's declaration.
45. A return type of vaoid means the function does not return a value. A return type of int means the function returns an integer value to the caller.
46. When a program is executed, the OS makes a function call to main(). Execution then jumbs to the top of the main. The statement in main are executed sequentially. Finally, main returns a integer value (usually 0) back to the OS. This is why main is declared as 'int main()'.
47. Functions offer a great way to break your program up into manageable and reusable parts, which can then can be easily connected together to perform a larger and more complex task.
48. A good rule of thumb is that each function should perform one (and only one) task.
49. An expression is a combination of literals, variables, operators, and functions that evaluates to a value.
50. A literal is a simply a number, such as 5, or 3.14159. When we talk about the expression "3+4", both 3 and 4 are literals. Literals alwasy evaluate to themselves.
51. Variables evaluate to the values they hold.
52. Functions evaluate to produce a value of the fuction's return type.
53. Literals, vaiables, and functions are all known as operands. Operands are the objects of an expression that are acted upon. Operands supply the data that the expression works with.
54. Operators tell how to combine the operands to produce a new results.
55. Operators come in two types: Unary and Binary.
56. Unary operators act on one operand. For ex., the '-' operator. In the expression -5, the - operator is on being applied to one operand (5) to produce a new value (-5).
57. Binary operators act on two operands (know as left and right). For ex., the binary operator is the + operator. In the expression 3+4, the + operator is working with a left operand (3) and a right operand (4) to produce a new value (7).
58. Some operators have more than one meaning. For ex., the - operator has two contexts. It can be used in unary form to invert a number's sign (eg. -5), or it can be used in binary form to do arithmetic subtraction (eg. 4-3).
59. Whitespace is a term that refers to characters that are used for formatting purposes. This refers to spaces, tabs, and (sometimes)newlines. The compiler generally ignors whitespace, with a few minor exceptions.
60. Whitespace do pay attention to space inside the quoted test, such as 'Hello World' and 'Hello World'.
61. New lines are not allowed in quoted text.
62. Single line comments only last to end of line.
63. C++ does not enforce any kind of formatting restrictions on the programmer.
64. Some recommendations for formatting:

* Your tab should be set to 4 spaces.
* The braces that tell where a function begins and ends should be aligned withthe function name, and be on their own lines
* Each statement within baces should start one tab in from the opening brace of the function it belongs to.
* Lines should net to too long. Typically, 72, 78, or 80 characters is the maximum length a line should be. If a line is going to be longer, it should be broken (at a reasonable spot) into multiple lines by indenting with a double tab.
* If a long line that is broken into pieces is broken with an operator (eg. << or +), the operator should be placed at the end of the line, notthe start of the next line
* Use whitespace to make your code easier to read

65. When addressing compile errors in your programs, always resolve the first error produced first.
66. In a forward declaration, we declare (but not define) our function in advance of where we use it, typically at the top of the file. This way, the compiler will understand what our function looks like when it encounters a call to it later. This is done by writing a declaration statement known as a function prototype.
67. A function prototype is a declaration of a function that includes the function's name, parameters, and retuurn type, but does not define the function. 1.7
68. A function prototype does not need to specify the names of the parameters. For ex., 'int add(int, int);'. If is is specified, it is more descriptive.
69. If a forward declaration is made, but the fuction is never called, the program will compile and run file. If a forware declaration is made, the function is called, but the program never defines the function, the program will compile okay, but the linker will complain that it can't resolve the function call.
70. A function prototype is declaration statement that tells the compiler that a function's return type is, what the name of the funtion is, and what the types of it's parameters are.
71. We can give files access to functions that live another file with the use of forward declarations.
72. As programs grow larger and larger, it becomes tedious to have to forward declare every function you use thaat lives in a different file. To solve that problem, the concept of header files is introduced.
73. Code files (with a .cpp extension) are not the only files commonly seen in programs. The other type of file is called a header file, sometimes known as an include file. Header files almost always have a .h extension. The purpose of a header file is to hold declarations for other files to use.
74. The program never defines cout, so how does the compiler know what cout is? The answer is that cout has been declared in a header file call 'iostream'. When we use the lin '#include ', we are telling the compile to locate and then read all the declarations from a header file name 'iostream'.
75. Header files typically only contain declarations. They do not define how something s implemented, and you already know that your program won't link if it can't find the implementation of something you use. So if cout is only defined in the 'iostream' header file, where is it actually implemented? It is implemented in the runtime support library, which is automatically linked into your program during the link phase.
76. A library is a package of code that is meant to be reused in many programs.
77. A libray includes a header file that contains declarations for everything the library wishes to expose (make public) to users, and a precompiled object that contains all of the implementation code compiled into machine language.
78. These libraries typically have a .lib or .dll extension on Windows, and a .a or .so extension on Unix.
79. Libraries are precompiled because they rarely change and do not need to be recompiled often. Also, precompiled programs are in machine language, it prevents people from acccesing or changing the source code, which is important to businessess or people who don't what to make their source code available for intellectual property reasons.
80. Header files need to be only written once and can be included in as many files as needed.
81. Header files consist of two parts. The first part is called a header guard and the second part is the actual content of the .h file, which should be the declarations for all of the functions we want other files to be able to see.
82. The reason for angled brackets, are to tell the compiler that we are including a header file that was included with the compiler. The double-quotes, "add.h", tell the compileer that this is a header file were are supplying, which causes it to look for the header file in the current directory containing our source code first.
83. Use angled brackets to include header files that come with the compiler. Use double qutes to include any other header files.
84. Rule: use the non.h version of a library if it exists, and access the functionality through the std namespace. If the non .h version does not exist, or you are creating your own headers, use the .h version.
85. The preprocessor is a separate program that runs before the compiler when you compile your program. It purpose is to process directives.
86. Directives are specific instructions that start with a # symbol and end with a newline (not a semicolon).
87. The prepocessor is not smart, it does not understand C++ syntax; rather, it manipulates text before the compiler gets to it.
88. #Include tells the preprocessor to insert the contents of the included file into the current file at the point of the #include directive. This is useful when you have information that needs to be included in multiple places (as forward declaration often are).
89. The #include command has two forms:

#include tells the compiler to look for the file in a special place defined by the operating system where header files fro the runtime library are held.
#include "filename" tells the compiler to look for the file in directory containing the source file doing the #include. If that fails, it will act identically to the angled brackets case. 1.10