達(dá)內(nèi)C++培訓(xùn)課件_第1頁(yè)
達(dá)內(nèi)C++培訓(xùn)課件_第2頁(yè)
達(dá)內(nèi)C++培訓(xùn)課件_第3頁(yè)
達(dá)內(nèi)C++培訓(xùn)課件_第4頁(yè)
達(dá)內(nèi)C++培訓(xùn)課件_第5頁(yè)
已閱讀5頁(yè),還剩500頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、1Copyright Tarena Corporation, 2005. All rights reserved.Tarena High-End IT T加拿大達(dá)內(nèi)科技加拿大達(dá)內(nèi)科技( (中國(guó)中國(guó)) )公司公司中國(guó)北京中國(guó)北京電話:(010)62136369、62132839地址:北京市海淀區(qū)北三環(huán)西路甲18號(hào) (大鐘寺附近)中鼎大廈B座7層中國(guó)上海中國(guó)上海電話:(021)61202630、61202603地址:上海市北京東路668號(hào) 上??萍季┏荂區(qū)9層 加拿大多倫多加拿大多倫多電話:(416)491-6456 地址:Suite 1208, Deerford Road, Toronto, O

2、ntario, Canada郵編:M2J 3J3郵件:C 網(wǎng)址:www.tarena.ca中國(guó)中國(guó)廣州廣州電話:(020)85518868、85518898 地址:廣州天河區(qū)崗頂僑鑫教育主樓三層The C+ Programming LanguageThe C+ Programming LanguageChapter 1Chapter 12C+ Programming in UNIXC+ Programming in UNIXu課程介紹lC+ 語(yǔ)法基礎(chǔ)l面向?qū)ο蟪绦蛟O(shè)計(jì)的概念l大量的編程實(shí)踐u目標(biāo)l熟練掌握C+語(yǔ)法l具有面向?qū)ο蟪绦蛟O(shè)計(jì)的概念與能力l能熟練閱讀復(fù)雜的C+程序源代碼l能獨(dú)立的設(shè)計(jì)與

3、完成面向?qū)ο蟮腃+程序3課程內(nèi)容簡(jiǎn)介課程內(nèi)容簡(jiǎn)介 1 1uC+語(yǔ)言基礎(chǔ)l保留字l變量,常量l表達(dá)式l語(yǔ)句l函數(shù)l程序的結(jié)構(gòu)l數(shù)據(jù)結(jié)構(gòu)與算法 數(shù)組、指針、引用、結(jié)構(gòu)、鏈表與棧4課程內(nèi)容簡(jiǎn)介課程內(nèi)容簡(jiǎn)介 2 2uC+面向?qū)ο缶幊蘬類l構(gòu)造函數(shù)與析構(gòu)函數(shù)l靜態(tài)成員與友員l函數(shù)重載l繼承與多態(tài)uI/O流u模板u異常5程序設(shè)計(jì)語(yǔ)言介紹程序設(shè)計(jì)語(yǔ)言介紹1 1uWhat computer understand?lbitsuAssembler LanguagelLimited structurelGlobal scopelMachine codeuPrimitive High-Level LanguagelF

4、unction decompositionlData separationlHigh level structure6程序設(shè)計(jì)語(yǔ)言介紹程序設(shè)計(jì)語(yǔ)言介紹2 2uBlock Structured LanguagelEncapsulationlFlexible data scopinglModularizationuObject-Oriented LanguagelInheritancelPolymorphismlAbstract data types7C+C+程序設(shè)計(jì)語(yǔ)言程序設(shè)計(jì)語(yǔ)言u(píng)1972,AT&T, Bell Lab. Dennis Ritche, C languageu1980,

5、Bell Lab. Bjarne Stroustrup, C extension, u1983, C+ namedu1997, ANSI (American National Standards Institute) C+ (standard C+)8What C+ will we learn What C+ will we learn in this coursein this course ? ?uStandard C+: ANSI C+ is more scalable to different platform such as Unix, Microsoft windows, Macl

6、The standard C+ library got supported by most of industry providers.uYoud better dont know C. .?uWe will try to avoid C library and C syntax.uA C+ file can be with .cc, .cp, .cpp extensions.9為什么選標(biāo)準(zhǔn)為什么選標(biāo)準(zhǔn) C+C+uANSI 規(guī)范了C+的標(biāo)準(zhǔn),使之具有高度的可移植性。u程序能夠運(yùn)行得很快,可直接操作系統(tǒng)資源,保持機(jī)器層次的實(shí)現(xiàn)細(xì)節(jié)。u 不要求圖形環(huán)境, 對(duì)系統(tǒng)要求相對(duì)較低。u易于解決與舊工程的接

