| Start |
|---|
| Hello World This program prints See it live on Coliru (opens new window). AnalysisLet's examine each part of this code in detail: Here, the `int` is what is called the function's [return type](http://stackoverflow.com/documentation/c%2B%2B/572/lambdas/1855/specifying-the-return-type#t=20170126184705835537). The value returned by the `main` function is an **exit code.** By convention, a program exit code of `0` or `EXIT_SUCCESS` is interpreted as success by a system that executes the program. Any other return code is associated with an error. If no `return` statement is present, the `main` function (and thus, the program itself) returns `0` by default. In this example, we don't need to explicitly write `return 0;`. All other functions, except those that return the `void` type, must explicitly return a value according to their return type, or else must not return at all.
|
| Comments A comment is a way to put arbitrary text inside source code without having the C++ compiler interpret it with any functional meaning. Comments are used to give insight into the design or method of a program. There are two types of comments in C++: Single-Line CommentsThe double forward-slash sequence C-Style/Block CommentsThe sequence In any block comment, you can write anything you want. When the compiler encounters the symbol The above example is valid C++ (and C) code. However, having additional Block comments can also start and end within a single line. For example: Importance of CommentsAs with all programming languages, comments provide several benefits:
However, comments also have their downsides:
The need for comments can be reduced by writing clear, self-documenting code. A simple example is the use of explanatory names for variables, functions, and types. Factoring out logically related tasks into discrete functions goes hand-in-hand with this. Comment markers used to disable codeDuring development, comments can also be used to quickly disable portions of code without deleting it. This is often useful for testing or debugging purposes, but is not good style for anything other than temporary edits. This is often referred to as “commenting out”. Similarly, keeping old versions of a piece of code in a comment for reference purposes is frowned upon, as it clutters files while offering little value compared to exploring the code's history via a versioning system. |
| The standard C++ compilation process Executable C++ program code is usually produced by a compiler. A compiler is a program that translates code from a programming language into another form which is (more) directly executable for a computer. Using a compiler to translate code is called compilation. C++ inherits the form of its compilation process from its "parent" language, C. Below is a list showing the four major steps of compilation in C++:
Many C++ compilers may also merge or un-merge certain parts of the compilation process for ease or for additional analysis. Many C++ programmers will use different tools, but all of the tools will generally follow this generalized process when they are involved in the production of a program. The link below extends this discussion and provides a nice graphic to help. [1]: http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/compile.html (opens new window) |
| Function A function is a unit of code that represents a sequence of statements. Functions can accept arguments or values and return a single value (or not). To use a function, a function call is used on argument values and the use of the function call itself is replaced with its return value. Every function has a type signature -- the types of its arguments and the type of its return type. Functions are inspired by the concepts of the procedure and the mathematical function.
Functions are often meant to perform a specific task. and can be called from other parts of a program. A function must be declared and defined before it is called elsewhere in a program.
Function DeclarationA function declaration is declares the existence of a function with its name and type signature to the compiler. The syntax is as the following: In the example above, the
The argument name is optional; the declaration for the function could also be the following: Per the one-definition rule, a function with a certain type signature can only be declared or defined once in an entire C++ code base visible to the C++ compiler. In other words, functions with a specific type signature cannot be re-defined -- they must only be defined once. Thus, the following is not valid C++: If a function returns nothing, its return type is written as Function CallA function can be called after it has been declared. For example, the following program calls Here, Function DefinitionA function definition* is similar to a declaration, except it also contains the code that is executed when the function is called within its body. An example of a function definition for Function OverloadingYou can create multiple functions with the same name but different parameters. Both functions are called by the same name Default ParametersDefault values for function parameters can only be specified in function declarations. In this example, Special Function Calls - OperatorsThere exist special function calls in C++ which have different syntax than Certain special character sequences that will be reduced to function calls by the compiler, such as C++ handles these character sequences with a special syntax; but, in essence, each occurrence of an operator is reduced to a function call. For example, the following C++ expression: is equivalent to the following function call: All operator function names start with While in C++'s immediate predecessor, C, operator function names cannot be assigned different meanings by providing additional definitions with different type signatures, in C++, this is valid. "Hiding" additional function definitions under one unique function name is referred to as operator overloading in C++, and is a relatively common, but not universal, convention in C++. |
| Visibility of function prototypes and declarations In C++, code must be declared or defined before usage. For example, the following produces a compile time error: There are two ways to resolve this: putting either the definition or declaration of However it is also possible to "forward-declare" the function by putting only a "prototype" declaration before its usage and then defining the function body later: The prototype must specify the return type ( One common way to integrate this into the organization of source files is to make a header file containing all of the prototype declarations: and then provide the full definition elsewhere: and then, once compiled, link the corresponding object file An “unresolved external symbol” error occurs when the function prototype and call exist, but the function body is not defined. These can be trickier to resolve as the compiler won't report the error until the final linking stage, and it doesn't know which line to jump to in the code to show the error. |
| Preprocessor The preprocessor is an important part of the compiler. (opens new window) It edits the source code, cutting some bits out, changing others, and adding other things. In source files, we can include preprocessor directives. These directives tells the preprocessor to perform specific actions. A directive starts with a # on a new line. Example: The first preprocessor directive you will meet is probably the directive. What it does is takes all of This line adds the functions (opens new window) and objects that let you use the standard input and output. The C language, which also uses the preprocessor, does not have as many header files (opens new window) as the C++ language, but in C++ you can use all the C header files. The next important directive is probably the directive. This tells the preprocessor that as it goes along the file, it should replace every occurrence of The This actually is useful, because of the These directives insert the code that is in the true bit, and deletes the false bits. this can be used to have bits of code that are only included on certain operating systems, without having to rewrite the whole code. RemarksThe 'Hello World' program is a common example that can be simply used to check compiler and library presence. It uses the C++ standard library, with The process for compiling a C++ program inherently differs between compilers and operating systems. The topic Compiling and Building (opens new window) contains the details about how to compile C++ code on different platforms for a variety of compilers. |
Read Article
https://develop.horje.com/learn/1434/reference