Primitive Build-in Types
void type
arithmetic type:
bool, char, wchar_t, char16_t, char32_t, short, int, long, long long, float, double, long double
Literals
'a' // character literal
"Hello World!" // string literal
L'a' // wide character literal, type is wchar_t
u8"hi!" // utf-8 string literal (utf-8 encodes a Unicode character in 8 bits)
42ULL // unsigned integer literal, type is unsigned long long
1E-3F // single-precision floating-point literal, type is float
3.14159L // extended-precision floating-point literal, type is long double
Variables
Initialization is not assignment. Initialization happens when a variable is given a value when it is created. Assignment obliterates an object’s current value and replaces that value with a new one.
Four different ways to define an int variable named units_sold and initialize it to 0.
int units_sold = 0;
int units_sold = {0};
int units_sold{0};
int units_sold(0);
When used with variables of built-in type, this form of initialization has one important property: The compiler will not let us list initialize variables of built-in type if the initializer might lead to the loss of information.
long double ld = 3.1415926536;
int a{ld}, b = {ld}; // error: narrowing conversion required
int c(ld), d = ld; // ok: but value will be truncated
The value of an object of built-in type that is not explicitly initialized depends on where it is defined. Variables defined outside any function body are initialized to zero. With one exception, which we cover in § 6.1.1 (p. 205), variables of built-in type defined inside a function are uninitialized. The value of an uninitialized variable of built-in type is undefined (§ 2.1.2, p. 36). It is an error to copy or otherwise try to access the value of a variable whose value is undefined.
Each class controls how we initialize objects of that class type. In particular, it is up to the class whether we can define objects of that type without an initializer. If we can, the class determines what value the resulting object will have.
Uninitialized objects of built-in type defined inside a function body have undefined value. Objects of class type that we do not explicitly initialize have a value that is defined by the class.
Variable Declarations and Definitions
C++ supports what is commonly known as separate compilation. Separate compilation lets us split our programs into several files, each of which can be compiled independently.
To support separate compilation, C++ distinguishes between declarations and definitions. A declaration makes a name known to the program. A file that wants to use a name defined elsewhere includes a declaration for that name. A definition creates the associated entity.
A variable declaration specifies the type and name of a variable.
a definition also allocates storage and may provide the variable with an initial value.
To obtain a declaration that is not also a definition, we add the extern keyword
extern int i; // declares but does not define i
int j; // declares and defines j
Any declaration that includes an explicit initializer is a definition.
extern double pi = 3.1416; // definition
Variables must be defined exactly once but can be declared many times.
Static Typing
C++ is a statically typed language, which means that types are checked at compile time. The process by which types are checked is referred to as type checking.
Identifiers
Identifiers are case-sensitive.
1_or_2; //wrong, two consecutive underscore
_Aaa; //wrong, underscore + uppercase letter
_a;; // correct if inside a function, wrong if outside a function
Reserved names can not be used as variable names.
Scope of a Name
A scope is a part of the program in which a name has a particular meaning. Most scopes in C++ are delimited by curly braces.
global scope, block scope
Advice: Define Variables Where you first use them
Nested scope: inner scope, outer scope
int reused = 42; //reused has global scope
int main()
{
int reused = 0;
std::cout<<::reused <
}
The global scope has no name.
Comments
Post a Comment