第十八章 类别与物件.docx_第1页
第十八章 类别与物件.docx_第2页
第十八章 类别与物件.docx_第3页
第十八章 类别与物件.docx_第4页
第十八章 类别与物件.docx_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第十八章 類別與物件18-1程式設計方法的演進演進一.function or subroutine使程式變的更有組織,提高程式的再利用率1. 經嚴格測試後,函數被保留下來,成為處理同類問題之building blocks2. 同一類函數可以聚集成模組(module)3. 有系統的方式安排各程式模組,搭配嚴謹的輸出輸入資料控管,稱為structured programming or procedural - based programming演進二.物件導向程式設計(Object-Oriented Programming,OOP),人以物件導向的方式來認識世界1. 自然界中萬物皆為具有各種特性(attribute),有各種特殊的行為(behavior),視為一個object2. 物件導向程式設計的精神:程式中保留各種物件的特性,規劃出使用者與物件,以及物件與物件之間的互動關係18-2抽象化和資料的隱藏Abstraction 抽象化 考慮三要點(規劃程式用)(1)系統有哪些物件參與(2)參與物件特質(3)物件如何與外界互動Encapsulation 封裝1. attribute如蛋黃,隱藏在中心,不能夠直接碰觸,代表了物件的狀態2. interface如蛋殼,可以直接與外界接觸3. behaviors如蛋白,可以經由介面與外界的互動而改變內部的特徵值,並把反應經由介面表現出來18-3 object與class的關係任何問題所涉及的物件,與有下列兩種特性1.狀態(state) 2.行為(behavior)對應至軟體為 state data members behavior member functions例如:struct Memberint Age;char Name20;故Member之state包括Age及Name(data member),行為如Inputfile.open(behavior)18-4 基礎class(參考洪維恩所著之C+教學手冊)一、 以struct來建構視窗矩形面積假設視窗編號id,視窗寬width,高height1. 定義structstruct WIN char id; int width; int height;2. 定義面積之函數int area(WIN w) return w.width*w.height;3. 主程式int main() WIN win1; win1.id = A; win1.width = 50; win1.height = 40; coutWindow win1.id , area =area(win1)endl; return 0;練習1.將上述片斷程式寫成完整程式討論:area()函數的width與height屬性是用來計算面積,如果能把三者封裝(encapsulate)在一起,更能表達為一個完整的類別二、 類別由資料成員與函數成員封裝而成(1) Data member : 例如前例之width、height(2) Function member : 例如前例之area( )(3) Encapsulation : 把事物的資料與相關函數包在一起,形成特殊結構,如以下圖示class Data memberFunction member1. Class decaration語法:class 類別名稱public: 資料型態 變數名稱; /Data member . 回傳値型態 函數名稱(參數列) /Function member statement; return; ;Example:class CWINpublic: /宣告成員屬性為公有 char id; int width; int height; int area() return width*height;/可直接使用成員 ;說明: public:代表其成員(member),可隨意在class外部作存取private:成員只能在類別內部作存取,也可經由public的Function member存取private成員如果省略public,則所有成員均為private2. 建立物件與存取 Example:CWIN win1; /Declaring a variable win1 below CWIN class(instance)win1.id = A; /存取物件成員win1.width = 50; /存取物件成員win1.height = 40; /存取物件成員範例練習1.將上述過程建立一個完整的程式,並將win1之成員印在螢幕上範例練習2.查詢上題宣告所建立物件佔了多少bytes可使用sizeof(win1)或sizeof(CWIN)即可得知3. 使用函數(函數已定義於class之內)語法:物件名稱.函數名稱(參數列)Example: (18-3-2.cpp) 可於範例練習1中加入下列指令 coutArea=win1.area()endl;完整程式如下:#include using std:cout;using std:endl;using std:cin;class CWIN public: char id; int width; int height; int area() return width * height; ;int main()CWIN win1;win1.id = a;win1.width = 50;win1.height = 60;coutThe id of win1 is win1.idendl;coutThe width of win1 is win1.widthendl;coutThe height of win1 is win1.heightendl;coutThe area of win1 is win1.area()endl;return 0;可將class 與 main program分開,如下(1) Area.h程式碼,檔名area.h#ifndef area_h#define area_hclass CWIN public: char id; int width; int height; int area() return width * height; int perimeter() return 2*(width+height); ;#endif(2) 18-1-2.cpp程式碼#include using std:cout;using std:endl;using std:cin;#include area.hint main()CWIN win1;win1.id = a;win1.width = 50;win1.height = 60;coutThe id of win1 is win1.idendl;coutThe width of win1 is win1.widthendl;coutThe height of win1 is win1.heightendl;coutThe area of win1 is win1.area()endl;coutThe perimeter of win1 is win1.perimeter()endl;return 0;4. 於類別內定義多個函數成員class內可定義多個函數,如下例class CWINpublic:char id;int width;int height;int area( )return width*height;int perimeter( )return 2*(width+height);範例練習(18-3-3.cpp)於主程式增加如下程式coutPerimeter =win1.perimeter()width*this-height; ;範例練習(18-3-5.cpp)將上述this指標加程式中7. 類別內呼叫函數成員如下例:class CWINpublic: char id; int width; int height; int area() return width*height; void show_area(void) coutWindow id , area=area()endl; ;範例練習(18-3-6.cpp)於主程式中加入下列敘述win1.show_area();18-5 函數引數的傳遞與多載一、 引述的傳遞之前18-4所撰寫的函數均無引述傳遞,如下int area(void) return width*height;其實函數可以加上各種資料型態的引數,如下例class CWINpublic: char id; int width; int height; int area() return width*height; void set_data(char i, int w, int h) id = i; width = w; height = h; ;void show_area(CWIN win) coutWindow win.id , area= win.area()endl;int main(void) CWIN win1; win1.set_data(B, 50, 40); show_area(win1); return 0;上例中set_data函數為win1之成員範例練習(18-4-1.cpp),將上述過程寫成程式二、 函數的多載以下例說明:(18-4-2.cpp)class CWINpublic: char id; int width; int height; int area() return width*height; void show_area() coutWindow id , area=area()endl; void set_data(char i, int w, int h) /First Function of set_data() id = i; width = w; height = h; void set_data(char i) /Second Function of set_data() id = i; void set_data(int w, int h) /Third Function of set_data() width = w; height = h; ;int main() CWIN win1, win2; win1.set_data(A, 50, 40); win2.set_data(B); win2.set_data(80,120); win1.show_area(); win2.show_area(); return 0;說明: win1.set_data(A, 50, 40) win1物件的所有資料成員皆可被設値win2.set_data(B)設定id的值win2.set_data(80,120)設値width及height結論:同一名稱的函數可具有不同的引數輸入18-6 公有成員與私有成員如果資料成員沒有一個機制來限定類別中成員存取,容易造成錯誤的輸入完整設定方式如下:class 類別名稱private: 私有成員 .public: 公有成員 .;例如: id、width與height資料成員為私有,area()函數為公有class CWINprivate: char id; int width; int height;public: int area() return width*height; ;依據上述定義,下列程式為錯誤示範int main() WIN win1; win1.id = A; win1.width = -5; win1.height = 12; return 0;因此為解決上述問題,需要建立公有成員(public member)來解決如下例:class CWINprivate: char id; int width; int height;public: int area() return width*height; void show_area() coutWindow id , area=area() 0 & h 0) width = w; height = h; else coutinput errorendl; ;int main() CWIN win1; win1.set_data(A, 50, 40); win1.show_area(); return 0;說明:1. 透過公有成員以存取修改私有成員id、width與height因此可以在公有函數成員內和加上判斷的程式碼2. 把資料成員和函數成員依功能劃分為私有與公有,然後包裝在一個類別內來保護18-7 友誼函數(friend function)雖然不屬於某一類別之成員的函數,但是能存取此類別私有的成員,如下例class CWINpublic: void set_data(char i, int w, int h) id = i; width = w; height = h; private: char id; int width; int height;frien

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论