C++入門介紹及使用手冊(cè)_第1頁(yè)
C++入門介紹及使用手冊(cè)_第2頁(yè)
C++入門介紹及使用手冊(cè)_第3頁(yè)
已閱讀5頁(yè),還剩27頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、C+門介紹及使用手冊(cè)C+基礎(chǔ)3第一章C+語(yǔ)言的組成部分9第二章變雖與常H10第四章表達(dá)式與語(yǔ)句13第五章函數(shù)16第六章面向?qū)ο缶幊?9第七章程序流程21第八章:指針24第九章引用26第十章高級(jí)函數(shù)28第11章面向?qū)ο蠓治雠c設(shè)計(jì)30第12章實(shí)現(xiàn)繼承31第13章管理數(shù)組和字符串34第14章多態(tài)35第15章特殊類和函數(shù)36第16章高級(jí)繼承39第17章處理流41第18章創(chuàng)建和使用名詞空間45第19章模板47第20章處理錯(cuò)誤和異常48第21章雜項(xiàng)內(nèi)容50C+基礎(chǔ)一、類和對(duì)象(class&object)面向?qū)ο缶幊蹋豪脤?duì)象的屬性和方法來實(shí)現(xiàn)程序或者系統(tǒng)所需的功能;非面向?qū)ο缶幊蹋何募?.cpp

2、、*.c;c+sourcefile;C+源代碼文件(程序代碼資源)*.h;c+headfile;c+頭文件;(定義,類型)*.dsw工程工作區(qū)文件;*.dsp工程文件二、c+常用數(shù)據(jù)類型(DataTypes)與變H整型:intx;長(zhǎng)整型:long;單精度類型:float雙精度類型:double邏輯型:bool字符類型:char字符串型:CString字節(jié)類型:byte(0255)10e+5=10*105?數(shù)組:類型數(shù)組名數(shù)組大小floatafRed4=(0.0f,2.0f,3.0f,4.0f);數(shù)組元素賦值:afRed2=1024.568*2.0+3;數(shù)組內(nèi)存分配:1. floatafRed8

3、float*d=(float*)malloc(8*sizeof(float);float*d=newfloat8(2)三、基本語(yǔ)法;/*/n換行函數(shù)類型名函數(shù)名(參數(shù)類型名參數(shù)變量).程序代碼;returnn;/返回值longsum(inti)(longn=0;for(intj=1;j<=i;j+)(n=n+j;2. returnn;/返回值語(yǔ)法:3. 運(yùn)算:+-*/=i+i一sincostanatanabsfmod;語(yǔ)句:#include"math.h"/包含頭文件#definest100;定義常數(shù)return100;/返回值循環(huán):for(intj=1;j<=

4、i;j+)n=n+j;/intj=1;don=n+j;j+;while(j<=i);/判斷:if(條件)if(j!=100&&i=100)(.else(if(j>1001)n=n+1;elsen=n+2;/switch/switch(i)(case0:n=1000;break;case1:n=2000;break;case2:n=2000;break;case3:n=2000;break;default:n=i;break;5、指針:(1).指針變雖:類型名*變雖名int*I;intmain(intargc,char*argv)(intm;intn;sum(100,

5、&m,&n);longs1=m;longs2=n;/CStringss;/s.format(printf("從1加到100的奇數(shù)總和是:dn從1加到100的偶數(shù)總和是:dn”,s1,s2);return0;voidsum(inti,int*s1,int*s2)/從1到i累加(*s1=0;*s2=0;for(intj=1;j<=i;j+)(if(fmod(j,2)=0)*s1=*s1+j;Return2;else*s2=*s2+j;Return;類與對(duì)象#include"stdafx.h"#include<iostream>clas

6、sCat(public:intGetAge();intSetAge(intv);voidMeow();private:intitsAge;protected:;intCat:GetAge()(returnitsAge;intCat:SetAge(intv)(itsAge=v;return0;voidCat:Meow()(std:cout<<"Meow.n”;intmain(intargc,char*argv)(Catwhitecat;whitecat.SetAge(10);whitecat.Meow();std:cout<<"whitecatisac

7、atwhois.n"std:cout<<whitecat.itsAge<<"yearsold.n"whitecat.Meow();6. return0;概念:類:對(duì)象:指針:第一章C+語(yǔ)言的組成部分1. cout函數(shù):#include<iostream>std:cout<<5/8;std:cout<<(float)5/8;std:cout<<std:endl;2. ?cin函數(shù)(輸入)intv;std:cin>>v;名詞空間Namespace:簡(jiǎn)化代碼Usingstd:cout;U

8、singstd:endl;cout<<endl;cout<<5/8;Usingnamespacestd;cout<<endl;cout<<5/8;cout<<(float)5/8;cout<<endl;cout<<(float)5/8;cout<<endl;3.注釋/或者/*與*/第二章變雖與常雖4. 變雖是存儲(chǔ)信息的地方。在內(nèi)存地址中存儲(chǔ)或者寫入數(shù)據(jù);RAM(RandomaccessMemory,內(nèi)存)隨機(jī)存取存儲(chǔ)區(qū):臨時(shí)存儲(chǔ);最小單元:byteC+關(guān)鍵字(不能用之命名變雖或者函數(shù))C+;If;W

9、hile;For;Main;變H定義:intI;i=-10;unsignedinti;i=-10;(錯(cuò)誤)IntI,j,k,l;LongI,j,klongintI,j,kShortI,j,kshortintI,j,kLong是Longint的簡(jiǎn)寫;short是shortint的簡(jiǎn)寫;typedef自定義數(shù)據(jù)類型typedefunsignedshortintUSSHORT;USSHORTI,j,k;(等價(jià)于:unsignedshortintI,j,k;)6.short與long的回繞shortI;I=3276732767I+-32768I+-327677.特殊字符:tTab鍵盤n新行r回車鍵b回

10、退f換頁(yè)8.常數(shù)定義;(常數(shù)值不能更改)#definestuperclass15;或者:constunsignedshortintstuperclass=159.枚舉型常Jl(enum)enumCOLORred,green,blue,white0,1,2,3EnumCOLORred=100,green,blue=500,white100101500501COLORmycolor;Mycolor=red;例:intmain(intargc,char*argv)enumWEEKDAYSun,Mon,Tues,Wedn,Thur,Frid,Satur;WEEKDAYmyday;myday=Sun;i

11、f(myday=Sun|myday=Satur)printf("it'sweekendn");elseprintf("backtowork!n");return0;第四章表達(dá)式與語(yǔ)句1. 避免給常數(shù)賦值35=25;(錯(cuò)誤)35=I;(錯(cuò)誤)constintI=100;1=101;(錯(cuò)誤)注意數(shù)據(jù)類型;unsignedintI;1=100;(正確)I=-100;(錯(cuò)誤)交換變雖值inta=100;intb=200;inttemp=a;a=b;b=temp;a=b;b=a;(錯(cuò)誤)inti,j;inti,j;j=100;j=100;i=j+;i=+j

12、;結(jié)果:I=100;j=101結(jié)果:I=101;j=1012. 表達(dá)式:3. 任何一個(gè)計(jì)算值的操作。表達(dá)式總能返回一個(gè)值;x=a+b;x=y;復(fù)雜表達(dá)式y(tǒng)=x=a+b如果a=3,b=2,貝U:x=5;y=5;數(shù)學(xué)運(yùn)算符+-*/%(求余數(shù),求模)+-5/3=1;(float)5/3=1.66666666x+=2表示:x=x+2x-=2表示:x=x-2優(yōu)先級(jí)intn=3*6+2+5*4+3*(2*3+1)關(guān)系運(yùn)算符a>b;a<b;a>=b;a<=b;a=b;a!=bif語(yǔ)句else語(yǔ)句inti=10,j;if(i<=10)if(i>5)j=5;elsej=i;e

13、lseif(i>1000)j=0;elsej=i*2;邏輯運(yùn)算符&&與|或!非if(a>=10&&a<=20);if(a<=10|a>=20);if(!(a=10)11.條件運(yùn)算符intk=(a>b)?a:b;(表達(dá)式1)?表達(dá)式2:表達(dá)式3(如a=10,b=15,責(zé)k=15)第五章函數(shù)1. 什么是函數(shù):函數(shù)是能夠?qū)?shù)據(jù)進(jìn)行處理并返回一個(gè)結(jié)果的子程序包括內(nèi)置函數(shù)和自定義函數(shù);局部變H與全局變H#include"stdafx.h"intadd(intv1,intv2,ints);/函數(shù)原型intadd(int

14、v1,intv2,int*s);/函數(shù)原型intsum=0;/全局變量intmain(intargc,char*argv)printf("HelloWorld!n");/printf("nn");intvv=0;intv=add(1000,250,vv);printf("V:%dn",v);printf("VV:%dn",vv);/printf("nn");int*vv2=newint1;intv2=add(1000,250,vv2);printf("V:%dn",v2);p

15、rintf("VV:%dn",*vv2);/printf("nn");intvv3=0;intv3=add(1000,250,&vv3);printf("V:%dn",v3);printf("VV:%dn",vv3);printf("nn");/printf("sum:%dn",sum);printf("nn");return0;intadd(intv1,intv2,ints)s=v1+v2;sum=s;/此處sum為全局變量returns;int

16、add(intv1,intv2,int*s)*s=v1+v2;2. intsum=*s;/此處sum為局部變量return*s;默認(rèn)參數(shù):原型:longchengfa(intn=5);/函數(shù)的默認(rèn)參數(shù)/longchengfa(intn)longv=1;for(inti=1;i<=n;i+)v=v*i;returnv;調(diào)用:3. longt=chengfa();(結(jié)果為120)或者longt=chengfa(4);(結(jié)果為24)函數(shù)重載:同名函數(shù)定義多次;doubleadd(doublev1,doublev2);/函數(shù)原型intadd(intv1,intv2,ints);/函數(shù)原型inta

17、dd(intv1,intv2,int*s);/函數(shù)原型doubleadd(doublev1,doublev2)returnv1+v2;intadd(intv1,intv2,ints)s=v1+v2;sum=s;returns;intadd(intv1,intv2,int*s)*s=v1+v2;sum=*s;return*s;內(nèi)嵌函數(shù)(避免跳轉(zhuǎn),從而提高程序執(zhí)行效率)inlineintAdd(intv1,intv2);/原型intAdd(intv1,intv2)4. returnv1+v2;/Add為內(nèi)嵌函數(shù)遞歸longChengfang(intv)/遞歸函數(shù)if(v=1)returnv;els

18、ereturn(v*Chengfang(v-I);如:v=5:Chengfang(5)=5*Chengfang(4)=5*4*Chengfang(3)=5*4*3*Chengfang(2)=5*4*3*2*Chengfang(1)=5*4*3*2*1=120第六章面向?qū)ο缶幊?. 類和對(duì)象類:抽象;對(duì)象:客觀;類的定義#include"stdafx.h"#include<iostream>classCatpublic:類的共有成員方法intGetAge();intSetAge(intv);voidMeow();private:類的私有成員屆性(成員變量)inti

19、tsAge;protected:保護(hù)類型;intCat:GetAge()(returnitsAge;intCat:SetAge(intv)(itsAge=v;return0;voidCat:Meow()(std:cout<<"Meow.n”;intmain(intargc,char*argv)(Catwhitecat;/定義類的對(duì)象:whitecat為Cat類的對(duì)象whitecat.SetAge(10);whitecat.Meow();std:cout<<"whitecatisacatwhois.n"std:cout<<whit

20、ecat.itsAge<<"yearsold.n"whitecat.Meow();2. return0;構(gòu)造函數(shù)與析構(gòu)函數(shù)成員函數(shù)const第七章程序流程循環(huán):goto語(yǔ)句inti=0;intv=0;loop:v=v+i;i+;if(i<5)gotoloop;while循環(huán)intSum(intv)inti=0;intTotal=0;while(i<=v)if(i%2=0)i+;continue;/跳出本次循環(huán),并繼續(xù)下一次循環(huán)if(i>10)break;/退出循環(huán)、Total=Total+i;i+;returnTotal;dowhile循環(huán)lo

21、ngSum2(longv)inti=0;intTotal=0;doTotal=Total+i;i+;while(i<=v);returnTotal;for循環(huán)for(inti=1;i<=10;i+)for(intj=1;j<=20;j+)for(intI=0,j=0;I<3;循環(huán)中止I+,j+)std:cout<<"I:"<<i<<"j:"<<i<<std:endl;switch語(yǔ)句intv,n;switch(v)case0:n=1000;break;case1:n=20

22、00;break;case2:n=3000;break;case3:n=4000;break;default:n=i;break;(如果v=2,則n=3000;如果v=1,則n=2000;)6.7.8.第八章:指針1. (1).指針變雖:類型名*變雖名int*I;intmain(intargc,char*argv)(intm;intn;sum(100,&m,&n);longs1=m;longs2=n;/CStringss;/s.format(printf("從1加到100的奇數(shù)總和是:dn從1加到100的偶數(shù)總和是:dn”,s1,s2);return0;longsum

23、(inti,int*s1,int*s2)/從1至Ui累加*s1=0;*s2=0;for(intj=1;j<=i;j+)(if(fmod(j,2)=0)*s1=*s1+j;else*s2=*s2+j;return0;指針運(yùn)算符:->刪除指針:delete指針名;int*I=newint;deleteI;內(nèi)存泄漏內(nèi)存泄漏迷途指針正確處理方法1正確處理方法2int*i=newint;int*i=newint;int*i=newint;int*i=newint;*I=100;*I=100;*i=100;*I=100;i=newint;deleteI;deleteI;*i=200;*i=20

24、0;*i=200;i=newint;*i=200;2. const指針一般指針Const指針指針常數(shù)Cat*Redcat=newCat;intI=Redcat->itsAge;intj=Redcat->GetAge();Redcat->SetAge(40);(V)ConstCat*Redcat=newCat;intI=Redcat->itsAge;intj=Redcat->GetAge();Redcat->SetAge(40);(錯(cuò)誤)hVCat*constRedcat=newCat;intI=Redcat->itsAge;intj=Redcat-&g

25、t;GetAge();Redcat->SetAge(40);(V)3. 應(yīng)用:指針與數(shù)組intn=10;int*myArray=newintn;/int*myArray=(int*)malloc(n*sizeof(int);/同樣/for(inti=0;i<n;i+)/myArrayi=i;for(inti=0;i<10;i+)*myArray+=i;/等價(jià)于:*myArray=i;myArray+;myArray-=n;/讓指針位置返回指向數(shù)組第一個(gè)元素for(i=0;i<10;i+)printf("myArray%d=%dn”,i,myArrayi);/p

26、rintf("myArray%d=%dn",i,*myArray+);第九章引用1. 什么是引用?引用就是一個(gè)別名(目標(biāo)的別名).2. 引用的語(yǔ)法:類型(空格)&(空格可有可無(wú))變雖名正確錯(cuò)誤Int&I;Int&I;Int&I;Int&I,jint&I;intj;引用的用法inti=100;int&ii=i;ii=200(則:i=200)i=200(則:ii=200)引用不能重新賦值inti=100;int&ii=i;intk=200;ii=k;k=250;/后續(xù)K的變化不引起i和ii的變化;函數(shù)i的值ii的值

27、k的值I=300300300-ii=350350350-K=250350350-ii=k250250250K=500250250500inti=100;int&ii=i;(ii值隨著I值變化,I=100;ii=100)I=150;(則I=100;ii=100)intk=200;ii=k;(ii值不隨著k值變化)(I與ii值變?yōu)?00)k=250;(I與ii值仍然是200)3. 通過引用傳遞參數(shù)(可返回多個(gè)值)voidAddAndSub(intv1,intv2,int&m,int&n)/引用的作用之一:從函數(shù)中返回值(m=v1+v2;n=v1-v2;v1=100;/不能返

28、回;return;4. 傳遞const指針constCat&Function(constCat&WhiteCat)7.第十章高級(jí)函數(shù)重載成員函數(shù)同一個(gè)成員函數(shù)名稱,不同的定義和作用;重載:重新定義與一個(gè)已經(jīng)存在的函數(shù)名稱相同的函數(shù)重載構(gòu)造函數(shù)classCatpublic:Cat()itsAge=100;/構(gòu)造函數(shù)Cat(intv)itsAge=v;/構(gòu)造函數(shù)重載Cat();/析構(gòu)函數(shù)public:intGetAge();intSetAge(intv);voidMeow();intadd(intv1,intv2);doubleadd(doublev1,doublev2);/重載成

29、員函數(shù)addpublic:longsum(longv=10);/使用默認(rèn)值private:intitsAge;boolsex;protected:;/CatBlackCat;1. CatBlackCat(50);復(fù)制構(gòu)造函數(shù)Cat(constCat&rCat)itsAge=rCat.itsAge;?&的好處:不用創(chuàng)建臨時(shí)對(duì)象,所以提高效率;?const作用:使得rCat的數(shù)據(jù)不能被修改;/Cat(CatrCat)itsAge=rCat.itsAge;應(yīng)用:2. CatBlackCat;BlackCat.SetAge(100);CatRedCat(BlackCat);/復(fù)制構(gòu)造函

30、數(shù)初始化對(duì)象classCatpublic:/Cat()itsAge=100;/構(gòu)造函數(shù)Cat():itsAge(100);/初始化對(duì)象運(yùn)算符重載CatblackCat;voidoperator+()+itsAge;/重載運(yùn)算符+Catoperator+(constCat&rcat2);/重載運(yùn)算符+CatRedCat;RedCat+=blackCat;CatCat:operator+(constCat&rcat2)CatTmpCat;TmpCat.itsAge=itsAge+rcat2.itsAge;重載運(yùn)算符+的應(yīng)用returnTmpCat;6.前綴和后綴運(yùn)算符重載const

31、Cat&operator+();前綴+,如:CatBlackCat;+BlackCatconstCat&operator+(int);后綴+,如:CatBlackCat;BlackCat+6.類型轉(zhuǎn)換;Cat(intv)itsAge=v;CatBlueCat=50;或者:CatBlueCat(50);operatorint()return(int(itsAge);intiii=(int)BlueCat;第11章面向?qū)ο蠓治雠c設(shè)計(jì)1. UML(UnifiedModelingLanguage,統(tǒng)一建模語(yǔ)言)軟件開發(fā)過程:分析7設(shè)計(jì)7實(shí)現(xiàn)7測(cè)試第12章實(shí)現(xiàn)繼承1. 繼承與派生概念pr

32、ivate與protected:(只在繼承時(shí)候有區(qū)另U)?private成員只能在本類內(nèi)部才能訪問,派生類中不能訪問;2. ?protected成員可以在派生類中訪問;派生類向基類的構(gòu)造函數(shù)傳遞參數(shù)Dog:Dog(intAge):Mammal(Age)(3. 調(diào)用被覆蓋的基類方法.語(yǔ)法:派生類對(duì)象名.基類名::被覆蓋的基類方法(參數(shù))。例:DogMydog;Mydog.Mammal:Move(2);/Mydog.Move(2);虛函數(shù)、虛方法語(yǔ)法:Virtual函數(shù)定義;通過基類對(duì)象指針,可以調(diào)用同一個(gè)函數(shù)名(虛函數(shù)名)而執(zhí)行的是不同的子類的函數(shù),這就是虛函數(shù)最根本的用途。用處:classCa

33、t(public:Cat()itsAge=100;/構(gòu)造函數(shù)Cat();/析構(gòu)函數(shù)public:virtualdoubleadd(doublev1,doublev2)returnv1+v2;public:longsum(longv)returnv1+v2;public:longcheng(intv1,intv2)returnv1*v2;protected:intitsAge;/classBosCat:publicCatpublic:doubleadd(doublev1,doublev2)returnv1-v2;longsum(longv)returnv1-v2;/Cat*BlackCat=ne

34、wBosCat;/多態(tài)BlackCat->add(10,20);/調(diào)用BosCat:add()結(jié)果=-10BlackCat->sum(10,20);/調(diào)用Cat:sum()結(jié)果=30私有繼承classBosCat:privateCatpublic:doubleadd(doublev1,doublev2)returnv1-v2;longsum(longv)returnv1-v2;私有繼承后,基類的共有成員和保護(hù)成員都變成了派生類的私有6. (private)成員,在類的外部,基類的所有成員不能被訪問;BosCatbsCat;BsCat.cheng(10,20);不能調(diào)用保護(hù)繼承cla

35、ssBosCat:protectedCatpublic:doubleadd(doublev1,doublev2)returnv1-v2;longsum(longv)returnv1-v2;;保護(hù)繼承后,基類的共有成員和保護(hù)成員都變成了派生類的保護(hù)類(protected)成員,在類的外部,基類的所有成員不能被訪I可;第13章管理數(shù)組和字符串1. 指針?biāo)銛?shù)int*x=newint10;for(inti=0;i<10;i+)xi=i;x+=5;intvvv=*x;/vvv=x5;printf("v=*x=%d!n",vvv);int*x=newint10;for(inti=

36、0;i<10;i+)*x+=i;x-=10;回到第1個(gè)元素x+=5;指向第6個(gè)元素intvvv=*x;/vvv=x1+5;printf("v=*x=%d!n",vvv);刪除數(shù)組deletex;刪除一個(gè)指針:int*v;deletev;重新分配內(nèi)存int*x=newint10;for(inti=0;i<10;i+)xi=i;x=newint15;原來的值將丟失例:2. int*y=newint10;for(intj=0;j<10;j+)yj=j;printf("v=*y=%d!n",y8);/8y=newint15;for(j=0;j&

37、lt;15;j+)yj=j+50;printf("v=*y=%d!n",y8);/58字符數(shù)組:chars;s='a'charstring1="hohaiyangbiao"charstring210;strcpy(string2,string1);strncpy(string2,string1,3);strcat(string2,"strcpy");CStrings1=s2+s3;第14章多態(tài)1. 多重繼承classPegasus:publicHorse,publicBird(2. ?java與C#不支持多重繼承;純虛

38、函數(shù)和抽象類.虛函數(shù):Virtual函數(shù)返回類型函數(shù)名稱(參數(shù)列表)純虛函數(shù)語(yǔ)法:Virtual函數(shù)返回類型函數(shù)名稱(參數(shù)列表)=0;例:virtualvoidDraw()=0;抽象類:包含一個(gè)或者多個(gè)純虛函數(shù)的類都是抽象類;抽象類注意事項(xiàng):3. 不要?jiǎng)?chuàng)建這個(gè)類的對(duì)象;而應(yīng)該從其派生;務(wù)必覆蓋從這個(gè)類繼承的純虛函數(shù)抽象類的作用和應(yīng)用(使用方便,以不變應(yīng)萬(wàn)變):第15章特殊類和函數(shù)1. 靜態(tài)成員變Hstatic“屬于類而不是屬于對(duì)象”。classCat(public:Cat(intage):itsAge(age)HowManyCats+;/構(gòu)造函數(shù)Cat()itsAge=100;HowManyC

39、ats+;/構(gòu)造函數(shù)virtualCat()HowManyCats-;析構(gòu)函數(shù)public:intID;staticintHowManyCats;protected:intitsAge;Catmm;intCat:HowManyCats=0;intn=Cat:HowManyCats;intmain(intargc,char*argv)Cat*MyCats=newCat10;CatwhiteCat(100);printf("HowManyCats:n%dn",Cat:HowManyCats);return0;靜態(tài)成員函數(shù);classCatpublic:Cat(intage):i

40、tsAge(age)HowManyCats+;/構(gòu)造函數(shù)Cat()itsAge=100;HowManyCats+;/構(gòu)造函數(shù)virtualCat()HowManyCats-;/析構(gòu)函數(shù)staticintGetHowMany()returnHowManyCats;public:intID;staticintHowManyCats;protected:intitsAge;intCat:HowManyCats=0;intmain(intargc,char*argv)Cat*MyCats=newCat10;CatwhiteCat(100);printf("HowManyCats:n%dn&q

41、uot;,Cat:GetHowMany();return0;注:靜態(tài)成員函數(shù)不能取決于非靜態(tài)成員數(shù)據(jù);如:staticintGetHowMany()returnitsAge;(編譯錯(cuò)誤)函數(shù)指針(以不變應(yīng)萬(wàn)變)語(yǔ)法:返回類型(*函數(shù)指針名稱)(參數(shù)列表)voidCube(int&w,int&h)returnw*h;/函數(shù)void(*pFunc)(int&,int&);函數(shù)指針定義和語(yǔ)法pFunc=Cube;給函數(shù)指針指定函數(shù),注意函數(shù)結(jié)構(gòu)要一致/pFunc=Circle;pFunc(ValOne,ValTwo);通過函數(shù)指針調(diào)用函數(shù)pFunc(10,20)等價(jià)于

42、Cube(10,20);函數(shù)指針數(shù)組void(*pFuncn)(int&,int&);/函數(shù)指針數(shù)組定義和語(yǔ)法pFunci=Cube;/給函數(shù)指針指定函數(shù)pFunci(ValOne,ValTwo);通過函數(shù)指針調(diào)用函數(shù)typedef用于函數(shù)指針(作用:代碼易讀)typedefvoid(*VPF)(int&,int&);/定義名稱為vpf的函數(shù)指針;intmain()VPFpFunc;/類似于intI;pFunc=Cube;成員函數(shù)指針(類似)CatRedCat;void(*pFuncn)(int&,int&);pFunc=RedCat.add;i

43、ntv=pFunc(10,20);/intv=RedCat.add(10,20);第16章高級(jí)繼承1. 聚合2. 類的成員可以是其它類的對(duì)象,稱為聚合;添加友元類A類B類ClassA(public:friendClassB;Public:Setvv()(private:intn;protected:Byy;4ClassB(Public:private:intadd(a->n+=1;);protected:A*a;則:B類可以訪問A內(nèi)的任何成員數(shù)據(jù)和成員函數(shù)(包括私有的);友元函數(shù)classCat(public:Cat():itsAge(100)(;/初始化對(duì)象Cat(intv)(itsA

44、ge=v;/構(gòu)造函數(shù)重載Cat(constCat&rCat)itsAge=rCat.itsAge;/復(fù)制構(gòu)造函數(shù)Cat()(;/析構(gòu)函數(shù)public:intGetAge()returnitsAge;intSetAge(intv)itsAge=v;public:friendintAdd(Cat&T);/友元函數(shù)某類的友元其參數(shù)一般與該類有關(guān);private:intitsAge;intAdd(Cat&T)/友元函數(shù)體returnT.itsAge;/友元函數(shù)的價(jià)值:訪問類的私有成員intmain(intargc,char*argv)CatRedCat;printf("

45、;RedCat'sAgeis%dn",RedCat.GetAge();printf("RedCat'sFriendFunctonis%dn",Add(RedCat);return0;第17章處理流1.cin<<遇空格結(jié)束2.輸入多個(gè)變量:charYourName6std:cout<<”pleaseinputyourName:”pleaseinputyourName:std:cin>>YourName;haohaiuniversitystd:cout<<YourName;haohai3.cin>&

46、gt;ValOne>>ValTwo>>ValThree;charp=cin.get();輸入1個(gè)單字符;chara;cin.get(a);輸入1個(gè)單字符;chara,b,c;cin.get(a).get(b).get(c);連續(xù)分別輸入3個(gè)單字符;輸入帶空格的字符串charStringOne256;charStringOne256;cin.get(StringOne,256);cin.getline(StringOne,256);輸入單個(gè)字符4.3. cin.ignorecharStringOne256;cin.get(StringOne,256);cout<&l

47、t;StringOne<<endl;cin.ignore(255,'n');charStringTwo256;cin.getline(StringTwo,256);2. cout<<StringTwo<<endl;6.putback與peekintmain2()(charch;cout<<"enteraword:"<<endl;while(cin.get(ch)!=0)(if(ch='!')cin.putback('$');/putback將一個(gè)字符插入輸入流中els

48、ecout<<ch;while(cin.peek()='#')/peek查找字符cin.ignore(1,'#');/ignore忽略指定字符return0;刷新輸出(回車)3. cout<<flush();put輸出cout.put('H').putCo').putCH').putCa').put('i');write()chars="hohai123456yang"intln=strlen(s);cout.write(s,ln-3)<<endl;c

49、out.write(s,ln)<<endl;4. cout.write(s,ln+3)<<endl;cout.width(int):保證下一個(gè)輸出量長(zhǎng)度為10調(diào)整輸出寬度;cout<<"abc"cout.width(10);cout<<"abc"5. 結(jié)果:abcabcfill(char)cout<<"abc"cout.width(10);cout.fill(*');cout<<"abc"結(jié)果:abc*abc標(biāo)記:fixed,showb

50、ase,Uppercase,dec,oct,hexcout<<dec<<0x7b<<endl;/123cout<<hex<<123<<endl;/7bcout.setf(ios:showbase);/cout<<hex<<123<<endl;/0x7bcout<<uppercase<<123<<endl;/0x7B常見轉(zhuǎn)換符:CStringss;printf(%s”,ss);IntI;printf(%d”,i);LongI;printf(%l”,i);

51、Doublex;printf(%ld”,x);Floatx;printf(%f”,x);doublei;i=123;CStringss;ss.Format(%ld”,i);6. MessageBox(ss);寫文件(注意:#include<fstream>)charfilename="D:CCMyfileData.txt"ofstreamfout(filename);cin.ignore(1,'n');charbuffer255;cin.getline(buffer,255);fout<<buffer<<"n&q

52、uot;fout.close;讀文件charfilename="D:CCMyfileData.txt"ifstreamfin(filename);charch;while(fin.get(ch)cout<<ch;fin.close();二進(jìn)制文件寫入與讀出寫入讀出ofstreamout(filename,ios:binary);charbuffer255;cin.getline(buffer,255);fout.write(buffer,255);fout.close();ifstreamfin(filename,ios:binary);charbuffer25

53、5;fin.read(buffer,255);cout<<buffer<<endl;fin.close();命令行處理intmain(intargc,char*argv)ifstreamfin(argv1,ios:binary);charbuffer255;fin.read(buffer,255);cout<<buffer<<endl;fin.close();return0;運(yùn)行方法:MyBinaryfileD:CCMyBinaryfileDataBinary.txtShellmspaint1.bmp”第18章創(chuàng)建和使用名詞空間/類pane使用名

54、字空間window,即類pane嵌套在名字空間winndow里。/namespacewindow/類pane使用名字空間window,即類pane嵌套在名字空間winndow里。(constintmax_x=30;constintmax_y=40;classpane(public:pane();pane();voidsize(intx,inty);voidmove(intx,inty);voidshow();private:staticintcnt;intx;inty;intwindow:pane:cnt=0;靜態(tài)成員變量window:pane:pane():x(0),y(0)(構(gòu)造函數(shù)win

55、dow:pane:pane()(析構(gòu)函數(shù)voidwindow:pane:size(intx,inty)(if(x<window:max_x&&x>0)pane:x=x;if(y<window:max_y&&y>0)pane:y=y;voidwindow:pane:move(intx,inty)(if(x<window:max_x&&x>0)pane:x=x;if(y<window:max_y&&y>0)pane:y=y;voidwindow:pane:show()(cout<<"

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 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)論