算法與數據結構:lecture 7 怎樣使用類和對象b_第1頁
算法與數據結構:lecture 7 怎樣使用類和對象b_第2頁
算法與數據結構:lecture 7 怎樣使用類和對象b_第3頁
算法與數據結構:lecture 7 怎樣使用類和對象b_第4頁
算法與數據結構:lecture 7 怎樣使用類和對象b_第5頁
已閱讀5頁,還剩31頁未讀 繼續免費閱讀

下載本文檔

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

文檔簡介

1、常對象定義類名 const 對象名(實參表);const 類名 對象名(實參表)常對象的所有數據成員的值均不能修改必須修改的數據成員必須聲明為mutable常對象只能調用常成員函數。 常對象成員常數據成員用const聲明只能通過構造函數的參數初始化表對常數據成員進行初始化。常成員函數聲明:類型名 函數名(參數表) const常成員函數的使用:P279表9.1#include using namespace std;class Xpublic: X(int k=0):m(k) const int m;int main( ) X a(5), b; couta.m b.mendl; return 0

2、;#include using namespace std;class Xpublic: X( ):m_flag(true), m_accessCount(0) void GetFlag( ) const m_accessCount+; coutm_flag m_accessCountendl; private: bool m_flag; mutable int m_accessCount;int main( )const X myX;for(int i=0; i10; i+) myX.GetFlag();return 0;指向對象的常指針(const pointer)將指針變量聲明為cons

3、t型,則指針變量的值將不能改變定義形式:類名 * const 指針變量名一般作為函數的形參指向常對象的指針變量(pointer to const)如果一個對象被聲明為常對象,則只能用指向常對象的指針變量指向它如果定義了一個指向常對象的指針變量,并使它指向一個非const的對象,則其指向的對象是不能通過該指針變量來改變的。指向常對象的指針最常用于函數的形參當希望在調用函數時對象的值不被改變,就應把形參定義為指向常對象的指針變量,同時用對象的地址作實參。#include using namespace std;class Xpublic: X(int k=0):m(k) int m;int mai

4、n( ) const X a(5); X b; const X *p; X *q; p=&a; q=&b; coutm mendl; return 0;能不能p=&b;能不能q=&a;#include using namespace std;class Timepublic:Time(int h, int m, int s):hour(h),minute(m),sec(s)int hour,minute,sec;void fun(Time &t)t.hour=18;int main( ) Time t1(10,13,56); fun(t1); coutt1.hourendl; return 0

5、;對象的常引用 P283 例9.8紅色部分能不能改成const Time&t18對象的動態建立和釋放可以用new運算符動態建立對象,用delete運算符撤銷對象。建立對象時,將開辟空間存儲對象,并且調用對象所屬類的構造函數來初始化對象。執行new運算時,如果無法開辟內存空間,則返回 NULL。執行delete運算時,在釋放內存空間之前,自動調用析構函數。對象的賦值和復制同類對象間可以相互賦值。形式:對象名1=對象名2;賦值時將一個對象的數據成員在存儲空間的狀態復制給另一個對象的數據成員的存儲空間。數據成員中不能包括動態分配的數據復制:用已有的對象去克隆出一個新對象形式1:類名 對象2(對象1)

6、;形式2:類名 對象2=對象1;區別賦值與復制的區別賦值是對一個已經存在的對象賦值復制是從無到有的建立一個新對象,并使它與一個已有的對象完全相同。普通構造函數與復制構造函數的區別:復制構造函數的參數是對象,目的是用已有對象去復制一個新對象。#include using namespace std;class Boxpublic: Box(int h=10, int w=10, int l=10):height(h), width(w), length(l) int volume( ) const return height*width*length; private: int height,

7、width, length;int main( ) Box box1(15,30,25), box2; coutbox1.volume=box1.volume( ), box2.volume= ; coutbox2.volume( )endl; box2=box1; coutbox2.volume=box2.volume( )endl; return 0;P286 例9.9#include using namespace std;class Boxpublic: Box(int h=10, int w=10, int l=10):height(h), width(w), length(l) i

8、nt volume( ) const return height*width*length; private: int height, width, length;int main( ) Box box1(15,30,25); Box box2=box1, box3(box1); coutbox2.volume=box2.volume( ); cout, box3.volume=box3.volume( )endl; return 0;例 dog5.cppint main( )Dog d1(fido,3,15,brown);Dog d2(pooch,4,20,white);d1.print(

9、);Dog d3(d1);d3.print( );d3=d2;return 0;例 dog6.cppclass Dog string name; int age, weight ;string color ;public:Dog(string n, int a, int w, string c):name(n), age(a), weight(w), color(c) Dog(const Dog& d) name=+1; age=d.age+1; weight=d.weight+1; color=d.color;Dog( )coutBye nameendl;void print(

10、) coutname is a age year old color;cout dog who weighs weight pounds.n;靜態成員支持同類的多個對象之間實現數據共享。靜態數據成員以關鍵字static開頭為各對象所共有,在內存中只占一份空間在程序開始時,被分配空間;程序結束時,才釋放空間只能在類體外進行初始化形式:數據類型 類名:靜態數據成員名=初值;不能用參數初始化表初始化。既可以通過對象名引用,也可以通過類名引用#include using namespace std;class Boxpublic: Box(int w=10, int l=10) : width(w),