7、口以及在數(shù)據(jù)庫(kù),存儲(chǔ)和性能方面的技術(shù)限制。uC+ 是一種面向?qū)ο蟮亩喾缎驼Z(yǔ)言,可以將面向?qū)ο蟮哪P陀成涑蔀镃+ 的結(jié)構(gòu)。它為開(kāi)發(fā)者設(shè)計(jì)和編寫(xiě)一個(gè)解決方案提供了一定的選擇范圍。10C+C+的優(yōu)點(diǎn)的優(yōu)點(diǎn)uSupports data abstraction and object-oriented programminguContains all existing features of C, making the migration from C to C+ relatively easyuIs as portable and efficient as CuCan be linked to exis

8、ting C libraries and functionsuProvides strong static-type checkinguIs a general-purpose language11程序員應(yīng)該具備的計(jì)算機(jī)知識(shí)程序員應(yīng)該具備的計(jì)算機(jī)知識(shí)1 1u操作系統(tǒng)與應(yīng)用程序u運(yùn)行環(huán)境與運(yùn)行機(jī)制l系統(tǒng)與命令l運(yùn)行環(huán)境與命令行參數(shù)l進(jìn)程l棧l堆12Binary and HexadecimalBinary and HexadecimaluBinary: 0101 1000uDecimal: 88uHexadecimal: 0 x581 byte = 8 bits.0123456789ABCDEF0

9、12345678910 1112 13 14 1513程序員應(yīng)該具備的計(jì)算機(jī)知識(shí)程序員應(yīng)該具備的計(jì)算機(jī)知識(shí)2 2u編輯器u編譯器l編譯器的功能l解釋執(zhí)行與編譯執(zhí)行的差別l熟悉自己常用的編譯器,查錯(cuò)能力u連接器u庫(kù)與庫(kù)函數(shù)u系統(tǒng)調(diào)用14軟件開(kāi)發(fā)周期軟件開(kāi)發(fā)周期源程序編輯源程序編輯編譯編譯連接連接調(diào)試調(diào)試運(yùn)行運(yùn)行15Software LifecycleSoftware Lifecycleu開(kāi)發(fā)u測(cè)試u維護(hù)u更新16熟悉你的環(huán)境熟悉你的環(huán)境u可用的UNIX服務(wù)器.21 ..26uTelnet命令介紹:telnet 192.168.

10、0.21Login: use your registered user account.Password: type in your pass word.17創(chuàng)建自己的學(xué)習(xí)帳號(hào)創(chuàng)建自己的學(xué)習(xí)帳號(hào)utelnet login: tarenaPassword: tarena歡迎使用達(dá)內(nèi)科技?xì)g迎使用達(dá)內(nèi)科技( (中國(guó)中國(guó)) )公司開(kāi)放實(shí)驗(yàn)室的服務(wù)公司開(kāi)放實(shí)驗(yàn)室的服務(wù)! !Welcome to the OpenLab of Tarena Technologies Inc. Cananda.請(qǐng)按照以下提示創(chuàng)建您的用戶帳號(hào)請(qǐng)按照以下提示創(chuàng)建您的用戶帳號(hào). .Please follow the steps t

11、o create your own account.(請(qǐng)輸入您要注冊(cè)的帳號(hào)請(qǐng)輸入您要注冊(cè)的帳號(hào))Please enter your new account name:XXXXXXXX (請(qǐng)輸入您的請(qǐng)輸入您的E-Mail地址地址)Please enter your email address: XXXYYY.ZZZ18用自己的帳號(hào)登錄用自己的帳號(hào)登錄UNIXUNIX服務(wù)器服務(wù)器Escape character is .SunOS 5.8login: XXXXXXXChoose a new password.New password: *19程序員經(jīng)常用到的程序員經(jīng)常用到的UNIXUNIX命令命令

12、1 1u簡(jiǎn)單的文件維護(hù)與管理ls, cd, mkdir, rm, cp, mv, cat, moreu源程序的編寫(xiě)vi, edu編譯與連接gcc, g+, ldu運(yùn)行與調(diào)試adb,gdb20程序員經(jīng)常用到的程序員經(jīng)常用到的UNIXUNIX命令命令2 2u查看運(yùn)行狀態(tài)% ps ef % grep aaa a.txt (aaa is the chars in the file name a.txt)% prstat (ctrl D to exit)% kill pid (pid is a process id)21第一個(gè)第一個(gè)UNIXUNIX上的上的C+C+程序程序u用vi編輯器來(lái)編寫(xiě)hello.

13、cc源程序% vi hello.cc/*the first C+ program*/#include using namespace std;/main functionint main()cout Hello world! endl;cout This is my first C+ program.n;22g g的常用參數(shù)的常用參數(shù)u- c 編譯成目標(biāo)文件.ou- o指定輸出文件名,輸出文件名跟在-o后面,用空格分隔。如果不使用這個(gè)選項(xiàng),缺省的輸出文件名為a.out。u- g產(chǎn)生有調(diào)試信息的可執(zhí)行文件u- w不產(chǎn)生警告信息u- l 連接指定的庫(kù)文件u- L指定庫(kù)文件的路徑u- i 要包含的頭

