Friday, December 23, 2022

c++ study notes

 terms: flooding network propagation


--- cpp


statement - type of instruction that causes the program to perform some action.


function - a collection of statements that execute sequentially


Declaration statements

Jump statements

Expression statements

Compound statements

Selection statements (conditionals)

Iteration statements (loops)

Try blocks


####################

Rule


Every C++ program must have a special function named main (all lower case letters). When the program is run, the statements inside of main are executed in sequential order.

####################


preprocessor directive

#include <iostream>


{

function body

}


// single line comments

/* multiple line comments */


data is any information that can be moved, processed, or stored by a computer.


 value - a single piece of data stored somewhere


In C++, direct memory access is not allowed. Instead, we access memory indirectly through an object. 


An object is a region of storage (usually memory) that has a value and other associated properties


 A named object is called a variable, and the name of the object is called an identifier.


runtime - when the program is run


instantiation - object is created and assigned a memory address


variable - a named region of memory


identifier - name that a variable is accessed by


type - tells the program how to interpret a value in memory.


copy assignment / assignement = after a variable has been defined, you can give it a value 


= assignment operator


initialization - variable defined and provided an initial value


initializer - the value used to initialize a variable


4 ways to initialize


int a; // no initializer

int b = 5; // initializer after equals sign - copy initialization

int c( 6 ); // initializer in parenthesis - direct initialization

int d { 7 }; // initializer in braces


copy initialization - when initializer follows an equal sign

direct initialization 

brace initialization/uniform initialization/list initialization


 3 kinds of brace initialization

 - int width { 5 }; // direct brace initialization of value 5 into variable width (preferred)

 - int height = { 6 }; // copy brace initialization of value 6 into variable height

 - int depth {}; // value initialization (see next section


value initialization and zero initialization

- variable initialized with empty braces

- value assigned is 0

- zero initialization



Initialization gives a variable an initial value at the point when it is created. Assignment gives a variable a value at some point after the variable is created.


Direct brace initialization.


<< - insertion operator - cout


>> - extraction operator - cin


using namespace std; - "using directive" avoid


std::endl vs ‘\n’


######

Best practice


Prefer ‘\n’ over std::endl when outputting text to the console.

######


_____1.6


- initialization - object given value at the point of definition

- assignment - object given value beyond the point of definition

- uninitialized - object not given a known value yet


always initialize your variables


Undefined behavior - executing code whose behavior not well defined in c++


_____1.7 


keywords - 92 words reserved by c++ for 

identifiers - override, final, import, and module.


ide changes color to blue of these words


c++ is case sensitive


always start lowercase

not number

no symbols

no space

no reserved words


snake_case

camelCase or interCapped


######

Best practice


When working in an existing program, use the conventions of that program (even if they don’t conform to modern best practices). Use modern best practices when you’re writing new programs.

######


_ underscores - OS, library, and/or compiler use


correct (follows convention)

incorrect (does not follow convention)

invalid (will not compile)


_____1.8 Whitespace


_____1.9 Literals and operators


literals or literal constant - fixed values inserted into source code


arity, the number of operatands an operator takes as input


- unary

- binary

- tertiary


_____1.10 Expressions


Expression

An expression is a combination of literals, variables, operators, and function calls that can be executed to produce a singular value. 


Evaluation 

The process of executing an expression is called evaluation, 


Result

and the single value produced is called the result of the expression.


_____2.1


function = reusable sequence of statements to do a job

user-defined function = function that user writes himself

function call = an expression that tells the CPU to interrupt the current function and execute another function


caller = the function that initiated the function call

callee or called function = the function being called


### syntax to define a user defined function


return-type identifier() // identifier replaced with the name of your function

{

// Your code here

}


the curly braces identify the function body


###


metasyntactic variable - placeholder names


_____2.2


return value

return type 

return statement

return value


""""""

When you write a user-defined function, you get to determine whether your function will return a value back to the caller or not. To return a value back to the caller, two things are needed.