11、 length(l) int volume( ) const return height*width*length; static int height; int width, length;int Box:height = 10;int main( ) Box a(15,20), b(20,30); couta.heightendl; coutb.heightendl; coutBox:heightendl; couta.volume( )endl; return 0;P291 例9.10能不能static int height=10;靜態成員函數在類中聲明函數的前面加上static就成了靜

12、態成員函數。靜態成員函數是類的一部分,而不是對象的一部分。在類外調用公用的靜態成員函數,要用類名和域運算符。靜態成員函數沒有this指針靜態成員函數主要用來訪問靜態數據成員訪問本類的非靜態成員時,要加上對象名和成員運算符#include using namespace std;class Studentpublic:Student(int n, int a, float s):num(n), age(a), score(s)void total( ) sum+=score;count+;static float average( )return sum/count;private:int nu

13、m, age;float score;static float sum;static int count;float Student:sum=0;int Student:count=0;P293 例9.11total能否改成const函數?average能否改成const函數?int main( ) Student stud3=Student(1001,18,70),Student(1002,19,78),Student(1005,20,98); int n; coutn; for(int i=0; in; i+) studi.total( ); coutthe average score o

14、f n students is ; coutStudent:average( )endl; return 0;例 dog7.cppclass Dog string name; int age, weight ;string color ;static string owner; public:Dog(string n, int a, int w, string c):name(n), age(a), weight(w), color(c) Dog( )coutBye nameendl;void print( ) cout name is a age year old color;cout do

15、g who weighs weight pounds.n;void printOwner( )coutowner:ownerendl;static void setOwner(const string& o)owner=o;string Dog:owner = Tom;例 dog7.cppint main()Dog d1(fido,3,15,brown);Dog d2(pooch,4,20,white);d1.printOwner( );d2.setOwner(Jack);d1.printOwner( );return 0;友元友元可以訪問與其有好友關系的類中的私有成員。友元函數:如果在本類以

16、外定義了一個函數,并在類體中用friend對其進行聲明,則此函數為本類的友元函數。將普通函數聲明為友元函數友元成員函數友元類:可以將B類聲明為A類的“朋友”,此時B類就是A類的友元類。#includeusing namespace std;class Timepublic:Time(int h, int m, int s):hour(h), minute(m), sec(s)friend void display(const Time &) ;private:int hour, minute, sec;void display(const Time &t)coutt.hour:t.minute

17、:t.secendl;int main( )Time t1(10,13,56);display(t1);return 0;普通函數聲明為友元函數P296 例9.1210:13:56例 dog8.cppclass Dog string name; int age, weight ;string color ;public:Dog(string n, int a, int w, string c):name(n), age(a), weight(w), color(c) Dog( )coutBye nameendl;void print( ) cout name is a age year old

18、 color;cout dog who weighs weight pounds.n;friend void welcome(const Dog& );例 dog8.cppvoid welcome(const Dog& d)coutwelcome !n;int main()Dog d1(fido,3,15,brown);welcome(d1);Dog d2(pooch,4,20,white);welcome(d2);return 0;#include using namespace std;class Date;class Timepublic:Time(int h, int m,

19、 int s): hour(h), minute(m), sec(s)void display(const Date&);private:int hour, minute, sec;class Datepublic:Date(int m, int d, int y):month(m), day(d), year(y)friend void Time:display(const Date&);private:int month, day, year;成員函數聲明為友元函數P297 例9.13void Time:display(const Date& d)coutd.month/d.day/d.y

20、earendl;couthour:minute:secendl;int main( )Time t1(10,13,56);Date d1(12,25,2004);t1.display(d1);return 0;class Datepublic:Date(int m, int d, int y):month(m), day(d), year(y)friend Time;/ friend void Time:display(const Date&);private:int month, day, year;友元類P307 練習11#include using namespace std;class

21、 Date;class Timepublic:Time(int h, int m, int s): hour(h), minute(m), sec(s)friend void display(const Date&, const Time&);private:int hour, minute, sec;class Datepublic:Date(int m, int d, int y):month(m), day(d), year(y)friend void display(const Date&, const Time&);private:int month, day, year;同時成為兩

22、個類的友元函數P307 練習10void display(const Date& d, const Time& t)coutd.month/d.day/d.yearendl;coutt.hour:t.minute:t.secendl;int main( )Time t1(10,13,56);Date d1(12,25,2004);display(d1, t1);return 0;類模板可能有兩個或多個類,其功能是相同的,僅僅是數據類型不同。class Compare_intpublic:Compare_int(int, int);int max( );int min( );private:in

23、t x, y;class Compare_floatpublic:Compare_float(float, float);float max( );float min( );private:float x, y;類模板(2)可以聲明一個通用的類模板,它可以有一個或多個虛擬的類型參數。類模板是類的抽象,類是類模板的實例。template class Compare public:Compare (numtype, numtype); numtype max( ); numtype min( );private: numtype x, y;#include #include using namespace std;templa

溫馨提示

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

評論

0/150

提交評論