14、文件u- I 頭文件的路徑u- E 顯示預(yù)處理后的程序文件到屏幕上,可以用-o指定輸出到文件u- S 產(chǎn)生匯編程序如果沒(méi)有c、E、S就會(huì)生成可執(zhí)行文件23編譯編譯hello.cchello.cc% g+ -c hello.cc% ls24連接連接hello.ohello.o% g+ -o hello hello.o% ls% g+ hello.o% ls25運(yùn)行運(yùn)行hello hello 程序程序% hello% a.out26C+C+程序的基本結(jié)構(gòu)程序的基本結(jié)構(gòu)1 1/*the first C+ program*/#include using namespace std;/main func

15、tionint main()cout Hello world! endl;cout This is my first C+ program.n;27C+C+程序的基本結(jié)構(gòu)程序的基本結(jié)構(gòu)2 2u#include 與 #include uName space: 提供了一個(gè)全局標(biāo)識(shí)符和全局變量所在的作用域。uint main() u注釋u函數(shù)u函數(shù)的調(diào)用ucout語(yǔ)句28頭文件頭文件u#include語(yǔ)句u#include 與 #include 使用使用#include using namespace std;少用少用#include 29MainMain函數(shù)函數(shù)umain函數(shù)的作用uStandar

16、d C+ main( )格式: int main( ) return 0; /the default return value is 0; 30注釋注釋uC+的注釋31基本的輸出語(yǔ)句基本的輸出語(yǔ)句ucout32練習(xí)程序練習(xí)程序hi.cchi.cc#include using namespace std;int main( )cout Hi Jian! endl;cout Have a nice day. endl;return 0;33練習(xí)程序練習(xí)程序myself.ccmyself.ccu編寫(xiě)一個(gè)程序,打印出自己的:l姓名l性別l年齡l家庭住址l電話號(hào)碼l愛(ài)好u每一條信息輸出為一行34在在hi

17、.cchi.cc中使用字符串中使用字符串#include using namespace std;int main()char name = John;cout Hi name ! endl;cout Have a nice day. endl;return 0;35字符與字符串類型字符與字符串類型u字符,字符串/字符數(shù)組char ch = A;char str120 = Hello world!; char str2 = Have a nice day!;36不同的不同的main()main()格式格式u命令行參數(shù)% ls l (or ls -al )% vi hello.ccu在程序中使用

18、命令行參數(shù)int main(int argc, char *argv )37命令行參數(shù)程序命令行參數(shù)程序cmdline.cccmdline.cc#include using namespace std;int main (int argc, char* argv ) for(int i=0; iargc; i+) cout argv i = argvi endl; 38使用命令行參數(shù)的使用命令行參數(shù)的hi.cchi.ccu使用命令行參數(shù),重新編寫(xiě)練習(xí)程序 cmdline.cc% hi John% hi Lisa% hi G. Bush39基本輸入語(yǔ)句基本輸入語(yǔ)句ucin語(yǔ)句u使用cin語(yǔ)句的h

19、i.ccl重寫(xiě)hi.cc程序,不帶命令行參數(shù)l程序自動(dòng)提示用戶輸入字符串來(lái)獲得姓名與年齡40練習(xí)程序練習(xí)程序age.ccage.cc#include using namespace std;int main() unsigned int age; char name 50; cout please enter your name: name; cout please enter your age: age; cout your name is: name endl; cout You were age -2 ch; if (ch = y) /note the difference: if (

20、ch = y) cout good endl; else cout try again. endl; 42練習(xí)程序練習(xí)程序grade.ccgrade.ccuThis is your assignment.43Q & AQ & A44Copyright Tarena Corporation, 2005. All rights reserved.Tarena High-End IT T加拿大達(dá)內(nèi)科技加拿大達(dá)內(nèi)科技( (中國(guó)中國(guó)) )公司公司中國(guó)北京中國(guó)北京電話:(010)62136369、62132839地址:北京市海淀區(qū)北三環(huán)西路甲18號(hào) (大鐘寺附近)中鼎大廈B座7層中國(guó)上海中

21、國(guó)上海電話:(021)61202630、61202603地址:上海市北京東路668號(hào) 上??萍季┏荂區(qū)9層 加拿大多倫多加拿大多倫多電話:(416)491-6456 地址:Suite 1208, Deerford Road, Toronto, Ontario, Canada郵編:M2J 3J3郵件:C 網(wǎng)址:www.tarena.ca中國(guó)中國(guó)廣州廣州電話:(020)85518868、85518898 地址:廣州天河區(qū)崗頂僑鑫教育主樓三層The C+ Programming LanguageThe C+ Programming LanguageChapter 2Chapter 245字符集字符集

