第6章類與對象的定義與訪問_第1頁
第6章類與對象的定義與訪問_第2頁
第6章類與對象的定義與訪問_第3頁
第6章類與對象的定義與訪問_第4頁
第6章類與對象的定義與訪問_第5頁
已閱讀5頁,還剩266頁未讀 繼續免費閱讀

下載本文檔

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

文檔簡介

第6章類與對象P216

?類(Class)是面向對象程序設計(OOP)

實現信息封裝的基礎。

-類是用戶定義類型,也稱為類類型。

?每個類包含數據說明和一組操作數據或傳

遞消息的函數。

?類的實例稱為對象。

6.1類和對象的定義與訪問P216

?在面向對象程序設計中,類是程序基本單位。

-類是數據和操作數據的函數的封裝。

?類的對象使用自己的方法完成對數據的操作。

-類可以隱藏數據和操作細節,對象通過類接口

與外部通信。

例1?2用面向對象方法編程,

求圓的周長和面積P4

圓類

數據成員

半徑

成員函數

置半徑值

求圓的半徑

求周長

求面積

〃例1-2輸入圓的半徑,求圓的周長和面積(用面向對象方法編程)P4

#include<iostream>

usingnamespacestd;

classCircle

{private:

doubleradius;

public:

voidSet_Radius(doubler){radius=r;}

doubleGet_Radius(){returnradius;}

doubleGet_Girth(){return2*3.14f*radius;}

doubleGet_Area(){return3.14f*radius*radius;}

};"

intmain()

{CircleA,B;

A.Set_Radius(6.23);

cout?"A.Radius=H?A.Get_Radius()?endl;

cout?"A.Girth="?A.Get_Girth()?endl;

cout?"A.Area="?A.Get_Area()?endl;

B.Set_Radius(10.5);

cout?"B.radius="?B.Get_Radius()?endl;

cout?"B.Girths'1?B.Get_Girth()?endl;

cout?"B.Area=11?B.Get_Area()?endl;

return(O);

}

例1一2用面向對象方法編程,求圓的周長和面積

#include<iostream>

數據成員

usingnamespacestd

classCircle

{doubleradius;

public:

voidSet_Radius(doubler){radius=r;}

doubleGet_Radius(){returnradius;}

doubleGet_Girth(){return2*3.14*radius;}

doubleGet_Area(){return3.14*radius*radius;}

};

intmain()

