版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、第三章 類與對象,本章主要內(nèi)容,3類 3對象 33構(gòu)造函數(shù)與析構(gòu)函數(shù) 34 類的聚集對象成員 35靜態(tài)成員 36指向類成員的指針 3.7綜合舉例,c+中的類,類是具有相同屬性和行為的一組對象的集合,它為屬于該類的全部對象提供了統(tǒng)一的抽象描述,其內(nèi)部包括屬性和行為兩個(gè)主要部分。 利用類可以實(shí)現(xiàn)數(shù)據(jù)的封裝、隱藏、繼承與派生。 利用類易于編寫大型復(fù)雜程序,其模塊化程度比C中采用函數(shù)更高。,31類,函數(shù)是將邏輯上相關(guān)的語句與數(shù)據(jù)封裝,用于完成特定的功能。 而類則是邏輯上相關(guān)的函數(shù)與數(shù)據(jù)的封裝,它是對所要處理的問題的描述。,31類的定義,類是一種用戶自定義類型,聲明形式: class 類名稱 publi
2、c: 公有成員(外部接口) private: 私有成員 protected: 保護(hù)型成員 ,公有類型成員,在關(guān)鍵字public后面聲明,它們是類與外部的接口,任何外部函數(shù)都可以訪問公有類型數(shù)據(jù)和函數(shù)。,私有類型成員,在關(guān)鍵字private后面聲明,只允許本類中的函數(shù)訪問,而類外部的任何函數(shù)都不能訪問。 如果緊跟在類名稱的后面聲明私有成員,則關(guān)鍵字private可以省略。,保護(hù)類型,與private類似,其差別表現(xiàn)在繼承與派生時(shí)對派生類的影響不同,第5章講。,類的成員,class Clock public: void SetTime(int NewH, int NewM, int NewS);
3、void ShowTime( ); private: int Hour, Minute, Second;/不允許初始化 ;,成員數(shù)據(jù),成員函數(shù),void Clock : SetTime(int NewH, int NewM, int NewS) Hour=NewH; Minute=NewM; Second=NewS; void Clock : ShowTime( ) coutHour:Minute:Second; ,成員數(shù)據(jù),與一般的變量聲明相同,但需要將它放在類的聲明體中。,class complex private: double real; double imag; public: vo
4、id init (double r,double i)real=r; imag=i; double realcomplex()return real; double imagcomplex()return imag; double abscomplex() double t; t=real*real+imag*imag; return sqrt(t); ;,結(jié)構(gòu)與類 P39,結(jié)構(gòu)的擴(kuò)充: struct Savings unsigned accountNumber; float balance; ; void fn() Savings a; Savings b; a. accountNumber
5、=1; b. accountNumber=2; ;,結(jié)構(gòu)與類的區(qū)別區(qū)別: 類中成員的缺省存儲(chǔ)屬性 為私有的 結(jié)構(gòu)體中的缺省存儲(chǔ)屬性為共有的.,3.1.2成員函數(shù)的定義,1. 外聯(lián)成員函數(shù)(外聯(lián)函數(shù)) 例如: void Clock : SetTime(int NewH, int NewM, int NewS) Hour=NewH; Minute=NewM; Second=NewS; ,全局函數(shù): #include using namespace std; class student; void add(student ,void add(student ,int main() student s
6、1,s2; add(s1,90); add(s2,90,91); s1.output(); s2.output(); return 0; ,3.1.2成員函數(shù)的定義,2. 內(nèi)聯(lián)成員函數(shù)(內(nèi)聯(lián)函數(shù)、內(nèi)部函數(shù)、內(nèi)置函數(shù)) 在類中說明原形,可以在類外給出函數(shù)體實(shí)現(xiàn),并在函數(shù)名前使用類名加以限定。也可以直接在類中給出函數(shù)體,形成內(nèi)聯(lián)成員函數(shù)。 P41(1)在類定義體內(nèi)定義內(nèi)聯(lián)成員函數(shù)(隱式聲明) (2)使用關(guān)鍵字inline定義內(nèi)聯(lián)成員函數(shù)(顯示聲明),內(nèi)聯(lián)成員函數(shù)舉例(隱式),class Point public: void Init(int initX,int initY) X=initX; Y=
7、initY; int GetX( ) return X; int GetY( ) return Y; private: int X,Y; ;,內(nèi)聯(lián)成員函數(shù)舉例(顯式),class Point public: void Init(int 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:Get
8、Y( ) return Y; ,3.C程序的多文件結(jié)構(gòu) 三個(gè)文件來保存: (1)類的定義:.h (2)類的實(shí)現(xiàn):*.cpp (3)類的使用:.cpp 舉例:P42例3.1,2對象,類的對象是該類的某一特定實(shí)體,即類類型的變量。 聲明形式: 類名 對象名; 例: Clock myClock;,類中成員的訪問方式,類中成員互訪 直接使用成員名 類外訪問 使用“對象名.成員名”方式訪問 public 屬性的成員,例: 類的應(yīng)用舉例,#include class Clock ./類的聲明 public: void SetTime(int NewH, int NewM, int NewS); void
9、ShowTime( ); private: int Hour, Minute, Second; ;,void Clock : SetTime(int NewH, int NewM, int NewS) Hour=NewH; Minute=NewM; Second=NewS; void Clock : ShowTime( ) coutHour:Minute:Second; ,/.類的實(shí)現(xiàn),void main(void) Clock myClock; myClock.SetTime(8,30,30); myClock.ShowTime( ); ,例3.2P45 例3.3 P46,名字解析和this
10、指針: (1)名字解析: s.set(2,15,2012)就是s.Tdate:set(2,15,2012)的縮寫,因此可以定義兩個(gè)或多個(gè)類的具有相同名字的成員而不會(huì)產(chǎn)生二義性。 (2)this指針 void Tdate:set(int month,int day,int year) this-month=month; this-day=day; this-year=year; ,帶缺?。J(rèn))形參值的成員函數(shù)和重載成員函數(shù),帶缺省形參值的函數(shù),Void clock:SetTime(int NewH=0,int NewM=0,int NewS=0) Hour=NewH; Minute=NewM;
11、 Second=NewS; ,例3.4P48 帶默認(rèn)參數(shù)的成員函數(shù) 例3.5P49 重載成員函數(shù)舉例,構(gòu)造函數(shù)的作用是在對象被創(chuàng)建時(shí)使用特定的值構(gòu)造對象,或者說將對象初始化為一個(gè)特定的狀態(tài)。 在對象創(chuàng)建時(shí)由系統(tǒng)自動(dòng)調(diào)用。 如果程序中未聲明,則系統(tǒng)自動(dòng)產(chǎn)生出一個(gè)缺省形式的構(gòu)造函數(shù) 允許為內(nèi)聯(lián)函數(shù)、重載函數(shù)、帶缺省形參值的函數(shù),3.3 構(gòu)造函數(shù)與析構(gòu)函數(shù),3.3.1 構(gòu)造函數(shù)-由于類的封裝性,不能象普通變量一樣初始化struct Savingsunsigned accountNumber; float balance;Savings A=1, 2000.0;Savings B(2,3000.0);
12、,構(gòu)造函數(shù)舉例:P50 例3.6 略,class Clock public: Clock (int NewH, int NewM, int NewS);/構(gòu)造函數(shù) void SetTime(int NewH, int NewM, int NewS); void ShowTime( ); private: int Hour,Minute,Second; ;,構(gòu)造函數(shù)的實(shí)現(xiàn): Clock:Clock(int NewH, int NewM, int NewS) Hour=H; Minute=M; Second=S; 建立對象時(shí)構(gòu)造函數(shù)的作用: void main( ) Clock c (0,0,0)
13、; /隱含調(diào)用構(gòu)造函數(shù),將初始值作為實(shí)參。 c.ShowTime( ); ,/ ex10_2.h #include class Demo intx,y; public: Demo(int a, int b) x=a; y=b; cout“Constructor demo(int,int) be calledn”; Demo( ) cunt“Constructor demo() be alledn”; void show() cout“X=“xt“Y=“yendl; ;,重載構(gòu)造函數(shù):P51 例3.7 略,#include “ex10_2.h” void main() Demo d1(3,5);
14、 /A d1.Show(); Demo d2; /B d2.Show(); 該程序的輸出為: Constructor Demo(int,int) be called X=3 Y=5 Constructor Demo( ) be called X=946 Y=928,帶默認(rèn)參數(shù)的構(gòu)造函數(shù):P52例3.8 略,note: 必須保證函數(shù)形式參數(shù)默認(rèn)后,函數(shù)形式不能與其他構(gòu)造函數(shù)完全相同,否則系統(tǒng)調(diào)用時(shí)會(huì)出現(xiàn)二義性。 如:,#include class Point int xVal,yVal; public: Point(int x=0,int y=0) xVal=x; yVal=y; Point()
15、 xVal=2; yVal=3; void print(); ;,void Point:print() coutprint(); /或者:下面這樣寫,就會(huì)error Point *p2=new Point(); p2-print(); ,正確:警告錯(cuò)誤都不會(huì)有。 #include class Point int xVal,yVal; public: Point(int x=0) xVal=x; yVal=0; Point(int x,int y) xVal=x; yVal=y; void print(); ;,void Point:print() coutprint(); ,默認(rèn)構(gòu)造函數(shù):P5
16、3例3.9 略 對象數(shù)組: P54例3.10 略 P55例3.11 略,4.4.2 拷貝構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)是一種特殊的構(gòu)造函數(shù),其形參為本類的對象引用。 作用:使用一個(gè)對象(參數(shù)指定的對象),去初始化一個(gè)正在被建立的同類型對象 class 類名 public : 類名(形參);/構(gòu)造函數(shù) 類名(類名 Y=yy; Point(Point,Point:Point (Point ,例4-2 拷貝構(gòu)造函數(shù)舉例,(1)當(dāng)用類的一個(gè)對象去初始化該類的另一個(gè)對象時(shí)系統(tǒng)自動(dòng)調(diào)用它實(shí)現(xiàn)拷貝賦值。 void main(void) Point A(1,2); Point B(A); /拷貝構(gòu)造函數(shù)被調(diào)用 cou
17、tB.GetX( )endl; ,拷貝構(gòu)造函數(shù)舉例(例4-2),(2)若函數(shù)的形參為類對象,調(diào)用函數(shù)時(shí),實(shí)參賦值給形參,系統(tǒng)自動(dòng)調(diào)用拷貝構(gòu)造函數(shù)。例如: void fun1(Point p) coutp.GetX( )endl; void main( ) Point A(1,2); fun1(A); /調(diào)用拷貝構(gòu)造函數(shù) ,拷貝構(gòu)造函數(shù)舉例(例4-2),(3)當(dāng)函數(shù)的返回值是類對象時(shí),系統(tǒng)自動(dòng)調(diào)用拷貝構(gòu)造函數(shù)。例如: Point fun2( ) Point A(1,2); return A; /調(diào)用拷貝構(gòu)造函數(shù) void main( ) Point B; B=fun2( ); ,拷貝構(gòu)造函數(shù),如
18、果程序員沒有為類聲明拷貝初始化構(gòu)造函數(shù),則編譯器自己生成一個(gè)拷貝構(gòu)造函數(shù)。 這個(gè)構(gòu)造函數(shù)執(zhí)行的功能是:用作為初始值的對象的每個(gè)數(shù)據(jù)成員的值,初始化將要建立的對象的對應(yīng)數(shù)據(jù)成員。,4.4.3 析構(gòu)函數(shù),完成對象被刪除前的一些清理工作。 在對象的生存期結(jié)束的時(shí)刻系統(tǒng)自動(dòng)調(diào)用它,然后再釋放此對象所屬的空間。 如果程序中未聲明析構(gòu)函數(shù),編譯器將自動(dòng)產(chǎn)生一個(gè)缺省的析構(gòu)函數(shù)。,構(gòu)造函數(shù)和析構(gòu)函數(shù)舉例,#include class Point public: Point(int xx,int yy); Point( ); /.其它函數(shù)原形 private: int X,int Y; ;,Point:Poin
19、t(int xx,int yy) X=xx; Y=yy; Point:Point( ) /.其它函數(shù)的實(shí)現(xiàn)略,/例 #include class Q int x,y; public: Q(int a=0,int b=0) x=a;y=b void P(void) coutxtyn; Q( ) cout “調(diào)用了析構(gòu)函數(shù)!”n;,void main(void) Q q(50,100); q.P( ); cout “退出主函數(shù)!n”; 輸出: 50 100 退出主函數(shù)! 調(diào)用了析構(gòu)函數(shù)!,例4-3 類的應(yīng)用舉例,一圓型游泳池如圖所示,現(xiàn)在需在其周圍建一圓型過道,并在其四周圍上柵欄。柵欄價(jià)格為35元
20、/米,過道造價(jià)為20元/平方米。過道寬度為3米,游泳池半徑由鍵盤輸入。要求編程計(jì)算并輸出過道和柵欄的造價(jià)。,#include const float PI = 3.14159; const float FencePrice = 35; const float ConcretePrice = 20; /聲明類Circle 及其數(shù)據(jù)和方法 class Circle private: float radius; public: Circle(float r); /構(gòu)造函數(shù) float Circumference( ); /圓周長 float Area( ); /圓面積 ;,/ 類的實(shí)現(xiàn) / 構(gòu)造函數(shù)
21、初始化數(shù)據(jù)成員radius Circle:Circle(float r) radius=r / 計(jì)算圓的周長 float Circle:Circumference( ) return 2 * PI * radius; / 計(jì)算圓的面積 float Circle:Area( ) return PI * radius * radius; ,void main ( ) float radius; float FenceCost, ConcreteCost; / 提示用戶輸入半徑 coutradius; / 聲明 Circle 對象 Circle Pool(radius); Circle PoolRi
22、m(radius + 3);,/ 計(jì)算柵欄造價(jià)并輸出 FenceCost = PoolRim.Circumference( ) * FencePrice; cout Fencing Cost is ¥ FenceCost endl; / 計(jì)算過道造價(jià)并輸出 ConcreteCost = (PoolRim.Area( ) - Pool.Area( ))*ConcretePrice; cout Concrete Cost is ¥ ConcreteCost endl; 運(yùn)行結(jié)果 Enter the radius of the pool: 10 Fencing Cost is ¥2858.85 Co
23、ncrete Cost is ¥4335.39,#include #include class complex private: double real; double imag; public: void init (double r,double i) real=r; imag=i; double realcomplex() return real; double imagcomplex()return imag; double abscomplex() double t; t=real*real+imag*imag; return sqrt(t); ;,例:復(fù)數(shù) (類與對象、內(nèi)聯(lián)成員函數(shù)
24、),void main() complex a ; a.init(1.1,2.2); cout“real of complex a=“a.realcomplex()endl; cout“image of complex a=“a.imagelcomplex()endl; cout“abs of complex a=“a.abscomplex()endl; ,#include #include class complex private: double real; double imag; public: complex (double r,double i) real=r; imag=i; d
25、ouble realcomplex() return real; double imagcomplex()return imag; double abscomplex() double t; t=real*real+imag*imag; return sqrt(t); ;,例:復(fù)數(shù) (構(gòu)造函數(shù)),void main() complex a (1.1,2.2) ; cout“real of complex a=“a.realcomplex()endl; cout“image of complex a=“a.imagelcomplex()endl; cout“abs of complex a=“a
26、.abscomplex()endl; ,#include #include class complex private: double real; double imag; public: complex (double r=0.0,double i=0.0) real=r; imag=i; double realcomplex() return real; double imagcomplex() return imag; double abscomplex() double t; t=real*real+imag*imag; return sqrt(t); ;,例:復(fù)數(shù) (帶缺省參數(shù)構(gòu)造函
27、數(shù)),void main() complex a (1.1,2.2) ; cout“real of complex a=“a.realcomplex()endl; cout“image of complex a=“a.imagelcomplex()endl; cout“abs of complex a=“a.abscomplex()endl; complex b; cout“real of complex b=“b.realcomplex()endl; cout“image of complex b=“b.imagelcomplex()endl; cout“abs of complex b=“
28、b.abscomplex()endl; ,#include #include class complex private:double real; double imag; public: complex (double r,double i) real=r; imag=i; complex () real=0.0; imag=0.0; double realcomplex() return real; double imagcomplex() return imag; double abscomplex() double t; t=real*real+imag*imag; return sq
29、rt(t); ;,例:復(fù)數(shù) (構(gòu)造函數(shù)重載),void main() complex a (1.1,2.2) ; cout“real of complex a=“a.realcomplex()endl; cout“image of complex a=“a.imagelcomplex()endl; cout“abs of complex a=“a.abscomplex()endl; complex b; cout“real of complex b=“b.realcomplex()endl; cout“image of complex b=“b.imagelcomplex()endl; cou
30、t“abs of complex b=“b.abscomplex()endl; ,#include #include class complex private:double real; double imag; public: complex (double r=0.0,double i=0.0) real=r; imag=i; complex()cout“destructing.n”; double realcomplex() return real; double imagcomplex() return imag; double abscomplex() double t; t=rea
31、l*real+imag*imag; return sqrt(t); ;,例:復(fù)數(shù) (析構(gòu)函數(shù)),void main() complex a (1.1,2.2) ; cout“real of complex a=“a.realcomplex()endl; cout“image of complex a=“a.imagelcomplex()endl; cout“abs of complex a=“a.abscomplex()endl; complex b; cout“real of complex b=“b.realcomplex()endl; cout“image of complex b=“b
32、.imagelcomplex()endl; cout“abs of complex b=“b.abscomplex()endl; ,#include #include class complex private:double real; double imag; public: complex (double r=0.0,double i=0.0) real=r; imag=i; complex (complex ,例:復(fù)數(shù) (拷貝構(gòu)造函數(shù) -應(yīng)用A),void main() complex a (1.1,2.2) ; cout“real of complex a=“a.realcomplex
33、()endl; cout“image of complex a=“a.imagelcomplex()endl; cout“abs of complex a=“a.abscomplex()endl; complex b(a); cout“real of complex b=“b.realcomplex()endl; cout“image of complex b=“b.imagelcomplex()endl; cout“abs of complex b=“b.abscomplex()endl; ,#include #include class complex private:double rea
34、l; double imag; public: complex (double r=0.0,double i=0.0) real=r; imag=i; complex (complex ,例:復(fù)數(shù) (拷貝構(gòu)造函數(shù) -應(yīng)用),void display(complex p) cout“real of complex p=“p.realcomplex()endl; cout“image of complex p=“p.imagelcomplex()endl; cout“abs of complex p=“p.abscomplex()endl; void main() complex a (1.1,2
35、.2) ; display(a); ,#include #include class complex private:double real; double imag; public: complex (double r=0.0,double i=0.0) real=r; imag=i; complex (complex ,例:復(fù)數(shù) (拷貝構(gòu)造函數(shù) -應(yīng)用C),complex fun() complex a (1.1,2.2) ; return a; void main() complex p; p=fun(); cout“real of complex p=“p.realcomplex()e
36、ndl; cout“image of complex p=“p.imagelcomplex()endl; cout“abs of complex p=“p.abscomplex()endl; ,組合的概念,類中的成員數(shù)據(jù)是另一個(gè)類的對象。 可以在已有的抽象的基礎(chǔ)上實(shí)現(xiàn)更復(fù)雜的抽象。, 類的組合,通過對復(fù)雜對象進(jìn)行分解、抽象,使我們能夠?qū)⒁粋€(gè)復(fù)雜對象理解為簡單對象的組合。 分解得到復(fù)雜對象的部件對象,這些部件對象比它高層的復(fù)雜對象更容易理解和實(shí)現(xiàn)。然后由這些部件對象來“裝配”復(fù)雜對象。,舉例,class Point private: float x,y; /點(diǎn)的坐標(biāo) public: Point(
37、float h,float v); /構(gòu)造函數(shù) float GetX(void); /取X坐標(biāo) float GetY(void); /取Y坐標(biāo) ; /.函數(shù)的實(shí)現(xiàn)略,Class Distance private: point p1,p2; /線段的兩個(gè)端點(diǎn) double dist; double price; public: Distance(Point a,Point b); /構(gòu)造函數(shù) double GetDis(void)return dist; ; /.函數(shù)的實(shí)現(xiàn)略,類組合的構(gòu)造函數(shù)設(shè)計(jì),原則:不僅要負(fù)責(zé)對本類中的基本類型成員數(shù)據(jù)賦初值,也要對對象成員初始化。 聲明形式: 類名:類名(
38、對象成員所需的形參,本類成員形參) :對象1(參數(shù)),對象2(參數(shù)),. 本類初始化 Point(int xx=0,int yy=0)x=xx;y=yy; Distance: Distance(Point a,Point b,double p):p1(a),p2(b) double x=double(p1.GetX()-p2.GetX(); double y=double(p1.GetY()-p2.GetY(); dist=sqrt(x*x+y*y); price=p; ,類組合的構(gòu)造函數(shù)調(diào)用,構(gòu)造函數(shù)調(diào)用順序:先調(diào)用內(nèi)嵌對象的構(gòu)造函數(shù)(按內(nèi)嵌時(shí)的聲明順序,先聲明者先構(gòu)造)。然后調(diào)用本類的構(gòu)造
39、函數(shù)。(析構(gòu)函數(shù)的調(diào)用順序相反) 若調(diào)用缺省構(gòu)造函數(shù)(即無形參的),則內(nèi)嵌對象的初始化也將調(diào)用相應(yīng)的缺省構(gòu)造函數(shù)。,#include #include class Point private: float x,y; /點(diǎn)的坐標(biāo) public: Point(float xx,float yy) coutpont構(gòu)造函數(shù)endl; x=xx; y=yy; Point(Point ,class Distance private: Point p1,p2; /線段的兩個(gè)端點(diǎn) double dist; public: Distance(Point a,Point b); /構(gòu)造函數(shù) double Get
40、Dis(void)return dist; ; Distance: Distance(Point a,Point b):p1(a),p2(b) cout Distance構(gòu)造函數(shù)endl; double x=double(p1.GetX()-p2.GetX(); double y=double(p1.GetY()-p2.GetY(); dist=sqrt(x*x+y*y); void main() Point myp1(1,1),myp2(4,5); Distance myd(myp1,myp2); coutthe distance is :; coutmyd.GetDis()endl; ,P
41、oint構(gòu)造函數(shù) Point構(gòu)造函數(shù) Point拷貝構(gòu)造函數(shù) Point拷貝構(gòu)造函數(shù) Point拷貝構(gòu)造函數(shù) Point拷貝構(gòu)造函數(shù) Distance構(gòu)造函數(shù) the distance is 5,class Distance private: Point p1,p2; /線段的兩個(gè)端點(diǎn) double dist; double price; public: Distance(Point a,Point b,double p); /構(gòu)造函數(shù) double GetDis(void)return dist; ; Distance: Distance(Point a,Point b ,double p=
42、30):p1(a),p2(b) cout Distance構(gòu)造函數(shù)endl; double x=double(p1.GetX()-p2.GetX(); double y=double(p1.GetY()-p2.GetY(); dist=sqrt(x*x+y*y); price=p; void main() Point myp1(1,1), myp2(4,5); Distance myd(myp1,myp2); coutthe distance is :; coutmyd.GetDis()endl; ,Point構(gòu)造函數(shù) Point構(gòu)造函數(shù) Point拷貝構(gòu)造函數(shù) Point拷貝構(gòu)造函數(shù) Poi
43、nt拷貝構(gòu)造函數(shù) Point拷貝構(gòu)造函數(shù) Distance構(gòu)造函數(shù) the distance is 5,類的組合舉例(二),class Part /部件類 public: Part( ); Part(int i); Part( ); void Print( ); private: int val; ;,class Whole public: Whole( ); Whole(int i,int j,int k); Whole( ); void Print( ); private: Part one; Part two; int hdate; ;,Whole:Whole( ) hdate=0; W
44、hole:Whole(int i,int j,int k): two(i),one(j),hdate(k) /.其它函數(shù)的實(shí)現(xiàn)略,4.5.2 前向引用聲明,類應(yīng)該先聲明,后使用 如果需要在某個(gè)類的聲明之前,引用該類,則應(yīng)進(jìn)行前向引用聲明。 前向引用聲明只為程序引入一個(gè)標(biāo)識(shí)符,但具體聲明在其它地方。,前向引用聲明舉例,class B; /前向引用聲明 class A public: void f(B b); ; class B public: void g(A a); ;,類模板,template 類聲明 使用類模板使用戶可以為類聲明一種模式,使得類中的某些數(shù)據(jù)成員、某些成員函數(shù)的參數(shù)、某些成員函數(shù)的返回值,能取任意類型(包括系統(tǒng)預(yù)定義的和用戶自定義的)。,定義一個(gè)類模板與定義函數(shù)模板的格式類似,必須以關(guān)鍵字template開始,后面是尖括號(hào)括起來的模板參數(shù),然后是類名,其格式如下: template class類名 ; 其中template是一個(gè)聲明模板的關(guān)鍵字,它表示聲明一個(gè)模板。關(guān)鍵字class表明后面的Type是模板參數(shù)。,在類定義中,欲采用通用數(shù)據(jù)類型的 數(shù)據(jù)成員、成員函數(shù)的參數(shù)或返回值, 前面需加上Type。 例如,下面的程序中建立了一個(gè)復(fù)數(shù)類模板。 #include #include template class complex
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 小學(xué)二年級(jí)體育教學(xué)工作總結(jié)
- 2025數(shù)字化技術(shù)基礎(chǔ)繼續(xù)教育公需課試題及答案
- 三病母嬰傳播培訓(xùn)試題(附答案)
- 2025年基本公共衛(wèi)生服務(wù)居民健康檔案管理培訓(xùn)班試題(附答案)
- 建筑工程中級(jí)職稱評定個(gè)人工作總結(jié)
- 銀行客戶經(jīng)理2026年度工作總結(jié)
- 2025年企業(yè)社會(huì)責(zé)任培訓(xùn)考核要點(diǎn)試卷及答案
- 傳染病防控工作實(shí)施方案
- 醫(yī)務(wù)科2025年工作計(jì)劃
- 建設(shè)工程施工合同糾紛要素式起訴狀模板要素精準(zhǔn)無偏差
- 臨床成人失禁相關(guān)性皮炎的預(yù)防與護(hù)理團(tuán)體標(biāo)準(zhǔn)解讀
- 創(chuàng)新創(chuàng)業(yè)教育學(xué)習(xí)通超星期末考試答案章節(jié)答案2024年
- 《最奇妙的蛋》完整版
- 三年級(jí)科學(xué)上冊蘇教版教學(xué)工作總結(jié)共3篇(蘇教版三年級(jí)科學(xué)上冊知識(shí)點(diǎn)整理)
- 種子室內(nèi)檢驗(yàn)技術(shù)-種子純度鑒定(種子質(zhì)量檢測技術(shù)課件)
- SEMI S1-1107原版完整文檔
- 心電監(jiān)測技術(shù)操作考核評分標(biāo)準(zhǔn)
- 2023年中級(jí)財(cái)務(wù)會(huì)計(jì)各章作業(yè)練習(xí)題
- 金屬罐三片罐成型方法與罐型
- 大疆植保無人機(jī)考試試題及答案
- 《LED顯示屏基礎(chǔ)知識(shí)培訓(xùn)》
評論
0/150
提交評論