22、uabcdefghijklmnopqrstuvwxyzuABCDEFGHIJKLMNOPQRSTUVWXYZu0 1 2 3 4 5 6 7 8 9u_u+ - * / % =u. , : ? | ! # &u( ) u空白46C+C+的保留字的保留字auto, bool, break, case, catch, char, class, const, const_cast , continue, default, delete, do, double, else, enum, extern, false, float, for, friend, goto, if, inline, i

23、nt, long, new, operator, private, protected, public, return, short, signed, sizeof, static, struct, switch, template, this, throw, true, try, typedef, union, unsigned, virtual, void, while, 47常量與變量常量與變量u內(nèi)存u程序的內(nèi)存使用l常量l變量l動(dòng)態(tài)內(nèi)存u變量的類型48C+C+變量名變量名u變量名(identifier)l第一個(gè)字符必須是字母或下劃線l只能使用字母,數(shù)字,或下劃線l中間不能有空格l不能是

24、保留字,不能與全局函數(shù)和數(shù)據(jù)類型同名uC+嚴(yán)格區(qū)分大小寫(xiě)(UNIX中)u使用易懂的變量名(一般是相關(guān)的英語(yǔ)單詞或者縮寫(xiě))u長(zhǎng)度一般不要超過(guò)32個(gè)字符不單是變量名,所有需要自己起名的地方都需要遵守這樣的規(guī)則。49C+C+變量變量uC+是強(qiáng)類型語(yǔ)言l每一個(gè)變量都有確定的類型,且保持不變u基本數(shù)據(jù)類型l整型,int,l字符型, char,l實(shí)型, float, double,l邏輯型, bool ( standard c+ new feature ! )50基本數(shù)據(jù)類型基本數(shù)據(jù)類型1 1char, unsigned char, signed char, int, unsigned int, sign

25、ed int, short int, unsigned short int, signed short int, long int, signed long int, unsigned long int, float, double, long double,boolvoid:enum,struct,union,array,pointer,class51基本數(shù)據(jù)類型基本數(shù)據(jù)類型2 2u實(shí)型數(shù)據(jù) (float, double) 無(wú)unsigned.52The standard C+ new featuresThe standard C+ new featuresubool: 其值必為關(guān)鍵字tru

26、e 或false u四個(gè)轉(zhuǎn)型運(yùn)算子:lstatic_cast: compiling time to check data type(primitive).lconst_cast: only for constant data type converting.ldynamic_cast: usually for top-down data type cast.lreinterpret_cast: more general cast for all data types.(到多態(tài)的時(shí)候再詳細(xì)講)53變量與數(shù)據(jù)類型變量與數(shù)據(jù)類型uC+是強(qiáng)類型語(yǔ)言l先聲明,后使用uC+編譯器對(duì)變量聲明的處理54一個(gè)使

27、用變量的程序例子一個(gè)使用變量的程序例子#include using namespace std;int main( )int i;i = 5;cout i = i endl;i = 8;cout i = i endl;return 0;55另一個(gè)使用變量的例子程序另一個(gè)使用變量的例子程序#include using namespace std;int main( )i = 5; /see what happenscout i = i endl;i = 8;cout i = i endl;return 0;int i;56變量與變量的變量與變量的sizesizeu變量都有類型u變量在內(nèi)存中的大小

28、int i;double d;cout size of i is sizeof( i ) endl;cout size of int is sizeof( int ) endl;cout size of d is sizeof( d ) endl;cout size of double is sizeof( double ) endl;57程序程序size.ccsize.ccu編寫(xiě)一個(gè)程序,打印出所有C+基本類型的大小#include using namespace std;int main() cout size of char is: sizeof(char) endl; cout siz

29、e of unsigned char is: sizeof(unsigned char) endl; cout size of signed char is: sizeof(signed char) endl; cout size of int is: sizeof(int) endl; cout size of unsigned int is: sizeof(unsigned int) endl; cout size of signed int is: sizeof(signed int) endl; cout size of short int is: sizeof(short int)

30、, =, =, !=!, &, |& | u結(jié)合性u(píng)優(yōu)先級(jí): see table 3-1 in page 35 in the recommended book 1.62運(yùn)算符的使用運(yùn)算符的使用uif ( demo = 2 ) 與 if (demo = 2 )if ( 2= demo ) /左值與右值。uif ( demo != 2 ) 與 if ( demo =! 2 )63運(yùn)算符的優(yōu)先級(jí)運(yùn)算符的優(yōu)先級(jí)int a=8,b=4,c=5;cout (a%b ? b : c);cout a%b ? b:c;64變量的賦值變量的賦值u賦值表達(dá)式u變量的初始化u一次聲明多個(gè)變量u聲明并初

