關于c-課件第3章_第1頁
關于c-課件第3章_第2頁
關于c-課件第3章_第3頁
關于c-課件第3章_第4頁
關于c-課件第3章_第5頁
免費預覽已結(jié)束,剩余104頁可下載查看

下載本文檔

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

文檔簡介

第第3章類與對thisstaticconst成員和const函數(shù) 大連理

第3章類與對大連理

第3章類與對類是具有相同或相似性質(zhì)的對象的抽象。抽象是從眾多的事物中抽取出共同的、本質(zhì)性的特征,而舍棄其非本質(zhì)的特征的過程。一是把對象的全部屬個不可分割的獨立單位 大連理

第3章類與對類的定class類{ 保護的成員數(shù)據(jù)和成員函 大連理

第3章類與對類的定class類的定{privateinthour,minute,second; public:voidsetClock(inth,intm,ints); voidshowClock(); //顯示時間值;大連理

第3章類與對類的定class{privateClock int public:void 時間大連理

第3章類與對類的定 說明類成員權(quán)限的關鍵字private、protected和voidsetClock(inth,intmints);//用來設置時、分、秒voidsetClock(inth,intm);//用來設置時、分voidsetClockint //大連理

第3章類與對類的實類的實{privateinthourminute //關于時間的數(shù)據(jù)類public 實voidsetClock(inth,intm,inthour=(h>=0&&h<=23)?h:0;minute=(m>=0&&m<=59)?m:0;second=(s>=0&&s<=59)?s:0; voidshowClock( 大連理class{private

第3章類與對類的實inthourminute //publicvoidsetClock(inth,intm,intvoidshowClock(voidClock::setClock(inth,intm,ints){hour=(h>=0&&h<=23)?h:0;minute=(m>=0&&m<=59)?m:0;second=(s>=0&&s<=59)?s:0; voidClock::showClock(){

類實運算大連理

第3章類與對類的實inlinevoidClock::showClock(){}

雜結(jié)構(gòu),為內(nèi)聯(lián)大連理

第3章類與對對象的定class{privateinthourminute publicvoidsetClock(inth,intm,intvoidshowClock(}大連理

第3章類與對對象的定{private

此處無類inthourminute publicvoidsetClock(inth,intm,intvoidshowClock(}大連理

第3章類與對對象的定class{privateinthourminute publicvoidsetClock(inth,intm,intvoidshowClock(Clock

另外這種方式最常大連理

第3章類與對對象的定Clockclock1,clock2;

類的成員函數(shù)空 對象空

36361245Clock*p; Clock1245

大連理3.33.3對象定義對象第3章類與對對是指對對象成員的使用,可以通過成員運算符“.”和向成員運算符“1,針對對象、指針2:3:::對象名.數(shù)據(jù)成對象名.成員函數(shù)(實參表指向?qū)ο蟮闹羔?>數(shù)據(jù)成指向?qū)ο蟮闹羔?>成員函(實參表(*指向?qū)ο蟮闹羔様?shù)據(jù)成員(*指向?qū)ο蟮闹羔?.成員函(實參表//此處不正確,hour私Clock*p; (*p).setClock(6,15,24);Clock&clock3=clock1;clock3.etClock(6,15,24);大連理

第3章類與對例3.1class{return

各自都有自己的內(nèi)存空間。voidsetX(doub賦值。此處調(diào) 構(gòu)造函數(shù)后面voidsetY(doublej){y=j;}doubledouble

distance參數(shù)必 嗎 大連理

第3章類與對例3.2兩點距離(代碼分離頭文件point.hclassPoint{doubledistance(Point&p);voidsetX(doublei);voidsetY(doubledoublex;double

usingnamespacestd;doublePoint::distance(Point&returnsqrt((p.x-x)*(p.x-x)+(p.y- voidPoint::setX(doublei){x=i;}voidPoint::setY(double大連理

第3章類與對例3.2兩點距離(代碼分離usingnamespacestd;voidmain(){Pointp1,p2;

}大連理

第3章類與對構(gòu)造函

成員函數(shù)(不是必須的大連理

第3章類與對構(gòu)造函{函數(shù)}成員名(實參成員名(實Clock(inth,intm,ints):hour(h),minute(m){second=s}

大連理

第3章類與對構(gòu)造函構(gòu)造函類名對象名(實參列表或類名*對象名=new類名(表Clockc1(1,2,3);Clockc2;

大連理

第3章類與對構(gòu)造函數(shù):默認構(gòu)造函默認構(gòu)造函數(shù)(defaultconstructor)就是在沒有顯式提供初始形式為:類名∷構(gòu)造函數(shù)名(){ Clock Clock 大連理

第3章類與對構(gòu)造函數(shù):默認構(gòu)造函默認構(gòu)造函數(shù)(defaultconstructor)就是在沒有顯式提供初始Clock(){hour=0;minute=0;second=0;}Clock Clock():hour(0),minute(0),second(0){

大連理

第3章類與對構(gòu)造函數(shù):默認構(gòu)造函默認構(gòu)造函數(shù)(defaultconstructor)就是在沒有顯式提供初始大連理

第3章類與對構(gòu)造函數(shù):有參構(gòu)造函class{privateinthour,minute,publicClock(inth,intm,ints){hour=h;minute=m;second=s;voidClock::setClock(inth,intm,ints);voidClock::showClock();Clock大連理

第3章類與對構(gòu)造函數(shù):有參構(gòu)造函class{privateinthour,minute,public

Clock(inth,intm,ints){hour=h;minute=m;second=s;voidClock::setClock(inth,itm,ints);voidClock::showClock();ClockClock大連理

第3章類與對構(gòu)造函數(shù):有參構(gòu)造函Clock(inth,intm,ints):hour(h),minute(m),second(s){Clock():hour(0),minute(0),second(0){//需要3//大連理

第3章類與對構(gòu)造函數(shù):有參構(gòu)造函Clock(inth=0intm=0ints=0hour=hminutemsecond=s;Clockclock0; //clock0的數(shù)據(jù)成員的值為0、0、0Clock //clock1的數(shù)據(jù)成員的值為1、0、Clock //clock2的數(shù)據(jù)成員的值為1、2、Clockclock3(1,2,3clock3的數(shù)據(jù)成員的值為1、2、大連理

第3章類與對析構(gòu)函 class{~類名{指定的操作 大連理3.4

第3章類與對析構(gòu)函 類名::~類名( 大連理3.4

第3章類與對例3.4構(gòu)造函數(shù)與析構(gòu)函class{doublex,y;charPoint(char*n=NULL,doublex=0.0,doubley=0.0);voiddisp(

Point::Point(char*n,doubledoublex=a;y=if(n)name=new}name=newchar[8];strcpy(name,"noname");}cout<<name<<"}大連理3.4delete[] }

第3章類與對例3.4構(gòu)造函數(shù)與析構(gòu)函void定義對PointPoint

homeconstructingschoolconstructingnonameconstructingp3=nononamedestructingschooldestructinghomedestructing對象析構(gòu)的順序恰好對象的構(gòu)造順序相反

Point//輸出對象}

大連理課堂練習-

第3章類與對classStudent{char*name;charsex;doublescore;voidprint();//輸 Student();//Student(char*n,chars,doublesc);//有參voidsetName(char*n);//設置大連理3.5

第3章類與對int開辟一個整型變量空間同時置其值為100數(shù),把已經(jīng)存在對象的數(shù)據(jù)按 到新生成對象的空間大連理Point3.5Point

第3章類與對類的成員函數(shù)空 對象空

Point(charPoint(char

工工nonanonamehohomeschool 大學軟件學3.5

第3章類與對Pointp1(“home”,1.0,2.0),p2(“school”,3.0),p3;Pointp4=p1;

怎樣析

Point(charPoint(char

堆homhomeschool

nmnme

第3章類與對Pointp1(“home”,1.0,2.0),p2(“school”,3.0),p3;Pointp4=p1;

p4析

Point(charPoint(char

堆school

nononame

第3章類與對Pointp1(“home”,1.0,2.0),p2(“school”,3.0),p3;Pointp4=p1;

p3析

Point(charPoint(char

堆school

大連理

第3章類與對Pointp1(“home”,1.0,2.0),p2(“school”,3.0),p3;Pointp4=p1;

p2析堆Point(charPoint(char大連理

第3章類與對Pointp1(“home”,1.0,2.0),p2(“school”,3.0),p3;Pointp4=p1;

delete[name?堆Point(charPoint(char大連理

第3章類與對Pointp1(“home”,1.0,2.0),p2(“school”,3.0),p3;Pointp4=p1;class類名{類名類名&形參{拷貝構(gòu)造函數(shù)的函數(shù)

類名類名&形參){函數(shù)名與類名必須,否則遞歸,后面介大連理

第3章類與對

而對象p2生

時參照p1生的,也稱作拷貝構(gòu)造函數(shù)利用一個已經(jīng)存在的對象再生成p1初始化p2象Pointp1(“home”,1.0,2.0);//對象p1生成時沒參照現(xiàn)有對象Pointp2=p1; //也可以寫作Pointp2(p1);PointP3大連理

第3章類與對淺拷Pointp1(“home”,1.0,2.0),p2(“school”,3.0),p3;Pointp4=p1;

Point(charPoint(char

堆home

nname同meschoeschool

第3章類與對深拷Point深拷 2類的成員函Point(char*,doublePoint(Point

Point::Point(Point&p){//拷貝構(gòu)造函x=p.x;y=if(){//如果形參不是空值name=newchar[strlen()+1];name=newcout<<name<<"copy}a 大連

學學

第3章類與對標記拷深拷貝,需要把資源一份,而有時候資源很大,需要大量的時間和空間,甚至有些資源是不可的,這時深拷貝象不再申請 源,去共享原有對象申請的資源析構(gòu)時拷貝構(gòu)造函數(shù)生成的對象不可以釋放 源,原生成的對象析構(gòu)時才可以釋放 源大連理

第3章類與對例3.6class{doublex,y;charintflag區(qū)分構(gòu)造對象和拷貝構(gòu)造對象Point(char*n=NULL,doublex=0.0,doubley=0.0);Point(Point&p);voiddisp();大連理

第3章類與對例3.6Point::Point(char*n,doublea,doubleb){x=a;y=b; if(n){ name=newchar[strlen(n)+1];}}

name=newstrcpy(name,"nocout<<name<<"}大連理3.5

第3章類與對例3.6Point::Point(Pointx=p.x;y=name= cout<<name<<"copyconstructing"<<endl;}cout<<name<<"delete[]}大連理標記拷標記拷

第3章類與對PointPointp1(“home”,1.0,2.0),p2(“school”,3.0),p3;Pointp4=p1;

對象空

0

11

111 空類的成員函數(shù)空Point(charPoint(char*,double,double)Point(Point&)

homhomeschoolnonamenoname3.5

第3章類與對函數(shù)對象參voidtest(Pointp){p.disp();}int

test函

對象空參數(shù)傳

yPointreturn}

類的成員函數(shù)空Point(charPoint(char

堆空

P和p1

homehomePoint::Point(Point&p){//拷貝構(gòu)造函x=p.x;y=if(){//如果形參不是空值name=newchar[strlen()+1];name=newchar[8];strcpy(name,"noname");}cout<<name<<"

int

Pointreturn}

Point(charPoint(charPoint(Point&)

homehhomehome

第3章類與對函數(shù)返回對比如函數(shù)內(nèi)定義對象:Pointp;當函數(shù)返回時:return p大連理Point{

第3章類與對函數(shù)返回對對象空ptest函ptest函臨時對main函xxxreturn

拷貝構(gòu) 按 }int{Pointp1;return0;

類的成員函數(shù)空Point(charPoint(char

emoemoh

}

大連理第3章類與對Point

函數(shù)返回對

最終解決還要用深拷對象空{(diào)Pointreturn

test函 拷貝構(gòu)y

臨時對

}int{Pointreturn

類的成員函數(shù)空Point(charPoint(char

emoemoh}

大連理第第3與對3.5拷貝構(gòu)造函對初始構(gòu)造函函數(shù)數(shù)對函數(shù)回對大連理

第3章類與對數(shù)組元素不僅可以由簡單類型組成(成(對象數(shù)組的每一個元素都是同類型的對象)classcharintage;floatscore;Student(ints){strcpy(name,"noname");

構(gòu)造函數(shù)只有一個參數(shù)組可以這樣初始化大連理

第3章類與對數(shù)組元素不僅可以由簡單類型組成(成(對象數(shù)組的每一個元素都是同類型的對象)classcharintage;floatscore;Student(char*n,inta,intscore=s;}

stud[3]={Student("zhangsan",20,60),Student("Liwu",18,78)大連理

第3章類與對例3.8classcharintage;floatscore;Student(char*n,inta,int floatgetScore(){returnscore;

intstud[3]={san",20,60),Student(“Wangwu”,18,78)};floatsum=0,average;int大連理return this

第3章類與對大連理類的成員函數(shù)空 對象空

3 636inthour,minute,second;public:voidsetClock(inth,intm,intvoidClock::setClock(inth,intm,ints){ second=s;}

4512voidClock::setClock(Clock*this,inth,intm,ints){ 大連理this

第3章類與對voidClock::setClock(inthour,intminute,intsecond)this->hour=hour;this->minute=minute;this->second=}return*this”,在第4大連理this關鍵字—

第3章類與對對Clock類定義函數(shù)Clockadd(),增加一秒;ClockClock課堂 代碼大連理static

第3章類與對static數(shù)據(jù)成 大連理static

第3章類與對classX{charchstaticintintX::s=0voidf({Xa,b,c,d;static數(shù)據(jù)成astatic數(shù)據(jù)成ac靜態(tài)空bs??誨

大連理static

第3章類與對static數(shù)據(jù)成<數(shù)據(jù)類型><類名>::<靜態(tài)數(shù)據(jù)成員名>t::s=0;初始化在類 行,而前面不加初始化時不加該成員 權(quán)限控制符public、protected大連理static

第3章類與對static數(shù)據(jù)成靜態(tài)數(shù)據(jù)成 形式<類對象名>.或<類類型名>::<靜態(tài)數(shù)據(jù)成員名> 大連理static

第3章類與對例3.9classcharintage;floatscore;Student(char*n,inta,ints){ score=s;sum+=score;}staticfloat float //

intstud[3]={Student(“zhangsan”,20,60),Student(“Lisi”,19,70),Student(“Wangwu”,18,78)};floataverage; return0;}大連理static

第3章類與對static數(shù)據(jù)成 大連理static

第3章類與對static函數(shù)成成員函數(shù)前面加static 大連理static

第3章類與對例3.10classchar int floatStudent(char*n,inta,ints){ staticfloatsum; staticfloatgetAverage(); voiddisplay(); floatStudent::getAverage(){returnsum/3實現(xiàn)時不需要float void //成員函 average="<<getverage(l;}static

第3章類與對例3.10intStudentstud[3]={Student(“zhangsan”,20,60),Student(“Lisi”,19,70),Student(“Wangwu”,18,78)};//靜態(tài)成員函數(shù)、靜態(tài)成員數(shù)據(jù)定義對象后,通過//靜態(tài)成員函數(shù)、靜態(tài)成員數(shù)據(jù)定義對象后,通過對cout<<"sum="<<stud[0].sum<<"average="<<stud[0//成員函return}

;大連理static

第3章類與對static函數(shù)成 大連理const成員和const

第3章類與對const數(shù)據(jù)成數(shù)據(jù)的安全性可以通 控制權(quán)限管理const理類的g等都是常量,可以用關鍵字const來常數(shù)據(jù)成員。大連理const成員和constclass{private

第3章類與對const數(shù)據(jù)成constinthour; intminute,second; public:Clock(inth,intm,ints):hour(h),minute(m),second(s){Clock(inth,intm,ints):hour(h){minute=m;second=s;}√Clock(inth,intm,ints){hour=h;minute=m;second=s;}×Clock(intm,ints){minute=m;second=s;} 成員函數(shù)可 大連理const成員和const

第3章類與對const函數(shù)成 類型成員函數(shù)名(參數(shù)表)classprivateinthour,minute,publicvoidsetClock(inth,intm,inthour=h;minute=m;second=s; voidshowClock()const{ cout<<hour<<minute<<second;大連理const成員和const

第3章類與對const函數(shù)成voidClock::showClock()const{

const數(shù)據(jù)成員,也可 const的成員函 ,只要const數(shù)據(jù)成員不被修改即可大連理const成員和const

第3章類與對class

doublearea()const{ returnpi*getRadius()*getRadius();};doublex,y; doubleradius; constdoublepi;//π doublegetRadiusreturnradius;}doublearea()

doublearea(){returnpi*radius*radius;}不調(diào)用const成員函數(shù)則編譯出錯returnpi*radius*radius;大連理const成員和const

第3章類與對const對const對象名[(實參表列)];const類名對象名[(實參表列constClock constCirclec1(1.2,3.4,3,3.14); c1.getRadius();//調(diào)用常對象c1中的非const大連理const成員和constclass

第3章類與對指針和對象間的關doublex,yradius //圓心坐標、半constdoublepi;Circle(doublex,doubley,doubleradius,doublethis- this- this- double returnradius;voidsetRadius(doubleradius){this->radius=radius;}doublearea()const{ returnpi*radius*radius;}Circlec1(1.2,2.3,4, Circle const成員和constclass

第3章類與對指針和對象間的關doublex,yradius //圓心坐標、半constdoublepi;Circle(doublex,doubley,doubleradius,doublethis- this- this- double returnradius;voidsetRadius(doubleradius){this->radius=radius;}doublearea()const{ returnpi*radius*radius;}指向常對象的指針,指向的對象可以是const的,也可以是非 Circlec1(1.2,2.3,4,3.14); constCircle*p=&c1;對象名c1可以任何公有成員:c1.area()、c1setRadius(10)。指針p可以const公有成員:p->area√p->setRadius(10×const成員和constclass

第3章類與對指針和對象間的關doublex,yradius //圓心坐標、半constdoublepi;Circle(doublex,doubley,doubleradius,doublethis- this- this- double returnradius;voidsetRadius(doubleradius){this->radius=radius;}doublearea()const{ returnpi*radius*radius;}Circlec1(1.2,2.3,4, Circle*const 任何公有成員:p->setRadius(10)、p->area()const成員和constclass

第3章類與對指針和對象間的關doublex,yradius //圓心坐標、半constdoublepi;Circle(doublex,doubley,doubleradius,doublethis- this- this- double returnradius;voidsetRadius(doubleradius){this->radius=radius;}doublearea()const{ returnpi*radius*radius;}constCirclec1(1.2,2.3,4,3.14);constCircle*constp=&c1; const公有成員:c1.area()√,c1setRadius(10)× const公有成員:p->area()√,p->setRadius(10)×const成員和const

第3章類與對函數(shù)參數(shù)為對象指允許實參指向const對象的指針,或指向非constvoidf(constCircle//cout<<p->cout<<p-}Circlec1(1.2,2.3,4,const大連理const成員和const

第3章類與對函數(shù)參數(shù)為對 如果形參為對象 名,實參為對象名,形參即為實參對象voidf(Circle&cout<<r.cout<<}Circlec1(1.2,2.3,4,

voidf(constCircle&//cout<<r.setRadius(10);cout<<r.area();}大連理3.7const關鍵字—

第3章類與對1.對例3.10,不改變程序功能,哪些函數(shù)可以改課堂練習2代碼**const成員函數(shù)可以修改靜態(tài)數(shù)據(jù)成大連理函數(shù)

第3章類與對類的私有成員只能在類的定義范圍內(nèi)使用,也就是說,類私有成員只能通過本類的成員函數(shù)來 ,但有時候又需要在部 時 成員。為此引入 。大連理函數(shù)

第3章類與對函 中用關鍵字friend說明的非成員函數(shù) friend<數(shù)據(jù)類型 函數(shù)名>(參數(shù)表大連理class{

intPoint return0;}doublex, //x,yPoint(doublex=0.0,doubley=0.0);voiddisp();frienddoubledistance(Pointp1,Pointp2 為PointPoint::Point(doublexdoubley){this->xx;this->yyvoidPoint::disp(){cout<<"點("<<x<<","<<y<<")";}doubledistance(Pointp1,Pointp2){ 大連理

第3章類與對例3.13普通函數(shù)作為多個類 函class{doublex, //x,yPoint(doublex=0.0,doubley=0.0);voiddisp();frienddoubledistance(Pointp,Linel); 為PointPoint::Point(doublex,doubley){this->x=x;this->y=y;}voidPoint::dispcout<<"點("<<x<<","<<y<<")";大連理

第3章類與對例3.13普通函數(shù)作為多個類 函class{doublea,b,c; Line(doublea=0.0,doubleb=0.0,doublec=0.0);voiddisp();frienddoubledistance(Pointp,Linel); 為LineLine::Line(doublea,doubleb,doublec){this->a=a;this->b=b;this->c=c;}voidLine::disp(cout<<"線("<<a<<"+"<<b<<"+"<<c<<"=0)大連理

第3章類與對例3.13普通函數(shù)作為多個類 函doubledistance(Pointp,Linereturn}intPointLinereturn}大連理函數(shù)

第3章類與對成員函如果一個類的成員函數(shù)是另一個類的函數(shù),則稱這個成員函數(shù)為成員函數(shù);通過成員函數(shù),不僅可以自己所在類對象中的私有和公有成員,還可由關鍵字friend語句所在的類對象中的大連理

第3章類與對例3.14成員函數(shù)作為類 函class{doublex, //x,yPoint(doublex=0.0,doubley=0.0);voiddisp();doubledistance(Linel); 為PointPoint::Point(doublex,doubley){this->x=x;this->y=y;}voidPoint::dispcout<<"點("<<x<<","<<y<<")";大連理

第3章類與對例3.14成員函數(shù)作為類 函class{doublea,b,c; Line(doublea=0.0,doubleb=0.0,doublec=0.0);voiddisp();frienddoublePoint::distance(Linel); 為LineLine::Line(doublea,doubleb,doublethis->a=a;this->b=b;this->c= voidLine::disp(大連理

第3章類與對例3.14成員函數(shù)作為類 函doublePoint::distance(Linereturn}intPointLinereturn}大連理函數(shù)

第3章類與對類當一個類作為另一個類 時,稱這個類 類 類 可以放在 中的任何位置 firiendclass 類名大連理函數(shù)

第3章類與對例 class{doublex, //x,yPoint(doublex=0.0,doubley=0.0);voiddisp();friendclassComputeTools; 為Point Point::Point(doublex,doubley){this->x=x;this->y=y;}voidPoint::dispcout<<"點("<<x<<","<<y<<")";大連理函數(shù)

第3章類與對例 class{doublea,b,c; Line(doublea=0.0,doubleb=0.0,doublec=0.0);voiddisp(); //輸出私有變量的成員函數(shù)friendclassComputeTools;// Line::Line(doublea,doubleb,doublethis->a=a;this->b=b;this->c=c;voidL

溫馨提示

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

評論

0/150

提交評論