219-10-計算機程序計-1類和對象2(趙老師).ppt_第1頁
219-10-計算機程序計-1類和對象2(趙老師).ppt_第2頁
219-10-計算機程序計-1類和對象2(趙老師).ppt_第3頁
219-10-計算機程序計-1類和對象2(趙老師).ppt_第4頁
219-10-計算機程序計-1類和對象2(趙老師).ppt_第5頁
已閱讀5頁,還剩51頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、0,計算機程序設(shè)計C+ 第10章 類和對象(二),西安交通大學 計算機教學實驗中心,1,本章內(nèi)容,10.1 構(gòu)造函數(shù) 10.2 析構(gòu)函數(shù) 10.3 對象與指針,2,引言,/Example 9-6 日期類 #include using namespace std; class Date int day, month, year; public: void init(int,int,int);/初始化數(shù)據(jù)成員 void print_ymd(); void print_mdy(); ;,3,void Date:init(int yy, int mm, int dd) month = ( mm = 1

2、 ,4,int main() Date date1,date2; date1.print_ymd(); date1.init(2006,3,28); date1.print_ymd(); date1.print_mdy(); date2.init(2006,13,38); date2.print_ymd(); date2.print_mdy(); return 0; ,5,初始化,變量的初始化: int a; a=10; int a=10; 類的初始化 class Date int day=5, month=5, year=2009; ; /?,6,公有成員的初始化,公有成員的初始化 clas

3、s Date public: int day, month, year; void main() Date d1=5,5,2009; . ,7,有一種在聲明對象時初始化數(shù)據(jù)成員的方法,構(gòu)造函數(shù) class Date int day,month,year; public: Date();/構(gòu)造函數(shù) void init(int,int,int);/對數(shù)據(jù)成員賦值 void print_ymd(); void print_mdy(); ;,成員函數(shù)的定義 Date:Date() year = 1900; month = 1; day = 1; ,8,10.1構(gòu)造函數(shù),構(gòu)造函數(shù)(constructor

4、) 用于對 對象進行初始化的一個或一組函數(shù)。 構(gòu)造函數(shù)是特殊的公有成員函數(shù),其特征如下: 1.函數(shù)名與類名相同。 2. 無返回類型。 3.在新的對象被建立時,該對象所屬的類的構(gòu)造函數(shù)自動被調(diào)用。 4.構(gòu)造函數(shù)可以重載。它們由不同的參數(shù)表區(qū)分。,9,一、對象的初始化和構(gòu)造函數(shù),例10-1 定義一個帶構(gòu)造函數(shù)的日期類。 #include using namespace std; class Date int day,month,year; public: Date();/構(gòu)造函數(shù) void init(int,int,int);/對數(shù)據(jù)成員賦值 void print_ymd(); void prin

5、t_mdy(); ;,Date:Date() year = 1900; month = 1; day = 1; ,10,二、構(gòu)造函數(shù)的重載,構(gòu)造函數(shù)重載時,這些構(gòu)造函數(shù)的參數(shù)的個數(shù)或類型不同 例10-2 定義一個帶重載構(gòu)造函數(shù)的日期類。 #include using namespace std; class Date int day,month,year; public: Date();/構(gòu)造函數(shù) Date(int,int,int); /構(gòu)造函數(shù) void init(int,int,int); void print_ymd(); void print_mdy(); ;,11,構(gòu)造函數(shù)的重載,D