31、始化變量65無(wú)符號(hào)類型的值無(wú)符號(hào)類型的值u無(wú)符號(hào)整數(shù)類型的回繞unsigned short int snum;snum = 65535;cout snum= snum endl;snum = snum + 1;cout snum= snum endl;66有符號(hào)類型的值有符號(hào)類型的值u有符號(hào)整數(shù)類型的回繞int inum = 2147483647;cout inum = inum endl;inum = inum + 1;cout inum = inum endl;67常用類型的取值范圍常用類型的取值范圍u常用類型的取值范圍int i = 65535;int j = 65535;cout i*

32、j/9 endl;68練習(xí)練習(xí)u為表示如下數(shù)據(jù),應(yīng)該使用什么類型的變量:l年齡l姓名l工資l電話號(hào)碼l身份證號(hào)碼l西三環(huán)到東三環(huán)的距離69練習(xí)程序練習(xí)程序bin.ccbin.cc#include using namespace std;int main( ) int a = 10; cout Please enter a number: a; unsigned int r; int k; unsigned int j; char str33; memset(str, 0, 33); str32=0; r = a; int i = 32;70練習(xí)程序練習(xí)程序bin.ccbin.ccdo j =

33、r; r = r/2; k = j - r * 2; if (k) str-i = 1; else str-i = 0; while(r != 0); cout str endl;71枚舉類型枚舉類型enum colorRED, GREEN, BLUE, WHITE, BLACK;color tvColor = GREEN;uEnum constant variable does not need memory allocation.uThe default value in Enum is: 0, 1, 2, 3, 4.uSpecify the element values: enum co

34、lor RED=100, GREEN=200, BLUE, WHITE=300, BLACK=400 72表達(dá)式表達(dá)式u左值與右值float a;a = 5/2;+a;u求值順序u特殊表達(dá)式: l ( ? : )l逗號(hào)表達(dá)式: int a, b, c; a = 1, b=a=2, c=b+3;73表達(dá)式的求值順序表達(dá)式的求值順序u求值順序u副作用74表達(dá)式的左值與右值表達(dá)式的左值與右值int a, b, c,d;int e = (a = 1, b = a, c = a+b, d = c + 5);(a = 1, b = a, c = a+b, d = c + 5) = 8;e = (a = 0

35、, b = a + 5, b+2);(a = 0, b = a + 5, b+2) = 9; /ohps , you may get problem. 75練習(xí)程序練習(xí)程序comma.cccomma.cc#include using namespace std;int main( ) int a, b, c,d; int e = (a = 1, b = a, c = a+b, d = c + 5); (a = 1, b = a, c = a+b, d = c + 5) = 8; cout d= d endl;76程序語(yǔ)句程序語(yǔ)句u控制語(yǔ)句u表達(dá)式語(yǔ)句u空語(yǔ)句u語(yǔ)句塊77控制語(yǔ)句控制語(yǔ)句u條件判

36、斷語(yǔ)句if ( ) if ( ) else u循環(huán)控制語(yǔ)句while( ) do while( );for ( ) u多路選擇語(yǔ)句switch( ) case: 78循環(huán)語(yǔ)句循環(huán)語(yǔ)句ufor語(yǔ)句udowhile語(yǔ)句uwhile語(yǔ)句ubreak語(yǔ)句ucontinue語(yǔ)句79分支語(yǔ)句分支語(yǔ)句uswitch語(yǔ)句switch(ss) case 1:break;case 2:break;default:break;80循環(huán)語(yǔ)句程序例子循環(huán)語(yǔ)句程序例子 chengFa.ccchengFa.ccu編寫(xiě)一個(gè)程序,打印出乘法口訣表l分別使用for語(yǔ)句,dowhile語(yǔ)句,while語(yǔ)句來(lái)實(shí)現(xiàn)The output

37、 is:1x1=11x2=2, 2x2=41x3=3, 2x3=6, 3x3=91x4=4, 2x4=8, 3x4=12, 4x4=161x5=5, 2x5=10, 3x5=15, 4x5=20, 5x5=251x6=6, 2x6=12, 3x6=18, 4x6=24, 5x6=30, 6x6=361x7=7, 2x7=14, 3x7=21, 4x7=28, 5x7=35, 6x7=42, 7x7=491x8=8, 2x8=16, 3x8=24, 4x8=32, 5x8=40, 6x8=48, 7x8=56, 8x8=641x9=9, 2x9=18, 3x9=27, 4x9=36, 5x9=4

38、5, 6x9=54, 7x9=63, 8x9=72, 9x9=8181練習(xí)程序練習(xí)程序year.ccyear.ccuThis is your assignment.u判斷輸入的年份是否是閏年。82Q & AQ & A83Copyright Tarena Corporation, 2005. All rights reserved.Tarena High-End IT T加拿大達(dá)內(nèi)科技加拿大達(dá)內(nèi)科技( (中國(guó)中國(guó)) )公司公司中國(guó)北京中國(guó)北京電話:(010)62136369、62132839地址:北京市海淀區(qū)北三環(huán)西路甲18號(hào) (大鐘寺附近)中鼎大廈B座7層中國(guó)上海中國(guó)上海電話:

