第十八章 类别与物件.docx

上传人:cl****1 文档编号:559753387 上传时间:2022-09-03 格式:DOCX 页数:16 大小:35.74KB
返回 下载 相关 举报
第十八章 类别与物件.docx_第1页
第1页 / 共16页
第十八章 类别与物件.docx_第2页
第2页 / 共16页
第十八章 类别与物件.docx_第3页
第3页 / 共16页
第十八章 类别与物件.docx_第4页
第4页 / 共16页
第十八章 类别与物件.docx_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《第十八章 类别与物件.docx》由会员分享,可在线阅读,更多相关《第十八章 类别与物件.docx(16页珍藏版)》请在金锄头文库上搜索。

1、第十八章 類別與物件18-1程式設計方法的演進演進一.function or subroutine使程式變的更有組織,提高程式的再利用率1. 經嚴格測試後,函數被保留下來,成為處理同類問題之building blocks2. 同一類函數可以聚集成模組(module)3. 有系統的方式安排各程式模組,搭配嚴謹的輸出輸入資料控管,稱為structured programming or procedural - based programming演進二.物件導向程式設計(Object-Oriented Programming,OOP),人以物件導向的方式來認識世界1. 自然界中萬物皆為具有各種特性(

2、attribute),有各種特殊的行為(behavior),視為一個object2. 物件導向程式設計的精神:程式中保留各種物件的特性,規劃出使用者與物件,以及物件與物件之間的互動關係18-2抽象化和資料的隱藏Abstraction 抽象化 考慮三要點(規劃程式用)(1)系統有哪些物件參與(2)參與物件特質(3)物件如何與外界互動Encapsulation 封裝1. attribute如蛋黃,隱藏在中心,不能夠直接碰觸,代表了物件的狀態2. interface如蛋殼,可以直接與外界接觸3. behaviors如蛋白,可以經由介面與外界的互動而改變內部的特徵值,並把反應經由介面表現出來18-3

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

4、 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)在一起,更能表達為一個完整的類

5、別二、 類別由資料成員與函數成員封裝而成(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:clas

6、s 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(instan

7、ce)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;usi

8、ng 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

9、 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 s

10、td: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.perimete

11、r()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 CWINpu

12、blic: 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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号