6、ate:Date() year = 1900; month = 1; day = 1; Date:Date(int yy, int mm, int dd) init(yy,mm,dd); /構(gòu)造函數(shù)調(diào)用其他成員函數(shù) ,Date:Date(int yy, int mm, int dd) year = yy; month = mm; day = dd; /合理性? ,12,成員函數(shù),void Date:init(int yy, int mm, int dd) month = ( mm = 1 ,13,主函數(shù),int main() /使用2個不同的重載構(gòu)造函數(shù)創(chuàng)建2個日期類對象 Date date1

7、, date2(2006,3,28); date1.print_ymd(); date2.print_ymd(); date1.init(2006,3,28); date1.print_ymd(); date2.init(2006,4,8); date2.print_ymd(); return 0; ,Date:Date() year = 1900; month = 1; day = 1; Date:Date(int yy, int mm, int dd) init(yy,mm,dd); ,14,三. 構(gòu)造函數(shù)中數(shù)據(jù)成員的初始化,1在構(gòu)造函數(shù)的函數(shù)體中進行初始化。 2在構(gòu)造函數(shù)的頭部初始化。

8、3混合初始化 4. 使用默認參數(shù)初始化。,15,1. 在構(gòu)造函數(shù)的函數(shù)體中初始化,class Person char Name20;int Age;charSex; public: Person() strcpy(Name, XXX); Age = 0; Sex = m; ; 例如, 當遇到聲明 Person person1; Person person2;,16,2.在構(gòu)造函數(shù)的頭部初始化。,Person:Person(): Age(0),Sex(m) . 在類的聲明中 Date():day(1),month(1),year(2011) ; /大括號不能省 其格式為: :():(), (),

9、 , () ,17,3.混合初始化,Person:Person(): Age(0),Sex(m) strcpy(m_strName, “XXX”); ,18,例10-3 構(gòu)造函數(shù)中數(shù)據(jù)成員初始化的不同方法,#include using namespace std; class Date int day, month, year; public: Date():year(1900), month(1), day(1)/構(gòu)造函數(shù),無參數(shù),使用參數(shù)初始化表 ,demo,19,拷貝構(gòu)造函數(shù),Date(int yy, int mm=1, int dd=1);/構(gòu)造函數(shù),3個參數(shù),2個有默認值 Date(

10、 Date ,20,構(gòu)造函數(shù)定義,Date:Date(int yy, int mm, int dd):year(yy), month(mm), day(dd) void Date:print_ymd() cout year - month - day endl;,21,主函數(shù)(1),int main() Date date1; /使用?構(gòu)造函數(shù)創(chuàng)建日期類對象 cout date1:; date1.print_ymd(); Date date2(2006); /使用?構(gòu)造函數(shù)創(chuàng)建日期類對象 默認? cout date2:; date2.print_ymd(); Date date3(2006,

11、4); /使用?造函數(shù)創(chuàng)建日期類對象 默認?,22,主函數(shù)(2),cout date3:; date3.print_ymd(); Date date4(2006, 4, 8); /使用3參數(shù)的構(gòu)造函數(shù)創(chuàng)建日期類對象 cout date4:; date4.print_ymd(); Date date5(date4); /使用拷貝構(gòu)造函數(shù)創(chuàng)建日期類對象 cout date5:; date5.print_ymd(); return 0; ,demo,23,使用構(gòu)造函數(shù)的注意事項,(1)構(gòu)造函數(shù)在創(chuàng)建對象時被調(diào)用,且只執(zhí)行一次。 (2)構(gòu)造函數(shù)不能被用戶顯式調(diào)用。 (3)構(gòu)造函數(shù)一般為public類型

12、。 (4)構(gòu)造函數(shù)的作用是初始化數(shù)據(jù)成員,所以一般為賦值語句。不推薦在構(gòu)造函數(shù)中編寫功能程序。 (5)只有在沒有定義構(gòu)造函數(shù)的情況下,系統(tǒng)才會自動生成默認的構(gòu)造函數(shù),但此函數(shù)不做任何事情。,24,10.2 析構(gòu)函數(shù),析構(gòu)函數(shù)(destructor) 當一個對象的生命周期結(jié)束時,C+也會自動調(diào)用一個函數(shù)注銷該對象并進行善后工作。 1.析構(gòu)函數(shù)名與類名相同,但在前面加上字符,如CGoods()。 2.析構(gòu)函數(shù)無返回類型。 3. 一個類有一個也只有一個析構(gòu)函數(shù)。 4. 析構(gòu)函數(shù)不帶任何參數(shù),因此不能重載,25,例10-4 為類Person增加構(gòu)造函數(shù)和析構(gòu)函數(shù),#include #include u

13、sing namespace std; class Person char Name20; /姓名 int Age; /年齡 charSex; /性別,26,構(gòu)造函數(shù)和析構(gòu)函數(shù),public: Person() /構(gòu)造函數(shù) strcpy(Name, XXX); Age = 0; Sex = m; Person() /析構(gòu)函數(shù) coutNow destroying the instance of Personendl; void Register(char *name, int age, char sex); void ShowMe(); ;,27,成員函數(shù),void Person: Regis

14、ter(char *name, int age, char sex) strcpy(Name, name); Age = age; Sex = (sex = m?m:f); void Person: ShowMe() cout Name t Age t Sex endl;,28,主函數(shù),int main() Person person1, person2;/對象調(diào)用構(gòu)造函數(shù) cout person1: t; person1.ShowMe(); person1.Register(Zhang3, 19, m); cout person1: t; person1.ShowMe(); cout per

15、son2: t; person2.ShowMe(); person2 = person1;/對象之間的賦值 cout person2: t; person2.ShowMe(); return 0; ,demo,29,10.3 對象與指針,指針的類型? 一、指向?qū)ο蟮闹羔?1、定義格式: 類名 *指針變量名; 例: Person person1; Person *ptr; ptr=,30,2、動態(tài)存儲,對象的動態(tài)存儲動態(tài)申請對象 指針變量new 類名(初始化值); delete 指針變量; 例: Person *p=new Person; p-ShowMe(); delete p; 聲明對象數(shù)組

