Chapt 4. Variable Scope and More Types

4.4.1. Implicit type conversion (Learn C++)

compiler automatically convert data from one fundamental to another fundamental

there are two kinds implicit type conversion

A. Promotion:

1. Integral promotion: i.e. char → int

2. Floating point promotion: i.e. float → double

B. Conversion:

convert between different types or larger to similar, but smaller type

can result in loss of data

i.e. int -> double, int -> char

C. Arithmetic conversion priority

arithmetic operation always conducted on same type operands

long double (highest) -> double -> float -> unsigned long long ->

long long -> unsigned long -> long -> unsigned int -> int(lowest)

∗ type lower than ‘int’ automatically promoted to ‘int’ before calculation

 

4.4.2. Explicit type conversion (Learn C++)

developer explicitly tell the compiler to convert variable data type: casting

three major casting: A. c-style, B. static, C. dynamic

A. c-style cast: not recommend to use

casting operator ‘()’ i.e. float f = (float) i

not support compile time type checking -> can lead garbage assign

B. static cast: recommend to use

static cast operator: i.e. int i = static_cast<int>(c)

support compile-time type checking

 

4.7. ‘struct’ (Learn C++)

‘struct’ is one of simple aggregate data type in C++

∗ aggregate data type: the data type grouping multiple different variables

member selection operator(.) to access member variables

initializer list to initialize struct variable with creating it

! Don’t forget the semicolon(;) at the end of struct declaration

google c++ community recommend to use class instead of structs (Structs vs Classes)

 

4.8. ‘auto’ keyword (Learn C++)

In C++11, the meaning of ‘auto’ keyword has changed to automatic type deduction

tell the compiler to infer the variable type from the assignments type

‘auto’ only works when initialing variable upon creating it

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s