39、(021)61202630、61202603地址:上海市北京東路668號(hào) 上??萍季┏荂區(qū)9層 加拿大多倫多加拿大多倫多電話:(416)491-6456 地址:Suite 1208, Deerford Road, Toronto, Ontario, Canada郵編:M2J 3J3郵件:C 網(wǎng)址:www.tarena.ca中國(guó)中國(guó)廣州廣州電話:(020)85518868、85518898 地址:廣州天河區(qū)崗頂僑鑫教育主樓三層The C+ Programming LanguageThe C+ Programming LanguageChapter 3Chapter 384函數(shù)函數(shù)u什么是函數(shù)u函

40、數(shù)的基本要素l參數(shù)l返回值u函數(shù)的聲明與定義u形參與值參u函數(shù)的調(diào)用85定義函數(shù)定義函數(shù) 函數(shù)名( )return u 86函數(shù)定義的例子函數(shù)定義的例子#include using namespace std;void disp(char str )cout This is your string: str endl;int main( )char course1 = C+;char course2 = Java;char course3 = Oracle;char course4 = UNIX;disp( course1 );disp( course2 );87函數(shù)聲明函數(shù)聲明 函數(shù)名( )

41、;void disp( char* );float average(int, int);float average(int a, int b);為什么需要函數(shù)聲明?88調(diào)用函數(shù)調(diào)用函數(shù)u函數(shù)的形參u函數(shù)的調(diào)用過(guò)程l填入值參l獲得返回值89棧的技術(shù)簡(jiǎn)介棧的技術(shù)簡(jiǎn)介u棧的工作原理u函數(shù)的調(diào)用與棧90棧的原理?xiàng)5脑韎 = f1( );j = f2( );cout i endl;cout j endl;cout f1( ) endl; cout f2( ) endl;#include using namespace std;int f1();int f2();int main( )int i, j;

42、i = f1( );j = f2( );cout i endl;cout j =0 & ch=9) ? 1 : 0);u必須先定義,不支持函數(shù)原形(聲明)u不支持結(jié)構(gòu)控制語(yǔ)句95遞歸函數(shù)遞歸函數(shù)u一個(gè)函數(shù)調(diào)用它自己u如何正確的遞歸l必須有結(jié)束的條件l并且該條件一定能夠滿足96使用與不使用遞歸的例子程序使用與不使用遞歸的例子程序u編寫(xiě)一個(gè)程序bigsum.cc ,使用一個(gè)函數(shù)來(lái)求n的值:#include using namespace std;int bigsum(int a) if(a = 0) return 0; return a + bigsum(a - 1);int main()

43、 int n; cout n; int m = bigsum(n); cout The sum is: m; cout endl; 97練習(xí)程序練習(xí)程序nbang.ccnbang.ccu使用遞歸函數(shù),編寫(xiě)一個(gè)程序來(lái)求n!的值:uThis is your assignment98函數(shù)的重載函數(shù)的重載uC+中的函數(shù)可以重載u什么是函數(shù)的重載: 對(duì)于在不同類型上作不同運(yùn)算而又用同樣的名字的情況,稱為重載。u函數(shù)重載的注意事項(xiàng): 重載函數(shù)至少在參數(shù)個(gè)數(shù),參數(shù)類型, 或參數(shù)順序上有所不同。99函數(shù)重載的例子函數(shù)重載的例子u求兩個(gè)數(shù)的平均值double Average(int, int);double A

44、verage(float, float);double Average(double, double);double Average(long, long);100思考題思考題u 在一個(gè)程序中定義了這兩個(gè)函數(shù)會(huì)怎樣?int Area(int width, int length=1);int Area(int size);101Area.cc source codeArea.cc source code#include using namespace std;int Area(int width, int length = 1) return width * length;int Area(in

45、t size) / int Area(int size, int leng) is not allowed return size * size;int main( ) cout Area(3, 5) endl; /set length = 5 instead. cout Area(10) endl; /is this ok?102函數(shù)參數(shù)的函數(shù)參數(shù)的constconst限定限定u可以使用const限定詞來(lái)修飾形參以保護(hù)實(shí)參不被修改。uconst形參的意義#include using namespace std;void disp(const int I)cout I endl;I = 100

46、; / think about main( )disp(50);103程序的結(jié)構(gòu)程序的結(jié)構(gòu)u多文件結(jié)構(gòu)u外部變量與內(nèi)部變量u變量的作用域與可見(jiàn)性u(píng)頭文件u靜態(tài)全局變量u靜態(tài)函數(shù)104多文件結(jié)構(gòu)多文件結(jié)構(gòu)u按不同的功能模塊將程序的源代碼劃分在多個(gè)文件中u不同源文件之間可以共享變量聲明與類型定義uC+多文件的劃分原則105外部變量與內(nèi)部變量外部變量與內(nèi)部變量u存儲(chǔ)類:auto, extern, register, static, volatile.u什么是外部變量u什么是內(nèi)部變量106變量的作用域與可見(jiàn)性變量的作用域與可見(jiàn)性u(píng)局部變量的作用域u靜態(tài)局部變量的作用域u全局變量的作用

47、域u外部變量的作用域u常量的作用域107頭文件頭文件u頭文件的作用u如何組織頭文件u頭文件的使用u編譯選項(xiàng)108靜態(tài)全局變量靜態(tài)全局變量u何為靜態(tài)全局變量l只在本源文件中可用109靜態(tài)函數(shù)靜態(tài)函數(shù)u定義靜態(tài)函數(shù)l只在本源文件中可用110改寫(xiě)改寫(xiě)bigsum.ccbigsum.cc與與nbang.ccnbang.cc程序程序u將bigsum.cc與nbang.cc改寫(xiě)成多文件結(jié)構(gòu),每個(gè)文件只能有一個(gè)函數(shù)111改寫(xiě)改寫(xiě)bigsum.ccbigsum.cc與與nbang.ccnbang.cc程序程序u分別將前面的函數(shù)改為靜態(tài)函數(shù),再次編譯并運(yùn)行所有的程序uThis is your assignmen

48、t112練習(xí)程序練習(xí)程序hash.cchash.cc#include using namespace std;int main( ) char line100; cout Please enter a string: line; int ch = 0; for(int i=0; istrlen(line); i+) ch += linei; cout The hash of your string is : ch endl;113練習(xí)程序練習(xí)程序mywc.ccmywc.ccu輸入一行文字,統(tǒng)計(jì)單詞的個(gè)數(shù)。u為了輸入的時(shí)候能包含空格,程序使用了getline函數(shù)。#include using n

49、amespace std;int main( ) cout Please enter a line: endl; char line120; int cnt = 0; cin.getline(line, 120); int i = 0;114練習(xí)程序練習(xí)程序mywc.ccmywc.ccwhile (istrlen(line) if (linei != ) cnt +; while (linei != ) & (i strlen(line) ) i+; i +;cout Total cnt words. endl;115Q & AQ & A116Copyright Tar

50、ena Corporation, 2005. All rights reserved.Tarena High-End IT T加拿大達(dá)內(nèi)科技加拿大達(dá)內(nèi)科技( (中國(guó)中國(guó)) )公司公司中國(guó)北京中國(guó)北京電話:(010)62136369、62132839地址:北京市海淀區(qū)北三環(huán)西路甲18號(hào) (大鐘寺附近)中鼎大廈B座7層中國(guó)上海中國(guó)上海電話:(021)61202630、61202603地址:上海市北京東路668號(hào) 上??萍季┏荂區(qū)9層 加拿大多倫多加拿大多倫多電話:(416)491-6456 地址:Suite 1208, Deerford Road, Toronto, Ontario, Canada

51、郵編:M2J 3J3郵件:C 網(wǎng)址:www.tarena.ca中國(guó)中國(guó)廣州廣州電話:(020)85518868、85518898 地址:廣州天河區(qū)崗頂僑鑫教育主樓三層The C+ Programming LanguageThe C+ Programming LanguageChapter 4Chapter 4117復(fù)雜數(shù)據(jù)類型復(fù)雜數(shù)據(jù)類型u數(shù)組u結(jié)構(gòu)u指針118數(shù)組數(shù)組1 1u由若干個(gè)同類型變量組成的集合u數(shù)組的聲明 數(shù)組名元素個(gè)數(shù);u下標(biāo)是數(shù)組元素到開(kāi)始的偏移量u數(shù)組下標(biāo)從0開(kāi)始uchar buf4;buf0buf1buf2buf3119數(shù)組數(shù)組2 2u數(shù)組在聲明時(shí),元素個(gè)數(shù)必須是常量或常量

52、表達(dá)式char buf10;int I;char bufI; /?int I = 10;char bufI; /?const int i = 10;char bufi;char bufi+1;120數(shù)組數(shù)組3 3u如果數(shù)組的聲明帶有初始化,可以直接對(duì)整個(gè)數(shù)組賦值u訪問(wèn)數(shù)組元素,使用下標(biāo)操作符 int iA10;iA0 = 0;iA1 = 1;uint I = 0; I = iA0 + iA1; 121數(shù)組的初始化數(shù)組的初始化u在聲明的時(shí)候就初始化int iA5 = 0,1,2,3,4;int iB = 1,2,3;u使用賦值語(yǔ)句初始化數(shù)組iA0 = 0;iA1 = 1;u數(shù)組的邊界問(wèn)題int

53、iC5;iC10 = 100; /run time ?Question: does C+ compiler have array bound checking?122數(shù)組程序例子數(shù)組程序例子u編寫(xiě)一個(gè)程序,從鍵盤(pán)接受一個(gè)字符串,將該字符串顛倒順序,然后打印出來(lái)123練習(xí)程序練習(xí)程序findmax.ccfindmax.cc#include using namespace std;int main( ) int iA = 103, 5, 68, 115, 32, 23, 66, 599, 38, 444; for(int i = 0; i 10; i+) for(int j = 0; j i; j

54、+) int temp; if(iAi iAj) /if(iAi iAj) temp = iAi; iAi = iAj; iAj = temp; cout The bigNumber is: iA9 endl;124多維數(shù)組多維數(shù)組u二維數(shù)組與多維數(shù)組: int iA510;int iB23 = 1,2,3, 4,5,6;int iC246;u 多維數(shù)組的初始化l僅有第一個(gè)維數(shù)可以省去int iB 3 = 1,2,3, 4,5,6, 7,8,9;125數(shù)組練習(xí)程序數(shù)組練習(xí)程序mdim.ccmdim.cc#include using namespace std;int maximum(int 4

55、, int, int);int main ( ) int sg34 = 68,77,73,86, 87,96,78,89, 90, 70, 81, 86; cout the max grade is maximum(sg, 3,4) endl;int maximum(int grade 4, int row, int col) int max = grade00; for (int i=0; i row; i +) for (int j=0; j max) max = gradeij; return max; 126結(jié)構(gòu)結(jié)構(gòu)1 1u將不同類型的相關(guān)數(shù)據(jù)信息組織在一起u是用戶自定義的類型l需要先

56、聲明類型的定義才能使用u結(jié)構(gòu)與數(shù)組的區(qū)別l數(shù)組只是同一個(gè)數(shù)據(jù)類型的聚集l數(shù)組本身不是一個(gè)新的數(shù)據(jù)類型127結(jié)構(gòu)結(jié)構(gòu)2 2struct ; ; ;(結(jié)構(gòu)變量名);struct Person char name20;unsigned long id;float salary;char address200; p1, p2;128結(jié)構(gòu)的賦值結(jié)構(gòu)的賦值1 1u通過(guò)取成員操作(.)來(lái)引用結(jié)構(gòu)變量的元素Person p1 = G.W Bush, 1000010, 1.5 , ZhongGuanChun, Beijing, China;strcpy(, G.W Bush);p1.id = 10

57、00010;p1.salary = 1.5;strcpy(p1.address, ZhongGuanChun, Beijing, China);129結(jié)構(gòu)的賦值結(jié)構(gòu)的賦值2 2u結(jié)構(gòu)賦值的例子Person p1 = G. W Bush, 1000010, 1.5 , ZhongGuanChun, Beijing, China;Person p2 = p1;130結(jié)構(gòu)的存儲(chǔ)模式結(jié)構(gòu)的存儲(chǔ)模式1 1u每一個(gè)成員都有自己的存儲(chǔ)空間u對(duì)每一個(gè)成員的操作都是獨(dú)立的,各元素間不會(huì)相互影響nameidsalary131結(jié)構(gòu)的存儲(chǔ)模式結(jié)構(gòu)的存儲(chǔ)模式2 2#include using namespace std

58、;struct Person char name20;unsigned long id;float salary;int main( )Person p1 = Zhang Weilong, 1000101, 32;cout &p1= &p1 endl;cout &= & endl;cout &p1.id= &p1.id endl;cout &p1.salary= &p1.salary endl;132What are pointers for ?What are pointers for ?uAcces

59、sing array elements.uPassing arguments to a function when the function needs to modify the original argument.uPassing arrays and strings to functions.uObtaining memory from the system.uCreating data structures such as linked lists.133Pointer, address, variablePointer, address, variableint theVariabl

60、e =5; int * pPointer = & theVariable;5200020002001200220032004200520062007200820092010addresstheVariablepPointer134指針指針1 1u編譯器為變量與常量分配存儲(chǔ)空間u任何存儲(chǔ)空間都用地址來(lái)表示u任何變量/常量都有地址u一個(gè)變量的地址也是一個(gè)有意義的值u變量地址也可進(jìn)行運(yùn)算u這個(gè)值也可以用另一個(gè)變量來(lái)存儲(chǔ)u這另一個(gè)變量的類型就是指針類型135指針指針2 2u指針就是用來(lái)存儲(chǔ)其他變量的地址的變量u指針變量自己也有地址u指針是有類型的u指針的類型要跟它所指向的變量的類型一致l整數(shù)指針,浮點(diǎn)數(shù)指針136理解指針的例子程序理解指針的例子程序addr.ccaddr.ccu定義下面的變量,分別輸出它們的值與他們的存儲(chǔ)地址int iVal1 = 1;int iVal2 = 2;double dVal1 = 1.1;do

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論