Monday, September 6, 2021

Return to C++: Basics

 Notes based on Module 2 of Bill Weinman's LinkedIn course.

Pointers

int * ip = &x      //designates ip as a pointer (ip = int pointer), and gets memory address of x and stores it to ip

int & y = x    //Makes y a reference to x (?)


References

const int & y = x //Prevents changing x's value by changing y's value


"receding the pointer" = changing what it points at


A REFERENCE cannot be redefined to refer to a different variable.



Primitive Arrays

Size is set upon initialization and can't be changed thereafter

int array[] = {1,2,3,4,5};

int x = array[2];

//x will now return 3




Strings

Primitive string or c-string = a special (null-terminated) array of characters

char my_string[] = {'a', 'b', 'c', 0};




Structs

Which statement would correctly declare a variable s1 for the structure S declared in the following code?


struct S {

    int i;

    const char * s;

};


Answer: S s1 = { 3, "string one" };


Struct members default to public access.




Functions

example: 

void no_return(int i)

{

  puts(i);

}





Classes

Class members default to private access



Quiz notes

A REFERENCE cannot be redefined to refer to a different variable.

A c-string is an array of characters

Use break to exit out of a switch

Any amount of whitespace is equivalent.

Semicolons must be used to terminate statements.

An expression is anything that returns a value.

Pointers are type-aware


What is one advantage of using cout instead of printf or puts?

>> The cout class is type aware, and can string together different data types.


Disadvantage:

The `cout` class tends to create LARGER executable files, which is one of its disadvantages.


The cout class is found in the iostream header


What are the necessary parts of a C for loop?

>> an expression, a condition, and post-loop control


What is a primitive string in C++?

>> Primitive strings in C++ are arrays that end in 0, so they can be iterated through like other arrays.


The for loop uses three expressions to control flow. 


When using a range-based for loop with a c-string you must test for the null terminator.


The "main" function is the entry point of the program, called by the OS when the program launches


While struct members default to public access, class members default to private.


Initialized arrays have values defined in the array, and uninitialized arrays do not have values defined.

No comments:

Post a Comment