4.6 KiB
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
- binary: with prefix
- e.g. refer to listing2-1.cpp
- literal (hardcoded value in program)
- Floating-Point Types
- Floating-Point Literals
User-Defined Types
3 broad categories:
- Enumerations (枚举)
- Class (类)
- 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 语句示例实际上并没有创建任何变量,只是定义数据类型。
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
Race langobard_race = Race::Aidan;
Switch Statements
Enum often used with switch statement
e.g. refer to 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:
struct Book {
char name[256];
int year;
int pages;
bool hardcover;
};
Declare, initialize, use as shown in 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.
union Variant {
char string[10];
int integer;
double floating_point;
};
- union
Variant
can be interpreted aschar[10]
, anint
ordouble
. It takes memory of largest member
Declare, initialize, use as shown in 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.:
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:
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
#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
#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
int a = 0; // initialization to 0
int b{}; // initialization to 0
int c = {};
int d;
First 3 are reliable, while last