First, your function has to indicate what type of value will be returned. This is done by setting the function’s return type, which is the type that is defined before the function’s name. In the example above, function getValueFromUser has a return type of void, and function main has a return type of int. Note that this doesn’t determine what specific value is returned -- only the type of the value.


Second, inside the function that will return a value, we use a return statement to indicate the specific value being returned to the caller. The specific value returned from a function is called the return value. When the return statement is executed, the return value is copied from the function back to the caller. This process is called return by value.""""""


status code 

exit code

return code


DRY = Don't Repeat Yourself


_____2.3 Function parameters and arguments



function parameter - is a variable used in a function. 


A function parameter is a variable used in a function. Function parameters work almost identically to variables defined inside the function, but with one difference: they are always initialized with a value provided by the caller of the function.


argument - value that is passed from the caller to the function when a function call is made


pass by value - parameters of the function created as variables, value is copied


//This is a hard topic, learn more about passing of value


**function prototyping


_____2.4 Local scope


Function parameters, as well as variables defined inside the function body, are called local variables (as opposed to global variables, which we’ll discuss in a future chapter).


An identifier’s scope determines where the identifier can be accessed within the source code. When an identifier can be accessed, we say it is in scope. When an identifier can not be accessed, we say it is out of scope.


_____2.5 Why functions are useful and how to use them effectively


- organization

- reusability

- testing

- extensibility

- abstraction


refactoring - when a function is split into multiple sub-functions 


_____2.6 forward declarations


A forward declaration allows us to tell the compiler about the existence of an identifier before actually defining the identifier.


function prototyping - a declaration statement. Is composed of 


    - return type

    - name

    - parameters



#### ODR #####


One Definition Rule


1. Within a given file, a function, object, type, or template can only have one definition.

2. Within a given program, an object or normal function can only have one definition. This distinction is made because programs can have more than one file (we’ll cover this in the next lesson).

3. Types, templates, inline functions, and variables are allowed to have identical definitions in different files. 


Declaration - a statement that tells the compiler about the existence of an identifier and its type information. 


in C++ - all definitions are also declarations


but not all declarations are definitions


Pure declarations - "An example of this is the function prototype -- it satisfies the compiler, but not the linker. These declarations that aren’t definitions are called pure declarations. Other types of pure declarations include forward declarations for variables and type declarations (you will encounter these in future lessons, no need to worry about them now)."


Function prototype - A function prototype is a declaration statement that includes a function’s name, return type, and parameters. It does not include the function body.


Forward declaration - A forward declaration tells the compiler that an identifier exists before it is actually defined.

 - according to Bucky this is actually called "Function prototyping"


Notes:


Compiler fails to compile if function does not have the same number of parameters.


Other compiler link problems - if a function is undefined (no body), linker can't resolve the function call.  


Violating ODR rule 2 - "Within a given program, an object or normal function can only have one definition"


_____2.8 naming collisions and namespaces


:: in std::cout - scope resolution operator 

The identifier to the left of the :: symbol identifies the namespace that the name to the right of the :: symbol is contained within. If no identifier to the left of the :: symbol is provided, the global namespace is assumed.


using directive - (using namespace std) -  tells the compiler to check a specified namespace when trying to resolve an identifier that has no namespace prefix


_____2.9 introduction to the pre-processor


Prior to compilation, program goes through translation.


Translation has many phases.


Preprocessor directive (or simply directive) scans from top to bottom of file. Start with # and end with a new line.


Macro defines 


a macro is a rule that defines how input text is converted into replacement output text.


object-like macros

function-like macros


Object-like macros can be defined in one of two ways:


#define identifier

#define identifier substitution_text


When the preprocessor encounters this directive, any further occurrence of the identifier is replaced by substitution_text. The identifier is traditionally typed in all capital letters, using underscores to represent spaces.


No comments:

Post a Comment

EventId's in Nostr - from CGPT4

The mathematical operation used to derive the event.id in your getSignedEvent function is the SHA-256 hash function, applied to a string rep...