知識結構: 1、if,for,switch,goto 2、#define,const 3、文件拷貝的代碼,動態生成內存,復合表達式,strcpy,memcpy,sizeof 4、函數參數傳遞,內存分配方式,內存錯誤表現,malloc與new區別 5、類重載、隱藏與覆蓋區別,extern問題,函數參數的缺省值問題,宏代碼與內聯函數區別 6、構造和析構的次序,String函數定義
具體實現: 1、if,for,switch,goto if: bool int float pointer char 變量的使用方法 bool bParam; int iParam; float fParam; int* pParam; char cParam; if(bParam) ,if(!bParam); if(iParam == 0 ),if(iParam != 0 ); if(fParam>= -0.00001 && fParam <= 0.00001); if(pParam == NULL),if(pParam != NULL); if(cParam == '\0'),if(cParam != '\0');
if/else/return 的使用方法 if(condition) 可以等價為 return (condition?x:y); { return x; } else { return y; }
for: 執行效率問題: int row,col,sum; int a[100][5]; for(row=0;row <100;row++) 效率低于 for(col=0;col <5;col++) { { for(col=0;col <5;col++) for(row=0;row <100;row++) { { sum = sum+a[row][col]; sum = sum+a[row][col]; } } } }
int i; for(i=0;i <N;i++) 效率低于 if(condition) { { if(condition) for(i=0;i <N;i++) DoSomething(); DoSomething(); else } DoOtherthing(); else } { for(i=0;i <N;i++) DoOtherthing(); }
for (int x=0;x <=N-1;x++) 直觀性差于 for (int x=0;x <N;x++)
switch: switch(variable) { case value1: ... break; case value2: ... break; default: ... break; } switch(c)中的c的數據類型可以是int,char,long,unsigned int,bool. variable必須是整數或者強制為整數,由于char實際上是ASCII碼,所以也可以. c不可以是double,float,char*.
goto: goto主要用于 {... {... {.... goto error; } } }
error: ...
2、#define,const #define和const區別 1、#define C語言 const C語言 C++語言 const常量有數據類型,編譯器會進行類型安全檢查,而#define沒有數據類型, const的常量可以進行調試,但宏常量不能進行調試. 2、const的使用方法 在全局定義 const float PI=3.1415926 在類中定義 class A {... A(int size); const int SIZE; }; A::A(int size):SIZE(size) { ... } 對參數和函數的定義(const只能修飾輸入參數,不能修飾輸出參數) const int x=1; 表示x的值是1,在程序中不能改變; const int* x; 表示x代表的地址所指向的內容是不能改變得; int const* x; 與const int* x;的表示的意思一樣; int * const x; 表示x代表的地址是不能改變的;
當是輸入參數時,不需要是void Func(const int i),void Func(const int& i),可以是void Func(int i) 因為輸入參數采用"值傳遞"(const int i),由于函數將自動產生臨時變量用于復制該參數,該輸入參數本來就無需保護,所以不要加const修飾; 不用const int& i的原因在于內部數據類型的參數不存在構造、析構的過程,而復制也非常快,"值傳遞"和"引用傳遞"的效率幾乎相當.
當是輸入參數時,不需要是void Func(const A a),void Func(A a),可以是void Func(A& a)或void Func(const A& a) 不用const A a,A a的原因是函數的效率比較低,因為函數體內將產生A類型的臨時對象用于復制參數a,而臨時對象的構造、復制和析構過程都需要消耗時間 最好用const A&a的原因是A&a中的a可以被改變,A&a和const A&a的好處在于都不會產生臨時對象,效率高;
const A Func(const A&a )const的好處 第一個const表示返回的是個內部產生的對象,它不能被修改 const A Func(...) {...} const A a=Func(...);//不能是A a=Func(...); 第二個const表示輸入的參數是引用傳遞,函數內部不會產生臨時對象,而且這個對象不能被內部修改 第三個const表示此函數內部的所涉及的數據成員不能修改 class Stack { int m_num; int GetCount(void) const; int Pop(void); } int Stack::GetCount(void) const { m_num++;//編譯錯誤,企圖修改數據成員m_num; Pop();//編譯錯誤,企圖調用非const函數 }
3、文件拷貝的代碼 #include <stdio.h> int main(int argc, char* argv[]) { printf("Hello World!\n"); FILE* in; FILE* out; in=fopen("d:\\1.txt","rb"); out=fopen("d:\\2.txt","wb"); char ch=fgetc(in); while(!feof(in)) { fputc(ch,out); ch=fgetc(in); } fclose(in); fclose(out); return 0; }
動態生成內存的代碼 ------------------------------------------ 正確代碼: void GetMemory(char **p, int num) { *p = (char *)malloc(sizeof(char) * num); } char* GetMemory2(int num) { char* p = (char *)malloc(sizeof(char) * num); return p; } ------------------------------------------ 錯誤的代碼: void GetMemory3(char *p, int num) { p = (char *)malloc(sizeof(char) * num); }
------------------------------------------ void Test(void) { char *str = NULL; GetMemory(&str, 100); // 注意參數是&str,而不是str strcpy(str, "hello"); cout < < str < < endl; free(str);
str=NULL; str=GetMemory2(100); strcpy(str, "hello"); cout < < str < < endl; free(str);
str=NULL; GetMemory3(str, 100); // str 仍然為NULL strcpy(str, "hello"); // 運行錯誤 cout < < str < < endl;//運行錯誤 free(str);//運行錯誤 }
strcpy代碼 char* strcpy(char* strDest,const char* strSrc) { if(strDest==NULL||strSrc==NULL) return NULL; char* pStr=strDest; while((*strDest++=*strSrc++)!='\0) NULL; return pStr; }
復合表達式 d = (a = b + c) + r ; 該表達式既求a 值又求d 值.應該拆分為兩個獨立的語句: a = b + c; d = a + r;
if (a < b < c) // a < b < c 是數學表達式而不是程序表達式 并不表示 if ((a <b) && (b <c)) 而是成了令人費解的 if ( (a <b) <c )
memcpy代碼 void* memcpy(char* strDest,const char* strSrc,size_t size) { if(strDest==NULL||strSrc==NULL) return NULL; if(size <=0) return NULL; char* pStr=strDest; while(size-->0) *strDest++=*strSrc++; return pStr; }
sizeof: i.在32位操作系統中,基本數據類型 類型 字節長度 char 1 short 2 short int 2 signed short 2 unsigned short 2 int 4 long int 4 signed int 4 unsigned int(unsigned) 4 long 4 unsigned long 4 float 4 double 8 void* 4 (所有指針類型長度都一樣)(char*,int*,float*,double*) enum 4
ii.在32位操作系統中,定義或函數中的大小 char a[]="hello"; char b[100]; char *p=a; 類型 字節長度 sizeof(a) 6 sizeof(b) 100 sizeof(p) 4
void Func(char a[100]) { sizeof(a); //4 }
#pragma pack(1) struct A { int i; char j; }; sizeof(A) //5
#pragma pack(1) struct A { int o; int j; union { int i[10],j,k; };
}; sizeof(A) //48
#pragma pack(1) struct A { enum day{monring, moon, aftermoon}; }; sizeof(A) //1 sizeof(A::day) //4
|