!!!Chapter 2 Variable and Basic Types (2.1 ~ 2.3)

2.1 Primitive Build-in Types

void is a special type, it has no associated values and is most often used as the return type for a function that has no return value.

Arithmetic types that represent integers, characters and boolean values are referred to as integral types.

We usually refer to a chunk of 8 bits as a "byte" and 32 bits, or 4bytes, as a "word".

The integers, int, short and long, are signed by default. To get an unsigned type, the type must be specified asunsigned.

Arithmetic Types
TypeMeaningMinimum SizeNote
boolbooleanNA0 means false, nonzero means true
charcharacter8 bitsIt usually represent 1 byte
wchar_twide character16 bitsIt's used to represent Chinese, Japanese, which need more than one byte. 2 bytes
shortshort integer16 bitsTypically, short is in half a machine word 2 bytes
int integer16 bitsTypically, Int is in a machine word 4 bytes
longlong integer32 bitsTypically, long is either one or two machine words 4/8 bytes
floatsingle-precision floating-point6 significant digitsTypically, float is one word (32 bits)
doubledouble-precision floating-point10 significant digitsTypically, doubles is two words (64 bits)
long doubleextended-precision floating-point10 significant digitsTypically, long double is three or four words (93/128 bits)
*float is not precise enough for real programs, double is sufficient for most calculations.

2.2 Literal Constant

a value, such as 42, in a program is known as a literal constant.

literal exits only for the built-in types, there are no literals of class types. Hence, there are no literals of any of the library types.

Rules

1. There are three integer notations: decimal, octal, hexadecimal

20      //decimal
024     //octal, begin with 0(zero)
0x14    //hexadecimal, begin with 0x or 0X

2. We can change the integer type by adding a suffix:

128u    //unsigned, add u or U at the end
1L      //long, add l or L at the end
128UL   //unsigned long
128LU   //unsigned long

3.We can also change the float type by adding suffix

3.14    //by default, it's double
3.14F   //f or F, means float
3.14L   //l or L, means long double
3.14e0  //e or E, means scientific notation

4.We use single quotation marks to represent character, and use double quotation marks to represent string literal

'a'                     //character literal
"a"                     //simple string literal, represent two characters: a and null
L'a'                    //wide-character literal, preceding the character literal with an L
L"Hello"                //wide string literal 
"\nCC\t"                //string literal using newlines and tabs

5.We use escape sequence to represent nonprintable characters and special characters

\n                 \\new line
\?                 \\question mark
\"                 \\double quote

6.String literals in C++ have one character in addition to those typed in by the programmer. Every string literal ends with a null character added by the compiler. To be compatible with C.

7.Two string literals(or two wide string literals) that appear adjacent to one another and separated only by space/newlines are concatenated into a single new string literal.

//concatenated long string literal
std::cout<<"a"
           "b"
         <<std::endl;
output is: a b

8.Putting a backslash as the last character on a line causes that line and the next to be treated as a single line.(No comments or trailing blanks are allowed)

std::cou\
t << "Hi" << std::endl;
equal to
std::cout << "Hi" << std::endl;

2.3 Variables

There are two kinds of expressions in C++:

lvalue: An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment         \\eg. variable

rvalue: An expression that is an rvalue may appear on the right- but not left-hand of an assignment                             \\eg. numeric literals

Identifier is the name of a variable, it can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct.

C++keywords or Operator Alternative Names cannot be used as identifier.

Conventions for variable names:

1.A variable name is normally written in lowercase letters. eg. index

2.An identifier is given a mnemonic name. eg. salary

3.An identifier containing multiple words is written either with an underscore between each word or by capitalizing the first letter of each word. eg.StudentLoan

-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - -

Variable definition starts with a type specifier, followed by a comma-separated list of one or more names. A semicolon terminates the definition.

int month,
     day, year;         //defines three variables of type int
std::string address;    //defines one variable of type std::string

An object defined with a specified first value is spoken of as initialized. C++supports two forms of variable initialization:direct-initialization andcopy-initialization

direct syntax is more flexible and can be slightly more efficient:

int ival (1024);    //direct-initialization
int ival = 1024;    //copy-initialization

Initialization is not assignment. Initialization happens when a variable is created and gives that variable its initial value. Assignment involves obliterating an object's current value and replace that value with anew one.

Each class may define one or more special member functions that say how we can initialize variables of the class type. The member functions that define how initialization works are known as constructors.

For object of built-in types, we can use both direct and copy forms of initialization. For objects of a class types, there are initializations that can be done only using direct-initialization.

//alternative ways to initialize string from a character string literal
std::string titleA = "hello";
std::string titleB ("hello");

std::string all_nines (10, '9');  //all_nines = "9999999999", in this case, initialization can only use direct-initialization

Initialization multiple variables:

//the name of an object becomes visible immediately, so it's possible to initialize a 
//subsequent variable to the value of one defined earlier in the same definition
double salary = 9.9, wage (salary +0.1);

int interval, month = 8, day = 7;                       //ok: mix of initialized and uninitialized.

double price =100.1, discount = 0.8;
double final_price = apply_discount(price, discount);   //we can use the return value of a function to initialize
A. Initialization of variable of built-in type

1.Variables defined outside any function body are initialized to zero.

2.Variables of built-in type defined inside the body of a function are uninitialized

B. Initialization of variable of class type

1.Classes control object initialization by defining one or more constructors

2.Class can define a special constructor called default constructor, which can define what happens if a variable of type type is defined but an initializer is not provided. (regardless of where a variable is defined)

-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - -

Definition of a variable allocates storage for the variable and may also specify an initial value for the variable.

Declaration makes known the type and name of the variable to the program.

Definition is also a declaration

We can declare a name without defining it by using the extern keyword. A declaration that is not also a definition consists of the object's name and its type preceded by the keywordextern.

extern int i;      //declares but does not define i
int i;             //declares and define i

A variable can be declared multiple times in a program, but it must be defined only once.

If an initializer is present, the declaration is treated as a definition even if the declaration is labeled externBecause only definition allocates storage.

extern double pi = 3.14;   // definition

-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - 

A scope is a region of the program, a name can refer to different entities in different scopes.

Most scopes in C++ are delimited by curly braces. Generally, names are visible from their point of declaration until the end the scope in which the declaration appears.

There are different scopes: global scope, local scope, statement scope (class scope, namespace scope).  P 54

Defining an object where the objects first used improves readability.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值