16、? 動態(tài)對象數(shù)組?,31,例10-5 使用動態(tài)存儲實現(xiàn)例10-4程序的功能。,#include #include using namespace std; int main() Person *p1,*p2; /聲明兩個指向?qū)ο蟮闹羔?p1=new Person;/動態(tài)生成一個Person對象 cout ShowMe(); p1-Register(Zhang3, 19, m); cout ShowMe(); p2=new Person;/動態(tài)生成一個Person對象,32,cout ShowMe(); *p2 = *p1; /對象之間的賦值 cout ShowMe(); delete p1;/

17、釋放p1指針指向?qū)ο笏嫉目臻g delete p2;/釋放p2指針指向?qū)ο笏嫉目臻g return 0; ,demo,33,二、指向?qū)ο蟪蓡T的指針,1、指向?qū)ο髷?shù)據(jù)成員的指針變量的聲明與指向普通變量的指針完全一樣 例:類Date有公有成員int year Date date1; int *p; p=,34,指向?qū)ο蟪蓡T函數(shù)的指針,2、指向?qū)ο蟪蓡T函數(shù)的指針變量聲明時要指明它所屬的類 指向函數(shù)的指針: int (*f)(int,int) 指向成員函數(shù)的指針 int (Date:*f)(int,int) (1)定義形式:數(shù)據(jù)類型名(類名:*指針變量名)(參數(shù)表) (2)指向成員函數(shù):指針變量名=類

18、名:成員函數(shù)名 例:Date date1; void (Date:*p)(int,int,int); /聲明指向成員函數(shù)的指針 p=Date:init; /指向成員函數(shù) (date1.*p)(2007,4,8); /通過指針使用函數(shù),35,例10-6 編寫程序,演示對象指針的使用方法,#include using namespace std; class Date /類的定義 public: int day, month, year; void init(int,int,int); void print_ymd(); ;,36,成員函數(shù),void Date:init(int yy, int m

19、m, int dd) year = yy; month = mm; day = dd; void Date:print_ymd() cout year - month - day endl;,37,主函數(shù),int main() Date date1; Date *p1 = ,38,指向成員函數(shù)的指針,void (Date:*p3)(int,int,int); void (Date:*p4)(); p3 = Date:init; p4 = Date:print_ymd; (date1.*p3)(2006,4,8); (date1.*p4)(); return 0; ,demo,39,三、this

20、指針,1、C+中每一個類的成員函數(shù)都包含一個指向本類對象的指針 2、指針名為this 3、該指針包含了當前被調(diào)用的成員函數(shù)所在對象的起始地址。,40,例:class Date int year,month,day; public: Date(int year,int month,int day); print_ymd(); ;,void Date:Date(int year, int month, int day) this-year = year; this-month = month; this-day = day; ,41,例10-8 簡單的日期計算 程序功能: 給定的一個日期,計算出增

21、加若干天后的日期。 日期:年、月、日,加X天 ? 根據(jù)題目完成的功能,算法設(shè)計如下: 定義一個日期類Date,數(shù)據(jù)成員:year,month,day;適當增加一些成員函數(shù),如 構(gòu)造函數(shù),初始化函數(shù),輸出函數(shù), 日期增加一天的函數(shù), 日期增加任意天的函數(shù), 判斷閏年的函數(shù),判斷月末的函數(shù)等。,程序設(shè)計舉例,42,class Date int day,month,year; void IncDay();/日期增加一天 public: Date( int y = 1900, int m = 1, int d = 1 );/構(gòu)造函數(shù) void SetDate( int yy, int mm, int

22、dd );/日期設(shè)置 bool IsLeapYear();/ 是否閏年? bool IsEndofMonth();/ 是否月末? void print_ymd();/輸出日期yy_mm_dd void print_mdy();/輸出日期mm_dd_yy void AddDay(int);/日期增加任意天 ;,日期類Date,43,Date:Date(int y,int m,int d) SetDate(y,m,d); void Date:SetDate( int yy, int mm, int dd ) month = ( mm = 1 ,構(gòu)造函數(shù),設(shè)置日期初值函數(shù),44,switch(mon

23、th) /1,3 5, 7,8, 10 12; 4 6 9 11; 2 case 4: case 6: case 9: case 11: day = ( dd = 1 ,45,void Date:IncDay() if ( IsEndofMonth() if (month = 12) / 年末 day = 1; month = 1; year+; else / 月末 day = 1; month+; else day+; ,void Date:AddDay( int days ) for ( int i = 0; i days; i+ ) IncDay(); ,增加一天成員函數(shù),增加任意天成員

24、函數(shù),46,bool Date:IsEndofMonth() switch(month) case 4: case 6: case 9: case 11: return day = 30; case 2: if (IsLeapYear() return day = 29; else return day = 28; default:return day = 31; ,判斷月末成員函數(shù) 哪天是月末?,47,int Date:DayCalc() int days; int yy = year - 1900; days = yy*365; if(yy) days += (yy-1)/4; switc

25、h(month) case 12:days = days + 30; case 11:days = days + 31; case 10:days = days + 30; case 9:days = days + 31; case 8:days = days + 31; case 7:days = days + 30; case 6:days = days + 31;,case 5:days = days + 30; case 4:days = days + 31; case 3:if(IsLeapYear() days = days + 29; else days = days + 28;

26、 case 2:days = days + 31; default:break; days = days + day; return days; ,48,int Date:Daysof2Date(Date ymd) int days; days = abs(this-DayCalc()-ymd.DayCalc(); return days; ,兩個日期的間隔天數(shù),int Date:Daysof2Date(Date ymd) int days; days = abs(DayCalc()-ymd.DayCalc(); return days; ,49,void Date:print_ymd() c

27、outyear“-”month“-”dayendl; void Date:print_mdy() char *monthName 12 = January,February, March, April, May, June,July, August, September, October,November, December ; cout monthName month-1 day , year endl; ,輸出日期成員函數(shù),bool Date:IsleapYear() if(year%400=0)|(year%100!=0 ,判斷閏年成員函數(shù),50,int main() Date date1; date1.print_ymd(); date1.print_mdy(); date1.SetDate(

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 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

提交評論