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.


No comments:

Post a Comment