Thursday, September 9, 2021

More C++: Datatypes. (Qualifiers, Bit Fields, ...)

Note: Much of my programming has been done in JS, so you could say I think in JavaScript. So, from that perspective...

Based on Module 3: Datatypes of Bill Weinman's LinkedIn course.

Covered in this chapter: 

Overview of data types
Integer Types
Integer Sizes
Fixed-Size Integers
Floating-point types
Characters and strings
Character escape sequences
Qualifiers
References
Structured data
Bit fields
Enumerations
Unions
Defining types with typedef
The 'void' type
The 'auto' type
Unambiguous null pointer constant
Challenge: A library card data structure
Solution to challenge

Qualifiers

CV Qualifiers

const -- makes program throw an error if reassignment is attempted

mutable

volatile


Storage Duration Qualifiers

static -- lets a variable persist outside of the block in which it was defined

register

extern


Bit Fields

Allow you to pack multiple values into less space
Avoid in threaded programming
Use when storage space comes at a premium
Can behave differently on different OSs

Enumerations

Type safe "alternative" to preprocessor macros
Behave kind of like constants 
Use mostly for grouping scalar constants together when they have a common usage
Enumerated types have integer values

Unions

Data structure that allows you to use the same memory space for different types
Sometimes used for crude polymorphism (multiple representations of same data)

Defining Types: TypeDef

A typedef can be used as an alias for a type


Quiz

Any valid type may be a member of a struct
The 'cstdint' header allows you to specify size of integers, both signed and unsigned
You would use the 'int32_t' type when you need a signed int with a fixed size of 32 bits (other options are 8, 16, 64)
The primitive integer types are not guaranteed to be specific sizes

correct format to concatenate two primitive strings in C++: const char * cstring = "String" " and another string";

Once defined, a reference can never be re-assigned.
The types defined in the 'cstdint' header are guaranteed to be consistent sizes.

Floating point types sacrifice precision for scale.

The special nullptr value is used to provide an unambiguous null value for pointers.


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.