Finished chap2
parent
4fa88dc8ad
commit
57f4a573b2
|
@ -0,0 +1,2 @@
|
|||
.vscode/*
|
||||
.ipynb_checkpoints/*
|
|
@ -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 <cstdio>
|
||||
|
||||
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 <cstdio>
|
||||
|
||||
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
|
Binary file not shown.
|
@ -0,0 +1,10 @@
|
|||
#include <cstdio>
|
||||
|
||||
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);
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,27 @@
|
|||
#include <cstdio>
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,14 @@
|
|||
#include <cstdio>
|
||||
|
||||
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);
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,19 @@
|
|||
#include <cstdio>
|
||||
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue