## C 基礎
strcpy(newstr,str)????????????????????????/*?拷貝?str?到?newstr?*/
expr1???expr2?:?expr3?????????????????????/*?if?(expr1)?expr2?else?expr3?*/
x?=?(y?>?z)???y?:?z;??????????????????????/*?if?(y?>?z)?x?=?y;?else?x?=?z;?*/
int?a[]={0,1,2};??????????????????????????/*?初始化數組?(或者?a[3]={0,1,2};?*/
int?a[2][3]={{1,2,3},{4,5,6}};????????????/*?初始化二維數組?*/
int?i?=?12345;????????????????????????????/*?從?i?轉換成?char?str?*/
char?str[10];
sprintf(str,?"%d",?i);
## C 實例
一個最小化的 C 程式 simple.c:
~~~
#include?<stdio.h>
main()?{
????int?number=42;
????printf("The?answer?is?%i\n",?number);??
}
~~~
編譯: #?gcc?simple.c?-o?simple
#?./simple
The?answer?is?42
## C++ 基礎
*pointer??????????????????????????????????//?指向對象的指針
&obj??????????????????????????????????????//?對象?obj?的地址
obj.x?????????????????????????????????????//?類(對象)?obj?成員?x
pobj->x???????????????????????????????????//?指針?pobj?指向類(對象)成員?x
??????????????????????????????????????????//?(*pobj).x?同?pobj->x
## C++ 實例
來一個稍微現實一點的 C++ 程序,我們在一個頭文件(IPv4.h)中創建一個類并且實現它(IPv4.cpp),然后創建一個程式來使用其功能。這個類的成員方法實現了 IP 地址從一串整數轉換成我們熟知的點分格式。這是一個最小化的 C++ 程式和多源文件(multi-source)的編譯。
### IPv4 class
#### IPv4.h:
#ifndef?IPV4_H
#define?IPV4_H
#include?<string>
namespace?GenericUtils?{??????????????????????????????//?創建?namespace
????class?IPv4?{??????????????????????????????????????//?類定義
????????public:
????????????IPv4();
????????????~IPv4();
????????????std::string?IPint_to_IPquad(unsigned?long?ip);//?成員方法接口
????};
}?//namespace?GenericUtils
#endif?//?IPV4_H
#### IPv4.cpp:
#include?"IPv4.h"
#include?<string>
#include?<sstream>
using?namespace?std;??????????????????????????????//?使用?namespace
using?namespace?GenericUtils;
IPv4::IPv4()?{}???????????????????????????????????//?默認構造/析構函數
IPv4::~IPv4()?{}
string?IPv4::IPint_to_IPquad(unsigned?long?ip)?{??//?成員方法實現
????ostringstream?ipstr;??????????????????????????//?使用字符串流
????ipstr?<<?((ip?&0xff000000)?>>?24)?????????????//?位右移
??????????<<?"."?<<?((ip?&0x00ff0000)?>>?16)
??????????<<?"."?<<?((ip?&0x0000ff00)?>>?8)
??????????<<?"."?<<?((ip?&0x000000ff));
????return?ipstr.str();
}
### 程序 simplecpp.cpp
#include?"IPv4.h"
#include?<iostream>
#include?<string>
using?namespace?std;
int?main?(int?argc,?char*?argv[])?{
????string?ipstr;?????????????????????????????????//?定義變量
????unsigned?long?ipint?=?1347861486;?????????????//?數字形式的?IP
????GenericUtils::IPv4?iputils;???????????????????//?創建一個類的對象
????ipstr?=?iputils.IPint_to_IPquad(ipint);???????//?調研類的成員方法
????cout?<<?ipint?<<?"?=?"?<<?ipstr?<<?endl;??????//?輸出結果
????return?0;
}
編譯和執行:
~~~
#?g++?-c?IPv4.cpp?simplecpp.cpp????????????????#?編譯成目標文件
#?g++?IPv4.o?simplecpp.o?-o?simplecpp.exe??????#?連接目標代碼,生成可執行文件
#?./simplecpp.exe?
1347861486?=?80.86.187.238
~~~
使用?`ldd`?腳本檢查并列出可執行程序所依賴的共享庫文件。這個命令同樣可以用來檢查共享庫的丟失。 #?ldd?/sbin/ifconfig
## 簡單的 Makefile
相應的最小化多源文件(multi-source)編譯 Makefile 顯示如下。每一個命令行必須以?_tab_?開始!可以將一個較長行使用反斜線"\"來分解為多行。
~~~
CC?=?g++
CFLAGS?=?-OOBJS?=?IPv4.o?simplecpp.o
simplecpp:?${OBJS}
${CC}?-o?simplecpp?${CFLAGS}?${OBJS}
clean:
rm?-f?${TARGET}?${OBJS}
~~~