版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、nC+: C+之父- 貝爾實(shí)驗(yàn)室 Bjarne Stroustrup nC+是更好的C 怎樣學(xué)好程序設(shè)計(jì)語(yǔ)言怎樣學(xué)好程序設(shè)計(jì)語(yǔ)言 n程序=數(shù)據(jù)結(jié)構(gòu)+算法 n掌握語(yǔ)法:基本概念要清晰,基本語(yǔ)法 要熟練 n掌握數(shù)據(jù)結(jié)構(gòu):把握如何將現(xiàn)實(shí)生活中 的事物抽象為程序中數(shù)據(jù)結(jié)構(gòu)的方法 n掌握算法:學(xué)會(huì)如何規(guī)劃計(jì)算機(jī)的工作 方式與順序的方法 n掌握編程思想:用計(jì)算機(jī)的思維方式去 思維 nC+是語(yǔ)法最復(fù)雜,功能最靈活的程序設(shè)計(jì)語(yǔ) 言 n學(xué)習(xí)C+,一方面要注意C+與C在語(yǔ)法上的不 同點(diǎn),更重要的是能從面向過(guò)程式的思維方式 轉(zhuǎn)向面向?qū)ο蟮乃季S方式。 n學(xué)C+,可以對(duì)計(jì)算機(jī)基礎(chǔ)知識(shí)有更深的理解, 可以增強(qiáng)軟件開(kāi)發(fā)基本
2、訓(xùn)練。重在實(shí)踐,對(duì)很 多現(xiàn)象要知其所以然。從這個(gè)意義上說(shuō),C+ 是軟件開(kāi)發(fā)人員的“思維體操”。 C+編程環(huán)境的建立編程環(huán)境的建立 布爾型 (bool) 字符型 (char) 整型 (int) 基本數(shù)據(jù) 類型 實(shí)數(shù)型(float) 自定義類型 在定義后不能再改變其值 常量類型: 整形常量,實(shí)型常量,字符常量, 字符串常量,布爾常量 const T = ; BREAK=6 char c1 = a; char* p = char c2 = *p; 內(nèi)存 1000H a . c1 地址 1000H 3000Hp ac2 new ()/individual objects new ;/arrays de
3、lete ;/individual objects delete ;/arrays int number(); char* strcpy(char* to, const char* from); void exit(int); (); double sqrt(double); /call sqrt() with the argument double(2) double sr2=sqrt(2); /error: sqrt() requires an argument of type double double sq3=sqrt(three); extern void swap(int*, in
4、t*);/a declaration void swap(int* p, int* q)/a definition int t=*p; *p=*q; *q=t; the body of the function #include void fun1(),fun2(),fun3();/函數(shù)聲明 void main() coutIt is in main. endl; fun2(); coutIt is back in main. endl; void fun1() coutIt is in fun1. endl; fun3(); coutIt is back in fun1. endl; /ex
5、ample41 void fun2() coutIt is in fun2. endl; fun1(); coutIt is back in fun2. endl; void fun3() coutIt is in fun3. endl; It is in main.It is back in fun1. It is in fun2. It is back in fun2. It is in fun1. It is back in main. It is in fun3. return ;or return; /1 is implicitly converted to double(1) do
6、uble f() return 1; int* fp() int local=1; return /bad int return local; /bad (); 參數(shù)傳遞的 方式 值調(diào)用 引用調(diào)用 值調(diào)用 地址調(diào)用 / #include void swap1(int x,int y) int temp; temp=x; x=y; y=temp; coutx=x, y=yendl; void main() int a(5),b(9); swap1(a,b); couta=a, b=bendl; Output: x=9,y=5 a=5,b=9 #include void swap2(int *x,
7、int *y) int temp; temp=*x; *x=*y; *y=temp; coutx=*x, y=*yendl; void main() int a(5),b(9); swap2( couta=a, b=bendl; Output: x=9,y=5 a=9,b=5 #include void swap3(int temp=x; x=y; y=temp; coutx=x, y=yendl; void main() int a(5),b(9); swap3(a,b); couta=a, b=bendl; Output: x=9,y=5 a=9,b=5 #include int /int
8、 r=aindex; return r; void main() int a=1,3,5,7,9; f(2,a)=11; for(i=0;i5;i+) coutait; pointer to as initial elements Output: 13117 a0 9 1 3 5 7 a2 r5 11r void f(const Large int strlen(const char*); char* strcpy(char* to, const char* from); int strcmp(const char*, const char*); float fsqrt(const float
9、 void g(double d) float r=fsqrt(2.0f); /pass ref to temp holding 2.0f r=fsqrt(r); /pass ref to r r=fsqrt(d); /pass ref to temp holding float(d) float update(float void g(double d,float r) update(2.0f); /error:const argument update(r);/pass ref to r update(d);/error:type conversion required int a; /文
10、件作用域 void main() int b; /函數(shù)作用域 If(b=0;b=10;b+) int c; /塊作用域 c+=b; void print(double); void print(long); void f() print(1L);/print(long) print(1.0);/print(double) print(1);/error:ambiguous void print(int); void print(const char*); void print(double); void print(long); void print(char); void h(char c,
11、 int i, short s, float f) print(c);/精確匹配: print(char) print(i); /精確匹配: print(int) print(s); /integral promotion: print(int) print(f); /float to double: print(double) print(c); /精確匹配: print(char) print(49); /精確匹配: print(int) print(0); /精確匹配: print(int) print(a); /精確匹配: print(const char*) int add(int
12、,int); void add(int,int);/error:重新定義 int add(int x,int y); void add(int a,int b);/error: 重新定義 int add(int x,int y) return x+y; double add(double x,double y) return x-y; #include int add(int,int); /形參類型不同 double add(double,double); / void main() coutadd(5,10)endl; coutadd(5.0,10.5)endl; int add(int x
13、,int y) coutintendl; return x+y; double add(double x,double y) coutdoubleendl; return x+y; Output: int 15 double 15.5 /#include int min(int a,int b);/形參數(shù)量不同 int min(int a,int b,int c);/ int min(int a,int b,int c,int d);/ void main() coutmin(13,5,4,9)endl; coutmin(-2,8,0)endl; int min(int a,int b) re
14、turn ab?a:b; int min(int a,int b,int c) int t=min(a,b); return min(t,c); int min(int a,int b,int c,int d) int t1=min(a,b); int t2=min(c,d); return min(t1,t2); Output: 4 -2 int add(int x,int y=0);or int add(int x,int y=0) return x+y; add(15);15+0add(15,10);15+10 int add(int x,int y=5,int z=6);/ok int
15、 add(int x=1,int y=5,int z);/error int add(int x=1,int y,int z=6);/error /默認(rèn)形參值在函數(shù)原型 中給出 int add(int x=5,int y=6); void main(void) add(); int daa(int x,int y) return x+y; void fun(int x, int y=0); void fun(int x); void g() fun(3); /error,匹配不明確: fun(3) or fun(3,0)? void fun(int x=7); /error:保持唯一 void
16、 fun(int x=8); void g() /ok:作用域不同 void fun(int x=9); / / #include void fun(int a=1,int b=3,int c=5) couta=a, b=b, c=cendl; void main() fun(); fun(7); fun(7,9); fun(7,9,11); coutOK! endl; Output: a=1,b=3,c=5 a=7,b=3,c=5 a=7,b=9,c=5 a=7,b=9,c=11 OK! Normal inline inline () / inline int add(int x,int y
17、,int z) return x+y+z; void main() int a(1),b(2),c(3),sum(0); sum=add(a,b,c);/replaced by sum=a+b+c; #include #define f(x) x*x void main() int x(2); coutf(x)endl; coutf(x+1)endl; Output: 4 5 #include inline int f(int x) return x*x; void main() int x(2); coutf(x)endl; coutf(x+1)endl; Output: 4 9 f(x)
18、is replaced 2*2 f(x+1) is repalced 2+1*2+1 f(x) is replaced 2*2 f(x+1) is repalced 3*3 - 邊界 外部接口 特定的訪問(wèn)權(quán)限 - class Date public: void SetDate(int y,int m,int d); int IsLeapYear(); void Print(); private: int year,month,day; ; / 成員函數(shù)定義 / 關(guān)鍵字類名 成員類型 分號(hào) 成員函數(shù) 數(shù)據(jù)成員 inline void Date:Print() coutyear.month.day
19、endl; void DataPrint(Date/error:外部訪問(wèn)非法 函數(shù):類的成員; 返回值類型 :() class Date public: void SetDate(int y,int m,int d); int IsLeapYear(); void Print() /類內(nèi)定義 coutyear.month.dayendl; ; private: int year,month,day; ; void Date:SetDate(int y,int m,int n)/定義 year=y; month=m; day=d; /class Point public: void Init(i
20、nt initX,int initY); int GetX(); int GetY(); Private: int X,Y; ; /inline void Point:Init(int initX,int initY) X=initX; Y=initY; inline int Point:GetX() return X; inline int Point:GetY() return Y; /類定義的完整實(shí)現(xiàn) #include class Date public: void SetDate(int y=2002,int m=10,int n=1); int IsLeapYear(); void
21、Print() coutyear.month.dayendl; private: int year,month,day; ; void Date:SetDate(int y,int m,int n) /類定義的完整實(shí)現(xiàn) year=y; month=m; day=d; int Date:IsLeapYear() return (year%4=0 void main() Date date1,date2; /類定義的完整實(shí)現(xiàn) date1.SetDate();/默認(rèn)參數(shù) date2.SetDate(2000,10,1); coutdate2.IsLeapYear()endl; date1.Print
22、(); date2.Print(); Output: 1 2002.10.1 2000.10.1 class Date/date.h public: Date(int yy,int mm,int dd)/構(gòu)造 y=yy; m=mm; d=dd; coutConstructor called. endl; Date()/析構(gòu) coutDestructor called. endl; void Print() couty.m.d.endl; private: int y,m,d; ; Date today=Date(23,6,1983); Date xmas(25,12,1990); Date m
23、y_birthday;/error:沒(méi)有初始化 Date release1_0(10,12);/error:缺少形參 class Date / Date(int y=2002,int m=10,int=1);/缺省參數(shù) Date(int);/重載構(gòu)造函數(shù) Date(const char*); ; Date today;/Date today(2002,10,1); Date tomorrow(2); Date(July 4, 1998); int i(5); int j=i;/ 拷貝構(gòu)造 today(2002,10,10); Date tomorrow=today;/拷貝構(gòu)造 Format:類
24、名:拷貝構(gòu)造函數(shù)名(const 類名Y=y; TPoint(TPoint /拷貝構(gòu)造函數(shù) TPoint() coutDestructor called.endl; int Xcoord() return X; int Ycoord() return Y; private: int X,Y; ; /構(gòu)造和析構(gòu) TPoint:TPoint(TPoint Y=p.Y; coutCopy_initialization Constructor called.n; #include #include tpoint.h TPoint f(TPoint Q); void main() TPoint M(20,
25、35),P(0,0); TPoint N(M);M is a created object,N is a creating object / P=f(N); coutP=P.Xcoord(), P.Ycoord()endl; TPoint f(TPoint Q) coutOK! endl; int x,y; x=Q.Xcoord()+10; y=Q.Ycoord()+20; TPoint R(x,y); return R; Pass by data value Return Rs data value Output: Copy_initialization Constructor called
26、. Copy_initialization Constructor called. OK! Copy_initialization Constructor called. Destructor called. Destructor called. Destructor called. P=30,55 Destructor called. Destructor called. Destructor called. Temporary object 游泳池 過(guò)道 公有成員 派生類 私有成員 公有成員 公有成員 私有成員 公有成員 基類 新成員 能被訪問(wèn) class Location public:
27、 void InitL(int xx,int yy); void Move(int xOff,int yOff); int GetX() return X; int GetY() return Y; private: int X,Y; ; void Location:InitL(int xx,int yy) X=xx; Y=yy; void Location:Move(int xOff,int yOff) X+=xOff; Y+=yOff; class Rectangle:public Location/公有繼承 public: void InitR(int x,int y,int w,int
28、 h); int GetH() return H; int GetW() return W; private: int H,W; ; void Rectangle:InitR(int x,int y,int w,int h) InitL(x,y); W=w; H=h; #include void main() Rectangle rect; rect.InitR(2,3,20,10); /可以訪問(wèn)基類的GetX rect.Move(3,2); coutrect.GetX()“, rect.GetY(), rect.GetH(), rect.GetW()endl; Output: 5,5,10,
29、20 通過(guò)派生類的對(duì)象能訪問(wèn)基通過(guò)派生類的對(duì)象能訪問(wèn)基 類的類的public成員成員。 class Rectangle:private Location/私有繼承 public: void InitR(int x,int y,int w,int h); int GetH() return H; int GetW() return W; private: int W,H; ; void Rectangle:InitR(int x,int y,int w,int h) InitL(x,y);/直接繼承 W=w; H=h; #include void main() Rectangle rect; r
30、ect.InitR(2,3,20,10); rect.Move(3,2); coutrect.GetX(), rect.GetY(), rect.GetH(), rect.GetW()endl; error 通過(guò)派生類的對(duì)象不能訪問(wèn)基類中通過(guò)派生類的對(duì)象不能訪問(wèn)基類中 的任何成員。的任何成員。 通過(guò)派生類的對(duì)通過(guò)派生類的對(duì) 象不能訪問(wèn)基象不能訪問(wèn)基 類中的任何成類中的任何成 員員 派生類名:派生類名() :( 參數(shù)表-1),( 參數(shù) 表-n 派生類新增成員的初始化語(yǔ)句 ; /構(gòu)造函數(shù) #include class B1 public: B1(int i) b1=i; coutConstruct
31、or B1.endl; void Print() coutb1endl; private: int b1; ; class B2 public: B2(int i) b2=i; coutConstructor B2.endl; void Print() coutb2endl; / private: int b2; ; class B3 public: B3(int i) b3=i; coutConstructor B3.endl; int Getb3() return b3; private: int b3; ; class A:public B2,public B1 public: A(in
32、t i,int j,int k,int l); 多基類繼承 /example71 void Print(); private: int a; B3 bb; ; A:A(int i,int j,int k,int l):B1(i),B2(j),bb(k) a=l; coutConstructor A. endl; void A:Print() B1:Print(); B2:Print(); coutabb.Getb3()endl; 對(duì)象 聲明和定義時(shí)基類順序不同 /example71 void main() A aa(1,2,3,4); aa.Print(); Constructor B2.2
33、Constructor B1.1 Constructor B3.3 Constructor A.4 1 2 4,3 /析構(gòu)函數(shù) #include class M public: M(); M(int i,int j); M(); void Print(); private: int m1,m2; ; M:M() m1=m2=0; / M:M(int i,int j) m1=i; m2=j; coutMs constructor called. m1, m2endl; M:M() coutMs destructor called. m1, m2endl; void M:Print() coutm
34、1, m2, ; / class N:public M public: N() n=0; N(int i,int j,int k); N(); void Print(); private: int n; ; M:M(int i,int j,int k):M(i,j),n(k) coutNs costructor called. nendl; N:N() /example73 coutNs destructor called. nendl; void N:Print() M:Print(); coutnendl; void main() N n1(5,6,7), n2(-2,-3,-4); n1
35、.Print(); n2.Print(); Output: Ms constructor called.5,6 Ns constructor called.7 Ms constructor called.-2,-3 Ns constructor called.-4 5,6,7 -2,-3,-4 Ns destructor called.-4 Ms destructor called.-2,-3 Ns destructor called.7 Ms destructor called.5,6 虛基類的引入虛基類的引入 用于有共同基類的場(chǎng)合 聲明聲明以virtual修飾說(shuō)明基類 例:class B1
36、:virtual public B 作用作用主要用來(lái)解決多繼承時(shí)可能發(fā)生的對(duì)同一基類繼承多次而 產(chǎn)生的二義性問(wèn)題. 為最遠(yuǎn)的派生類提供唯一的基類成員,而不重復(fù)產(chǎn)生多次 拷貝 注意:注意: 在第一級(jí)繼承時(shí)就要將共同基類設(shè)計(jì)為虛基類。 B B1B2 C b1 b2 d B1類成員 B2類成員C類對(duì)象 bB類成員 D1類 void fund(); int nVd; B1類 int nV1; D1類 void fun(); void fund(); int nv; int B1:nV1; int B2:nV2; int nVd; B2類 int nV2; B0類 void fun(); int nV;
37、 B0 B1新增成員 B0 B2新增成員 D1新增成員 B0 B0 B1 B2 D1 nV,fun() / #include class A public: A(const char *s) coutsendl; A() ; class B:virtual public A public: B(const char *s1,const char *s2):A(s1)/構(gòu)造函數(shù) couts2endl; ; class C: virtual public A / public: C(const char *s1,const char *s2):A(s1)/構(gòu)造函數(shù) couts2endl; ; cl
38、ass D:public B,public C public: D(const char *s1,const char *s2, const char *s3,const char *s4) :B(s4,s2),C(s2,s3),A(s1)/構(gòu)造函數(shù) couts4endl; ; void main() / D *ptr=new D(class A, class B, class C, class D); delete ptr; Output: class A class B class C class D /運(yùn)算符重載 #include class complex public: comple
39、x(double r=0.0,double i=0.0) real=r;imag=i; complex operator +(complex c2); complex operator -(complex c2); void diaplay() ; private: double real; double imag ; /運(yùn)算符重載 complex complex:operator +(complex) return complex(real+c2.real,imag+c2.imag); complex complex:operator -(complex) return complex(re
40、al-c2.real,imag-c2.imag); void complex: display() cout“(“real“,”imag“)”endl; void main() complex c1(5,4),c2(2,10),c3; c3=c1-c2; c3.display; c3=c1+c2; c3.display(); Output: (3,-6) (7,14) /虛函數(shù)應(yīng)用 #include class Point public: Point(double i,double j) x=i;y=j; virtual double Area() const return 0; privat
41、e: double x,y; ; class Rectangle:public Point public: Rectangle(int i,int j,int k,int l); virtual double Area() const return w*h; private: / double w,h; ; Rectangle:Rectangle(int i,int j,int k,int l):Point(i,j) w=k; h=l; void fun(Point void main() Rectangle rect(3.0,5.2,15.0,25.0); fun(rect); Output
42、: 375 調(diào)用 Rectangle:Area(); 虛函數(shù)聲明 class virtual ()=0; / /純虛函數(shù)使用 #include class Number public: Number(int i) val=i; virtual void show()=0; protected: int val; ; class Hextype:public Number public: Hextype(int i):Number(i) /純虛函數(shù)使用 virtual void show() couthexvaldecendl; ; class Dectype:public Number public: Dectype(int i):Nu
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025版協(xié)議起訴離婚案件財(cái)產(chǎn)評(píng)估與分配服務(wù)協(xié)議3篇
- 2025年鋼材行業(yè)供應(yīng)鏈金融合作協(xié)議范本2篇
- 2025年度個(gè)人藝術(shù)品購(gòu)買連帶擔(dān)保協(xié)議4篇
- 2025年度個(gè)人藝術(shù)品交易傭金協(xié)議書樣本4篇
- 2025年度個(gè)人教育培訓(xùn)課程開(kāi)發(fā)與授權(quán)協(xié)議書3篇
- 2025-2030全球ASME 規(guī)范高壓釜行業(yè)調(diào)研及趨勢(shì)分析報(bào)告
- 2025-2030全球雙向拉伸PET薄膜行業(yè)調(diào)研及趨勢(shì)分析報(bào)告
- 2025年全球及中國(guó)步進(jìn)式爐床行業(yè)頭部企業(yè)市場(chǎng)占有率及排名調(diào)研報(bào)告
- 2025-2030全球半導(dǎo)體濕法工藝泵行業(yè)調(diào)研及趨勢(shì)分析報(bào)告
- 2025-2030全球地下雨水儲(chǔ)存系統(tǒng)行業(yè)調(diào)研及趨勢(shì)分析報(bào)告
- 2024-2025學(xué)年山東省濰坊市高一上冊(cè)1月期末考試數(shù)學(xué)檢測(cè)試題(附解析)
- 江蘇省揚(yáng)州市蔣王小學(xué)2023~2024年五年級(jí)上學(xué)期英語(yǔ)期末試卷(含答案無(wú)聽(tīng)力原文無(wú)音頻)
- 數(shù)學(xué)-湖南省新高考教學(xué)教研聯(lián)盟(長(zhǎng)郡二十校聯(lián)盟)2024-2025學(xué)年2025屆高三上學(xué)期第一次預(yù)熱演練試題和答案
- 決勝中層:中層管理者的九項(xiàng)修煉-記錄
- 幼兒園人民幣啟蒙教育方案
- 臨床藥師進(jìn)修匯報(bào)課件
- 軍事理論(2024年版)學(xué)習(xí)通超星期末考試答案章節(jié)答案2024年
- 《無(wú)人機(jī)法律法規(guī)知識(shí)》課件-第1章 民用航空法概述
- 政治丨廣東省2025屆高中畢業(yè)班8月第一次調(diào)研考試廣東一調(diào)政治試卷及答案
- 2020-2024年安徽省初中學(xué)業(yè)水平考試中考物理試卷(5年真題+答案解析)
- 鑄石防磨施工工藝
評(píng)論
0/150
提交評(píng)論