{CircleA,B;

A.Set_Radius(6.23);

cout?"A.Radius="vvA.Get_Radius()?endl;

cout?"A.Girth="<vA.Get_Girth()?endl;

cout?"A.Area="?A.Get_Area()?endl;

B.Set_Radius(10.5);

cout?"B.radius="?B.Get_Radius()?endl;

cout?"B.Girth="?B.Get_Girth()?endl;

cout?"B.Area="?B.Get_Area()?endl;

例1一2用面向對象方法編程,求圓的周長和面積

#include<iostream>

usingnamespacestd;

classCircle成員函數

{doubleradius;

public:

voidSet_Radius(doubler){radius-r;}

doubleGet_Radius(){returnradius;}

doubleGet_Girth(){return2*3.14*radius;}

doubleGet_Area(){return3.14*radius*radius;}

};

intmain()

{CircleA,B;

A.Set_Radius(6.23);

cout?"A.Radius="?A.Get_Radius()?endl;

cout?"A.Girth="<vA.Get_Girth()?endl;

cout?"A.Area="?A.Get_Area()?endl;

B.Set_Radius(10.5);

cout?nB.radius="?B.Get_Radius()?endl;

cout?”B.Girth="wB.Get_Girth()?endl;

cout?"B.Area="?B.Get_Area()?endl;

例1一2用面向對象方法編程,求圓的周長和面積

#include<iostream>

usingnamespacestd;

classCircle

{doubleradius;

public:

voidSet_Radius(doubler){radius=r;}

doubleGet_Radius(){returnradius;}

doubleGet_Girth(){retuip2*3.14*radius;,

doubleGet_Area()Ld^turi建立對象is;}

L;main()(類類型變量)

{CircleA,B;

A.Set_Radius(6.23);

cout?"A.Radius="?A.Get_Radius()?endl;

cout?"A.Girth="<vA.Get_Girth()?endl;

cout?"A.Area="?A.Get_Area()?endl;

B.Set_Radius(10.5);

cout?nB.radius="?B.Get_Radius()?endl;

cout?”B.Girth="wB.Get_Girth()?endl;

cout?"B.Area="?B.Get_Area()?endl;

例1一2用面向對象方法編程,求圓的周長和面積

#include<iostream>

usingnamespacestd;

classCircle

{doubleradius;

public:

voidSet_Radius(doubler){radius=r;}

doubleGet_Radius(){returnradius;}通過對象

doubleGet_Girth(){return2*3.14*qdi;}調用類的成員函數

doubleGet_Area(){return3.14*radyls*radius-77

J,

intmain()

{CircleA,B;

A.Set_Radius(6.23);

cout?"A.Radius="?A.GetRadius()?endl;

cout?"A.Girth="?A.Get_Girth()?endl;

cout?"A.Area="v<A.Get_Area()?endl;

B.Set_Radius(10.5);

cout?"B.radius="?B.Get_Radius()?endl;

cout?"B.Girth="?B.Get_Girth()?endl;

cout?"B.Area="?B.Get_Area()?endl;

數組類型定義與排序操作P217

〃排序函數原型

voidSort(int[],int);

〃數組相加函數原型

voidAdd(int[],int[],int);

//

intmain()

{inta[10],b[10];

II.......

Sort(a,10);

Sort(b,10);

Add(a,b,10);

II.......

)

“數組類”類型P217

?設計一個“數組類”類型,把數組類型定

義和對數組排序操作封裝在一起。

?這樣“排序”就成了所有數組類對象自己

做的操作。

封裝classArray〃定義數組類

{int*ap;intlen;

數組與數組類

public:

〃排序函數原型Array(intsize)〃建立數組

voidSort(int[],int);{len=size;ap=newint[size];}

〃數組相加函數原型voidSort();//排序

voidAdd(int[],int[],int);〃重載算符中函數

Arrayoperaor+(constArray&other);

intmain());

{inta[10],b[10];

intmain()

{Arraya(10),b(10);〃聲明對象

Sort(a,10);

Sort(b,10);a.Sort();b.Sort();〃調用排序方法

Add(a,b,10);a=a+b;〃數組相加

封裝

classArray〃定義數組類

finf*ap;intlen;

die:

trray(intsize)〃建立數組

len=size;ap=newint[size];}

oidSort();〃排序

/重載算符+函數

rrayoperaor+(constArray&other);

111L

{inta[10],b[10];

intmainQ

{Arraya(10),b(10);〃聲明對象

Sort(a,10);

Sort(b,10);a.Sort();b.Sort();〃調用排序方法

Add(a,b,10);a=a+b;〃數組相加

封裝

classArray〃定義數組類

數組與數組類int*ap;intlen;

public:

//排序函數原型Array(intsize)〃建立數組

voidSort(int[],int);{len=size;ap=newint[size];}

〃數組相加函數原型voidSort();//排序

void戰算符+函數

operaor+(constArray&other);

intm

{int

:)

a(10),b(10);〃聲明對象

Son

Sona-Sort();b.Sort();//調用排序方法

Add(a,b,10);a=a+b;〃數組相加

6.1.1定義類與對象P218

?C++中,類定義的說明語句一般形式為:

class類名

{public:

公有性質數據成員和成員函數;

protected:

保護性質數據成員和成員函數;

private:

私有性質數據成員和成員函數;

};

說明P218

?其中,class是定義類的關鍵字。

■“類名”是用戶自定義的標識符,用于標

識類型名字。通常第一個字母大寫。

?一對花括號相括說明類成員,以分號結束

類定義語句。

*成員的性質

-類成員用關鍵字指定不同訪問特性,決定其在類體系

中或類外的可見性。

?private聲明私有成員:私有性質成員僅在類中可見。

不能在類外或派生類中使用。如果私有類成員放在放

在第一段,則可以省略關鍵字private。(建議不省)

?protected聲明保護成員:保護性質成員在本類和它

的派生類中可見。

?public聲明公有成員:公有性質的成員是的接口,在

類中和類外可見。

?各段中既可以包含數據成員,也可以包含成員函數。

成員函數可在類外定義P219

?成員函數在類外定義,使用作用域區分符

進行說明,此時函數頭形式為:

返回類型類名::函數名(參數表)

?簡單的成員函數實現可以在類中定義,此

時編譯器作內聯函數處理。

關鍵字struct也可定義類P218

?除了關鍵字class外,關鍵字struct也可

定義類。

?用關鍵字struct也可定義類時,若不特別

指出,則所有成員都是公有成員。

關于類聲明的進一步說明

1.允許已定義類名出現在類的說明中

允許已定義類名出現在類的說明中

例:

classX

);

classY

{XdataMember;

);

允許已定義類名出現在類的說明中

例:

classlink

{link*next;

//聲明一個指向link類類型的指針

);

2.類可以無名,用于直接聲明對象

}mydate;

3.類是一個程序包

?可以只有數據成員或只有成員函數,或者為空

#include<iostream>

usingnamespacestd;

classempty{};

intmain()

{emptye1;

cout?M&e1="?&e1?endl;

cout?Msizeofe1=M?sizeof(el)?endl;

}

強調

注:

1.允許已定義類名出現在類的說明中

2.類可以無名,用于直接聲明對象

3.類是一個程序包。可以只有數據成員或只有成員函數,或者為空。

//例

#include<iostream>

CAC:\WIND0WS\systefn32\(

usingnamespacestd;*1=0012FF63日

classempty{};sizeofe1=1

intmain()請按任意犍繼續..

Lil

{emptyel;

cout?n&el="vv&el?endl;

cout?"sizeofel="?sizeofifel)?endl;

“類”類型變量的定義P219

?“類”類型變量的定義,定義方法與普通結

構類型變量定義方法相同。

?說明一個類類型的對象(變量)后,編譯

器為每個對象的數據成員分配內存。

?對象沒有成員函數的副本,類成員函數可

以被對象調用。

6.1.2訪問對象成員P220

?公有成員是提供給外部的接口。

?類外用”.”和“?>”運算符訪問對象成員。

訪問對象的公有成員P220

〃例6」訪問對象的公有成員P220

#include<iostream>

usingnamespacestd;

classTclass

{public:

intx,y;

voidprint()

{cout?xw"Jwy;};

);

intmain()

{Tclasstest;

test.x=100;test.y=200;

test.print();

)

〃例6?2用指針訪問對象成員

#include<iostream>

usingnamespacestd;

classTclass

{public:

intx,y;

voidprint(){cout?x??y?endl;};

);

intadd(Tclass*ptf){return(ptf->x+ptf->y);}

intmain()

{Tclasstest,*pt=new(Tclass);

pt->x=100;pt->y=200;

pt->print();

test.x=150;test.y=450;

test.print();

cout?"x+y="?add(&test);

)

6.1.3this指針P221

?C++中,同一類的各個對象都有自己的數據成員

的存儲空間,系統不為每個類的對象建立成員函

數副本,但類的成員函數可以被各個對象調用。

?C++為成員函數提供了一個稱為this的隱含指針

參數,所以常常稱成員函數擁有this指針。

?當一個對象調用類的成員函數時,對象的地址被

傳遞給this指針,即this指針指向了該對象。

?this指針不能顯式聲明,但可以顯式使用。

說明

?this指針是一個常指針,相當于:

?class_Type*constthis;

?其中:

?class_Type是用戶定義的類類型標識符。

■這里,this指針一旦初始化(成員函數被調用)

之后,獲取了對象地址,指針值就不能再修改

和賦值,以保證不會指向其它對象。

?this指針的顯式使用主要在運算符重載、自引

用等場合。

this指針

#include<iostream>

usingnamespacestd;

classSimple

{intx,y;

public:

voidsetXY(inta,intb){x=a;y=b;}

voidprintXY(){cout?x?”,?y?endl;};

};

intmain()

{Simpleobjl,obj2,obj3;

objl.setXY(10,15);obj1.xobjl.y

obj1.printXY();

obj2.setXY(20,25);obj2.xobj2.y

obj2.printXY();

obj3.setXY(30,35);obj3.xobj3.y

obj3.printXY();

6.1.3this指針

#include<iostream>

usingnamespacestd;

classSimple

{intx,y;

public:

voidsetXY(inta,intb){x=a:y=b;}

voidprintXY(){cout?x?”,?y?endl;};

};

intmain()

{Simpleobjl,obj2,obj3;

objl.setXY(10,15);obj1.xobjl.y

obj1.printXY();

obj2.setXY(20,25);obj2.xobj2.y

obj2.printXY();

obj3.setXY(30,35);obj3.xobj3.y

obj3.printXY();

6.1.3this指針

#include<iostream>向哪個對象

usingnamespacestd;的數據成員賦值?

classSimple

{intx,y;

public:

voidsetXY(inta,intb){x=a:y=b;}

voidprintXY(){夕out<<x??y?endl;};

);//

??八11

mtmam()//

{Simpleobj1,objQ,obj3;

objl.setXY(10,15,;

obj1.printXY();

obj2.setXY(20,25);

obj2.printXY();

obj3.setXY(30,35);

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

成員函數隱含定義this指針

usingnamespacestd;

classSimple接受調用對象的地址

{intx,y;

public:

voidsetXY(inta,intb){x=a;y

voidsetXY(inta,intb,Simple*con){this->x=a;this->y=b;}

intmain()

{Simpleobj1,obj2,,

objl.setXY(10,15Mobjn;

obj1.printXY();

obj2.setXY(20,25);

obj2.printXY();

obj3.setXY(30,35);

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

this指針不能顯式聲明

usingnamespacestd;

classSimple

{intx,y;

public:

voidsetXY(inta,intb){x=a;y:

voidsetXY(inta,intb){this->x=a;this->y=b;}

J,

intmain()

{Simpleobj1,obj2,obj3;

objl.setXY(10,15);obj1.xobjl.y

obj1.printXY();

obj2.setXY(20,25);obj2.xobj2.y

obj2.printXY();

obj3.setXY(30,35);obj3.xobj3.y

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

this指針不能顯式聲明

usingnamespacestd;

classSimple可以顯式使用

{intx,y;

public:

voidsetXY(inta,intb){x=a;y:

voidsetXY(inta,intb){this->x=a;this->y=b;}

J,

intmain()

{Simpleobj1,obj2,obj3;

objl.setXY(10,15);obj1.xobjl.y

obj1.printXY();

obj2.setXY(20,25);obj2.xobj2.y

obj2.printXY();

obj3.setXY(30,35);obj3.xobj3.y

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

usingnamespacestd;

classSimple通過調用函數的對象

{intx,y;this指針獲取對象地址

public:

voidsetXY(inta,intb){x=a;y=b;}

voidprintXYQ4cout?x?vvy?endl;};

/

intmain()/

{Simnlgfobjl,obj2,obj3;

objl?>etXY(10,15);obj1.xobjl.y

obj1.printXY();

obj2.setXY(20,25);obj2.xobj2.y

obj2.printXY();

obj3.setXY(30,35);obj3,xobj3.y

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

usingnamespacestd;

classSimple

{intx,y;

public:

voidsetXY(inta,intb){x=a;y=b;}

voidprintXY(){cout?x?n,n?y?endl;};

};

intmain()

{Simpleobj1,obj2,obj3;

objl.setXY(10,15);obj1.x10objl.y15

obj1.printXY();

obj2.setXY(20,25);obj2.xobj2.y

obj2.printXY();

obj3.setXY(30,35);obj3.xobj3.y

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

usingnamespacestd;

classSimple

{intx,y;

public:

voidsetXY(inta,intb){x=a;y=b;}

voidprintXY(){cout?x?n,n?y?endl;};

};

intmain()

{Simpleobj1,obj2,obj3;

objl.setXY(10,15);obj1.x10objl.y15

obj1.printXY();

obj2.setXY(20,25);obj2.xobj2.y

obj2.printXY();

obj3.setXY(30,35);obj3.xobj3.y

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

usingnamespacest^;在objl上操作10,15

classSimple

{intx,y;

public:

voidsetXX(inta,intb){x=a;y=b;}

voidprintXY(){cout?xvv"J?y?endl;};

intmain()

{Simpleobj1,obj2,obj3;

objl.setXY(10,15);

Cggp.printXY^

obj2.setXY(20,25);

obj2.printXY();

obj3.setXY(30,35);

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

usingnamespacestd;10,15

classSimple

{intx,y;

public:

voidsetXY(inta,intb){x=a;y=b;}

voidprintXY(){cout?x?n,n?y?endl;};

};

intmain()

{Simpleobj1,obj2,obj3;

objl.setXY(10,15);obj1.x10objl.y15

obj1.printXY();

obj2.setXY(20,25);obj2.y

obj2.printXY();

obj3.setXY(30,35);obj3,xobj3.y

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

usingnamespaces叱

在obj2上操作10,15

classSimple

{intx,y;

public:

voidsetXY(inta,intb){x=a;y=b;}

voidprintXY(){cout?x?n,n?y?endl;};

intmain()

{Simpleobj1,obj2,obj3;

objl.setXY(10,15);

obj1.printXY();

之亞.setXY(2U,軍力;

obj2.printXY();

obj3.setXY(30,35);

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

町在上操作

usingnamespaces0bj?10,15

classSimple/_______________

{intx,y;/

public:J

voidsetXY(inta,intb){x=a;y=b;}

voidprintXY(){cout?x?n,n?y?endl;};

intmain()

{Simpleobj1,obj2,obj3;

objl.setXY(10,15);obj1.x10objl.y15

obj1.printXY();

COST.setXY(2025^obj2.x20obj2.y25

obj2.printXY();

obj3.setXY(30,35);obj3.xobj3.y

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

usingnamespacestd;10,15

classSimple

{intx,y;

public:

voidsetXY(inta,intb){x=a;y=b;}

voidprintXY(){cout?x?n,n?y?endl;};

};

intmain()

{Simpleobj1,obj2,obj3;

objl.setXY(10,15);obj1.x10objl.y15

obj1.printXY();

obj2.setXY(20,25);obj2.x20obj2.y25

obj2.printXY();

obj3.setXY(30,35);obj3.xobj3.y

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

usingnamespacest^;在obj2上操作10,15

classSimple

{intx,y;

public:

voidsetXX(inta,intb){x=a;y=b;}

voidprintXY(){cout?xvv"J?y?endl;};

intmain()

{Simpleobj1,obj2,obj3;

objl.setXY(10,15);obj1.x10objl.y15

obj1.printXY();

obj2.setXY(20,25);obj2.x20obj2.y25

?printXY^

obj3.setXY(30,35);obj3.xobj3.y

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

usingnamespacest^;在obj2上操作10,15

classSimple20,25

{intx,y;

public:

voidsetXX(inta,intb){x=a;y=b;}

voidprintXY(){cout?xvv"J?y?endl;};

intmain()

{Simpleobj1,obj2,obj3;

objl.setXY(10,15);obj1.x10objl.y15

obj1.printXY();

obj2.setXY(20,25);obj2.x20obj2.y25

?printXY^

obj3.setXY(30,35);obj3.xobj3.y

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

usingnamespacestd;10,15

classSimple20,25

{intx,y;

public:

voidsetXY(inta,intb){x=a;y=b;}

voidprintXY(){cout?x?n,n?y?endl;};

};

intmain()

{Simpleobj1,obj2,obj3;

objl.setXY(10,15);obj1.x10objl.y15

obj1.printXY();

obj2.setXY(20,25);obj2.x20obj2.y25

obj2.printXY();

obj3.setXY(30,35);obj3.xobj3.y

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

usingnamespacestd;10,15

classSimple20,25

{intx,y;

public:

voidsetXY(inta,intb){x=a;y=b;}

voidprintXY(){cout?x?n,n?y?endl;};

intmain()

{Simpleobj1,obj2,obj3;

objl.setXY(10,15);obj1.x10objl.y15

obj1.printXY();

obj2.setXY(20,25);obj2.x20obj2.y25

obj2.printXY();

obj3,setXY(30,35);obj3.xobj3.y

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

usingnamespacestd;10,15

classSimple20,25

{intx,y;

public:

voidsetXY(inta,intb){x=a;y=b;}

voidprintXY(){cout?x?n,n?y?endl;};

};

intmain()

{Simpleobj1,obj2,obj3;

objl.setXY(10,15);obj1.x10objl.y15

obj1.printXY();

obj2.setXY(20,25);obj2.x20obj2.y25

obj2.printXY();

obj3,setXY(30,35);obj3.x30obj3.y35

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

usingnamespacestd;10,15

classSimple20,25

{intx,y;

public:

voidsetXY(inta,intb){x=a;y=b;}

voidprintXY(){cout?x?n,n?y?endl;};

};

intmain()

{Simpleobj1,obj2,obj3;

objl.setXY(10,15);obj1.x10objl.y15

obj1.printXY();

obj2.setXY(20,25);obj2.x20obj2.y25

obj2.printXY();

obj3.setXY(30,35);obj3.x30obj3.y35

obj3.printXY();

6.1.3this指針

#include<iostream>〃例6-5

usingnamespacestd;10,15

classSimple20,25

{intx,y;

public:

voidsetXY(inta,intb){x=a;y=b;}

voidprintXY(){cout?xvv"J?y?endl;};

intmain()

{Simpleobj1,obj2,obj3;

objl.setXY(10,15);obj1.x10objl.y15

obj1.printXY();

obj2.setXY(20,25);obj2.x20obj2.y25

obj2.printXY();

obj3.setXY(30,35);obj3.x30obj3.y35

obj3,printXY();

6.1.3this指針

#include<iostream>〃例6-5

usingnamespacestd;10,15

classSimple20,25

{intx,y;30,35

public:

voidsetXY(inta,intb){x=a;y=b;}

voidprintXY(){cout?xvv"J?y?endl;};

intmain()

{Simpleobj1,obj2,obj3;

objl.setXY(10,15);obj1.x10objl.y15

obj1.printXY();

obj2.setXY(20,25);obj2.x20obj2.y25

obj2.printXY();

obj3.setXY(30,35);obj3.x30obj3.y35

obj3,printXY();

6.1.3this指針

#include<iostream'

.一J

usingnamespacesi15

成員函數擁有this指針

classSimple15

{intx,y;行

public:

voidsetXY(inta,intb){x=a;y=b;}

voidprintXY(){cout?x?n,n?y?endl;};

};

intmain()

{Simpleobjl,obj2,obj3;

objl.setXY(10,15);obj1.x10objl.y15

obj1.printXY();

obj2.setXY(20,25);obj2.x20obj2.y25

obj2.printXY();

obj3.setXY(30,35);obj3.x30obj3.y35

obj3.printXY();

6.2構造函數和析構函數P222

基本概念

■在類中構造函數和析構函數都是特殊函數。

?構造函數用于建立類的實例;

?析構函數用于撤消類的實例。

■程序并不顯式調用這些函數,當程序創建一個

對象時,系統自動調用構造函數;

?當程序撤消一個對象時,系統自動調用析構函

數。

?這些都是編譯器自動調用的。

構造函數P222

?構造函數是用于創建對象的特殊成員函數。

?當創建對象時,系統自動調用構造函數。

?構造函數的作用是:

為對象分配空間;

對數據成員賦初值;

請求其他資源。

說明

?構造函數名與類名相同:類名

?構造函數可以有任意類型的參數,但沒有返回

類型。

?構造函數可以由用戶提供,也可使用系統的默

認版本。

?如果沒有用戶定義的構造函數時,系統提供缺

省版本的構造函數,只負責對象的創建,不能

為對象中的數據成員賦初值。(不做數據成員

初始化工作)

?構造函數可以重載。

何時調用構造函數

?何時調用構造函數十分明確:

?定義類的對象時;

?調用new或者new□運算符動態開辟內存空間時。

?在對象被創建的時候就會調用構造函數,

?比如:聲明一個類classA{?.J,

?當你在main函數中Aa;

?定義一個對象a的時候,就調用構造函數。

?默認構造函數都是無參數的。

何時調用析構函數

?何時調用構造函數要十分明確:

?當函數調用結束的時候,會自動調用析構

函數,析構的作用就是釋放對象占用的資

源。

?以為默認析構什么也不打印,所以你不會

直觀看出析構函數什么時候執行,i己在

析構中加上打印的語言你就會看到了。

構造函數和析構函數

例:

classAA

{public:

AA(){……}

?AA(){……}

};

voidtest()

{intx,y;

AAtl,t2;

return;

構造函數和析構函教_

構造函數

};

voidtest()

{intx,y;

AAtl,t2;

return;

構造函數和析構函數

例:/析構函數

classAA//

{public://

AA(){…./

);

voidtest()

{intx,y;

AAtl,t2;

return;

構造函數和析構函數

例:

classAA

{public:

AA(){……}

?AA(){.......

};

voidtest()

{intx,y;〃聲明變量時開辟兩個整型存儲空間

AAtl,t2;

return;

構造函數和析構函數

例:

classAA

{public:

AA(){……}

?AA(){.......

};

voidtest()

{intx,y;

〃兩次調用構造函數,對象tl,t2

return;

構造函數和析構函數

例:

classAA

{public:

AX01

?AA(){

};

voidtest()

{intx,y;

AAtl,t2;〃兩次調用構造函數,創建對象

return;

構造函數和析構函數

例:

classAA

{public:

AA(){……}

?AA(){.......

};

voidtest()

{intx,y;

AAtl,t2;

return;

〃兩次調用析構函數,撤消對象tl,t2

構造函數和析構函數

例:

classAA

{public:

AA(){

?AA(){……}

};

voidtest()

{intx,y;

AAtl,t2;

return;//兩次調用析構函數,撤消對象

構造函數和析構函數

例:

classAA

{public:

AA(){……}

?AA(){.......

};

voidtest()

{intx,y;

AAtl,t2;

return;〃兩次調用析構函數,撤消對象

}

〃變量生存期結束

6.2.1簡單構造函數和析構函數

P222

■構造函數原型

類名::類名(參數表)、

?析構函數原型

類名~類名()

#include<iostream>//例6-3重寫日期類P223

usingnamespacestd;

classDate

{public:

Date();

-Date();

voidSetDate(inty,intm,intd);

intIsLeapYear();

voidPrintDate();

private:

intyear,month,day;

};

Date::Date(){cout?"DateobjectinitializedAn";}〃無參構造函數

Date::-Date(){cout?nDateobjectdestroyed.\n";}//析構函數

voidDate::SetDate(inty,intm,intd){year=y;month=m;day=d;}

intDate::IsLeapYear(){return(year%4==0&&year%100!=0)||(year%400==0);}

voidDate::PrintDate(){cout?year??month??day?endl;}

intmain()

{Dated;

d.SetDate(2001,10,1);

d.PrintDate();

}

#include<iostream>〃例6?3重寫日期類P223

usingnamespacestd;

classDate

{public:

Date();

~Date();

voidSetDate(inty,intm,intd);

intIsLeapYear();

voidPrintDate();

private:

intyear,month,day;

};

Date::Date(){coutvv"Dateobjectinitialized.、!!'';}〃無參構造函數

Date::~Date()"Dateobjectdestroyed.\n";}//析構函

voidDate::SetDate(intintd){year=y;monthday=d;}

intDate::IsLeapYear()

voidDate::PrintDate()

創建對象時

intmain()

調用構造函數

d.SetDate(2001,10,l);

d.PrintDate();

#include<iostream>〃例重寫日期類P223

usingnamespacestd;

classDate

{public:

Date();

?Date。;

voidSetDate(inty,intm,intd);

intIsLeapYear();

voidPrintDate();

private:

intyear,month,day;

};

Date::Date(){cout?"Dateobjectinitialized.、!!";}//無參構造函數

Date::-Date(){cout?”Dateobjectdestroyed.\nn;}〃析構函數

voidDate::Setinty,intm,intd){year=y;month=m;day=d;}

intDate::IsLeapYear(){reear%4==0&&year%100!=0)||(year%400==0);}

voidDate::PrintDate(){cout?year<?ndl;}

intmain()作用域結束時

{Dated;調用析構函數

d.SetDate(2001,10,ll<

d.PrintDate();

)

#include<iostream>〃例6-3重寫日期類P223

usingnamespacestd;

CAC:\VIID0TS\systeB32\cBd.exe

classDate

Dateobjectinitialized.

{public:2001/10/1

Date();Dateobjectdestroyed.

~Date();請按任意鍵繼續.?.

voidSetDate(inty,intm,intd);

intIsLeapYear();

溫馨提示

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

評論

0/150

提交評論