diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e42b40d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/* +.ipynb_checkpoints/* \ No newline at end of file diff --git a/chap2_types.md b/chap2_types.md new file mode 100644 index 0000000..b927ac8 --- /dev/null +++ b/chap2_types.md @@ -0,0 +1,221 @@ +# Chapter 2 Types + +## Fundamental Types + +aka **primitive types** or **built-in types** + +* **Integer Types** (`int`) + * **literal** (hardcoded value in program) + * binary: with prefix `0b` + * octal: `O` + * decimal: default + * hexadecimal `0x` + * e.g. refer to [listing2-1.cpp](src/chap2/listing2-1.cpp) +* **Floating-Point Types** + * **Floating-Point Literals** + +## User-Defined Types + +3 broad categories: + +1. **Enumerations (枚举)** +2. **Class (类)** +3. **Unions (共同体)** + +### Enumerations + +Delaring enumeration: + +* start with `enum` followed by type name + a list of values it takes. +* values are alphanumeric strings. + * Values are integer under the hood, allow us to write safer. + * enum 语句示例实际上并没有创建任何变量,只是定义数据类型。 + +```cpp +enum class Race { + Dinan, + Teklan, + Ivyn +}; +``` + +After declare, we can initiate & use it. + +Initialize an enum variable: use name of type followed by `::` + desired values + +```cpp +Race langobard_race = Race::Aidan; +``` + +#### Switch Statements + +Enum often used with switch statement + +e.g. refer to [listing2-15.cpp](src/chap2/listing2-15.cpp) + +### Plain-Old-Data Classes + +* **Classes** are user-defined types that contain data and functions. +* Simplest kind of classes are **plain-old-data classes (PODs)** + * PODs are simple containers. +* POD construction: keyword `struct` + POD's name +* i.e. `struct` in C language + +Define POD: + +```cpp +struct Book { + char name[256]; + int year; + int pages; + bool hardcover; +}; +``` + +Declare, initialize, use as shown in [listing2-16.cpp](src/chap2/listing2-16.cpp) + +### Unions + +Unions is similar as POD, but puts all of its members in the same place. It's like an interpretation of a block of memory. + +Define a union, like struct. + +```cpp +union Variant { + char string[10]; + int integer; + double floating_point; +}; +``` + +* union `Variant` can be interpreted as `char[10]`, an `int` or `double`. It takes memory of largest member + +Declare, initialize, use as shown in [listing2-18.cpp](src/chap2/listing2-18.cpp) + +* Problem of Union: When you try to interpret v as an old value, it failed, as old variable is gone. + + +## Fully Featured C++ Classes + +POD classes only contain data members. Classes can be used to *encapsulate* data and methods. + +### Methods + +**Methods** = member functions, create connection among a class, its data. Methods have access to all class's members + +Defining class method e.g.: + +```cpp +struct ClockOfTheLongNow{ + void add_year() { + year++; + } + int year; +}; +``` + +### Access Controls + +* **Access controls** restrict class-member access. +* 2 major access controls: `public` & `private` +* Anyone can access a public member, but only a class can access private members. All `struct` members are public by default + +#### The `class` Keyword + +Replace `struct` keyword with `class`, which declare members `private` by default (i.e. change default access controls). + +To make it method public via: + +```cpp +class ClockOfTheLongNow { + int year; +public: + void add_year() { + // + } + bool set_year(int new_year) { + // + } + int get_year() { + + } +}; +``` + +There are no difference btw `struct` & `class`, only matter of style. + +#### Initializing Members + +### Constructors + +* Constructors are special methods with special declaration. +* Constructors declarations don't state a return type +* Constructors has same name as class + +```cpp +#include + +struct ClockOfTheLongNow { // Constructor takes no arguments + ClockOfTheLongNow() { + year = 2019; // Set value in initialization as default + } +} + +int main() { + ClockOfTheLongNow clock; + printf("Default year: %d", clock.get_year()); +} +``` + +* We can create multiple constructor for initializiton, as shown below + +```cpp +#include + +struct ClockOfTheLongNow { + ClockOfTheLongNow(int year_in) { // Define a constructor with argument + if (!set_year(year_in)) { + year = 2019; + } + } +}; + +int main() { + ClockOfTheLongNow clock{2020}; // initialization with value + printf("Year: %d", clock.get_year()); +} +``` + +### Initialization + +**Object initialization** or just **initialization**, is the way to bring objects to life. + +Here explain the initialization in simple way. + +#### Initializing a Fundamental Type to Zero + +```cpp +int a = 0; // initialization to 0 +int b{}; // initialization to 0 +int c = {}; +int d; +``` + +First 3 are reliable, while last + + +#### Initializing a Fundamental Type to an Arbitrary Value + +#### Initializing PODs + +#### Initializing PODs to Arbitrary Values + +#### Initializing Arrays + +#### Full Featured Classes + +#### Narrowing Conversions + +#### Brace Youself + +### Destructor \ No newline at end of file diff --git a/src/chap2/listing2-1 b/src/chap2/listing2-1 new file mode 100755 index 0000000..94df87c Binary files /dev/null and b/src/chap2/listing2-1 differ diff --git a/src/chap2/listing2-1.cpp b/src/chap2/listing2-1.cpp new file mode 100644 index 0000000..19b9a20 --- /dev/null +++ b/src/chap2/listing2-1.cpp @@ -0,0 +1,10 @@ +#include + +int main() { + unsigned short a = 0b10101010; + printf("%hu\n", a); + int b = 0123; + printf("%d\n", b); + unsigned long long d = 0xFFFFFFFFFF; + printf("%llu\n", d); +} \ No newline at end of file diff --git a/src/chap2/listing2-15 b/src/chap2/listing2-15 new file mode 100755 index 0000000..2ad8c45 Binary files /dev/null and b/src/chap2/listing2-15 differ diff --git a/src/chap2/listing2-15.cpp b/src/chap2/listing2-15.cpp new file mode 100644 index 0000000..b9508bc --- /dev/null +++ b/src/chap2/listing2-15.cpp @@ -0,0 +1,27 @@ +#include + +enum class Race { + Dinan, + Teklan, + Ivyn, + Moiran, + Camite, + Julian, + Aidan +}; + +int main() { + Race race = Race::Dinan; + + switch (race) + { + case Race::Dinan: + printf("You work hard."); + break; + case Race::Teklan: + printf("You are very strong."); + break; + default: + printf("Error: unkown race"); + } +} \ No newline at end of file diff --git a/src/chap2/listing2-16 b/src/chap2/listing2-16 new file mode 100755 index 0000000..490f079 Binary files /dev/null and b/src/chap2/listing2-16 differ diff --git a/src/chap2/listing2-16.cpp b/src/chap2/listing2-16.cpp new file mode 100644 index 0000000..be1305e --- /dev/null +++ b/src/chap2/listing2-16.cpp @@ -0,0 +1,14 @@ +#include + +struct Book { + char name[256]; + int year; + int pages; + bool hardcover; +}; + +int main() { + Book neuromancer; // Declare Book object + neuromancer.pages = 271; + printf("Neuromancer has %d pages.\n", neuromancer.pages); +} \ No newline at end of file diff --git a/src/chap2/listing2-18 b/src/chap2/listing2-18 new file mode 100755 index 0000000..ec89e34 Binary files /dev/null and b/src/chap2/listing2-18 differ diff --git a/src/chap2/listing2-18.cpp b/src/chap2/listing2-18.cpp new file mode 100644 index 0000000..c669860 --- /dev/null +++ b/src/chap2/listing2-18.cpp @@ -0,0 +1,19 @@ +#include + +union Variant { + char string[10]; + int integer; + double floating_point; +}; + +int main() { + Variant v; // Declare a Variant v + v.integer = 42; // Interpret va as an integer, set its values + printf("The ultimate answer: %d\n", v.integer); + + v.floating_point = 2.718281; // Reinterpret v as a float and reassign its value + printf("Euler's number e: %f\n", v.floating_point); + + printf("A dumpster fire: %d\n", v.integer); + // Problem of Union: When you try to interpret v as an integer again. original value is no more +} \ No newline at end of file