1.wchart_t 是兩個位元組存儲的,用來表示寬字元,如中文,, wchar_t c=L'靠';
2.Intergral 類型的包括: int,short,char,wchar_t,bool ,long ,C++規定隻需要保證他們的最低位元組數,而沒規定每個類型的實際位元組數,一般機器上int,long 一樣. int 其實就是signed int.
如果表示無符号類型,則unsigned int 或者縮成unsigned . 無符号類型的數超過他能表示的範圍,則該數%這個類型的最大表示值 ,取模數. 如果是signed ,C++ standar 沒規定,一般是跟unsigned 的規則.
unsigned short k=65536; //max short is 65536
cout<<k<<endl; //print 0
unsigned short k=65537; //max short is 65536
cout<<k<<endl; //print 1
3. 浮點型: float,double,long double, float 一般隻有6個小數位,而double 有至少10個,足夠我們運算,而long double 往往性能不好. 1.2 這樣的literal constant 預設就是double,
要想float, then 1.2f. or 1.2F, 加上字尾.
4.char a='A' compare char a="A" ,所占用的空間是不一樣的, 使用單引号的那個,隻是占一個位元組,但雙引号的那個,是一個字元數組, 要用\0 結尾的. 如果是wchar_t=L"A",則是\0\0 結尾. 由于A 用一個位元組就可以存了,是以整體數組是這樣的 A\0\0\0,
5. 變量的初始化與指派是兩個不同的概念, 初始化發生在變量在定義時候,賦初始值, include direct-initialize and copy-initial ,這兩中方式在primitive type 沒什麼差別,如
int i=10 ;copy-initialize
int b(10);diret-initialize ,但在class 類型中,有了direct-initailize 我們就可以調用這個類的不同構造函數進行initialize.
ie:std::string a="abcd" //copy-initialize ,we can just invoke the constructor string(char * ),
但有了direct-initialize,我們可以調用不通的contstructor.ie:std:string str1("abcdef"),str2("a",5) ==> str2="aaaaa"
而assignment 是一個摸除舊值,然後copy new value 的過程.
global scope 如果沒有初始化,則compile will set to 0. for primitive type. local scope is undefine. 是以對于local scope 的變量我們最好在定義時候就進行初始化.
對于class 類型,undifined.
6.like C,變量都是先聲明然後是定義,然後才能使用. 定義隻能一次,而聲明與使用則是多次都可以的. ....怎麼算是一個變量的聲明.
int i; 這是一個變量的定義但沒有初始化.,但同時有是聲明. 因為unconstant variable default is :extern int i. 預設就帶extern 了.
extern int i; 這是一個變量的聲明. 如果的定義可以是在另外一個檔案當中.
extern int i=10; decalare and define and init .
7.常量. constant
const int PI=3.14159;常量在聲明的同時就必須初始化.
如果要在另外的檔案也能access,則需要extern const int PI=3.14159;//因為常量的聲明預設沒帶extern ,是local file scope.
9.reference 引用類型,其實隻所引用變量的一個别名,起不占空間,
int i=0;
int &iRef=i;// 引用在聲明的同時也要初始化,且以後都不能改了.
int &iRef2=10;//error.
but
const int &iRef3=10;//ok
定義多個引用:
int &iRef4=i,&iRef5=i,iRef6=i;4,5都是引用,但6不是,是int 類型.
const int PI=3.14159;
const int &rPI=PI;//可以的.
如果沒有加const ,
int &rPI=PI;//則不能,
int k=10;
int &rK=k;
const int &rK2=k; //both are ok. 但這個rK2是不能改變k的值的.rK可以.
rK=10;//ok.
rK2=20;//error.
也即常量的引用可以引用常量,也可以引用一般的變量
8.typdef 可以重新定義一個新的類型表識符.typedef int agg; 那樣用的時候直接寫 age i=10;//跟int i=10;是一樣的.
9.enum COLOR {RED,GREEN=3,BLUE}; 使用enum 定義枚舉,其實隻是定義了常量的集合. RED,GREEN,其實就是常量的名稱.
const RED=0; const GREEN=3,const BLUE=4; 其實是一樣.
COLOR mc=RED;// 但不能用COLOR mc=0;進行指派.因為畢竟使用enum 定義了一種新的類型.
10. class struct 關鍵字定義的類其實是可以互換的,唯一不異樣的是,struct 預設如果沒有通路辨別符的話,是public, but class is private.
class A{
int k; //k is private.
};//不要忘記;号.
sturct A
{
int k;//k is public
};
類的定義包括類的聲明與實作部分. 聲明Interface 在.h 的頭檔案中. implement 在source 檔案.
注意interface 中是不能有成員變量的初始化的.因為member data 是在constructor 中進行初始化的.這跟java是不一樣的.
常量與inline fuction 可以在.h 檔案中定義.
11. header can only be include once. we can use
#ifndef XXX
#define XXX
#include XXX
#endif.
to avoid multi-include header files.