untitled

Similar documents
untitled

I/O Files讀寫檔案:

untitled

移民資料

untitled

2011台灣高中職專題暨小論文競賽

PowerPoint 簡報

中華民國第45屆中小學科學展覽會

隱形眼鏡的世界

untitled

1

untitled

了 立 連 立 量 領 來 例 蘭 便 不 數 不 論 更 更 更 力 更 參 例 來 例 見 量 度 量 量 參 論 量 行 量 量 瑩 理 來 錄 量 量 不 力 省 力 立 力 量 量 量 了 量 便 錄 錄 錄 料 說 省 6

untitled

臺灣地區的警察教育現況與展望

人身保險業務員資格測驗方案

國立自然科學博物館館訊第263期

untitled

untitled

untitled

untitled

吃寒天真的能減肥嗎

廉 樂 不 廉 倫 理 廉 倫 理 領 不 參 領 不 若 不 不 不 不 利 聯 行 李 聯 例 律

untitled

國立陽明大學輻射防護計畫書

untitled

untitled

行政院國科會九十一年度專題研究

個人教室 / 網路硬碟

untitled

untitled

untitled

untitled

untitled

九十三年第三期檔案管理工作研習營學員建議事項答覆情形彙整表

untitled

untitled

untitled

untitled

94年度學習障礙補救教學進階研習

untitled

見 例 年 例 利 不 料 林 不 立 理 不 念 類 理 利 了 路 來 行 來 行 立 見不 立 亂 不 理 立 來 2

untitled

untitled

小兒過敏之中醫照護

untitled

untitled

第一章 緒論

untitled

untitled

untitled

性別主流化簡介

富春國小主題統整課程計畫表 每年級(教學群)至少三個主題

untitled

untitled

untitled

untitled

我最大的一次震驚,來自於不同區域客家人 客家人是邊陲

untitled

untitled

untitled

untitled

中華人民共和國殘疾人保障法(2008年修訂)

專 題 論 述

untitled

健康與食品安全的問卷訪問

地方公共服務績效比較評量之探討—標竿學習策略的觀點

untitled

untitled

untitled

untitled

龍華科技大學

微處理機實習期末專題

untitled

untitled

untitled

電腦組裝訓練

untitled

第五章 實例個案

台南縣全民學區數位學習課程進階班—PhotoImpact 10

untitled

untitled

公立學校教職員成績考核辦法修正草案總說明

untitled

第一章 緒論

國立台灣大學法律學院院史(1928~2000):台大法學教育的回顧〔初稿〕

如何去除食物的農藥

untitled

untitled

untitled

untitled

untitled

PATENT PROSECUTION

中老年人養生保健藥饍

untitled

untitled

untitled

untitled

untitled

untitled

Transcription:

(encapsulation) 例 類 說 類 料 來 料 information hiding 念 (inheritance) 來說 類 類 類 類 類 類 行 利 來 (polymorphism) 不 類 數 不 1

2

3

4

類 類 不 類 不 類 5

6

7

// virtual 不見了 #include <iostream> #include <cstdlib> using namespace std; class CWin // CWin 類 類 protected: char id; int width, height; public: CWin(char i='d',int w=10, int h=10) id=i; width=w; height=h; } void show_area() // 類 show_area() 數 cout << "Window " << id << ", area = " << area() << endl; } virtual int area() // 類 area() 數 return width*height; } void sayhello() cout<<"father!,hello\n"; } class CMiniWin : public CWin public: CMiniWin(char i,int w,int h):cwin(i,w,h)} virtual int area() return (int)(0.8*width*height); } void sayhello() cout<<"father!,hello\n"; } int main(void) CWin *ptr=new CWin('A',70,80); CMiniWin m_win('b',50,60); ptr->show_area(); delete ptr; ptr=&m_win; ptr->show_area(); ptr->sayhello(); system("pause"); return 0; } 8

#include <iostream.h> class CShape public: virtual void display()=0; //------------------------------------------------ class CEllipse : public CShape public: void display() cout << "Ellipse \n"; } //------------------------------------------------ class CCircle : public CEllipse public: void display() cout << "Circle \n"; } //------------------------------------------------ class CTriangle : public CShape public: void display() cout << "Triangle \n"; } //------------------------------------------------ class CRect : public CShape public: void display() cout << "Rectangle \n"; } //------------------------------------------------ class CSquare : public CRect public: void display() cout << "Square \n"; } void main() CShape ashape; //Error 不 立 // 類 CEllipse aellipse; CCircle acircle; CTriangle atriangle; CRect arect; CSquare asquare; CShape* pshape[6] = &ashape, &aellipse, &acircle, &atriangle, &arect, &asquare for (int i=0; i< 6; i++) pshape[i]->display(); CRect *prect=&asquare; prect->display(); } Ellipse Circle Triangle Rectangle Square Square 類 不 什 類 來 了 類 CAnimal CYahoo 類 利 行, 例 roar 類 行 類 ( 類 ) 了 roar 9

類 類 virtual 令 不 Polymorphism C++ Polymorphism #include <string.h> class CEmployee // private: char m_name[30]; public: CEmployee(); CEmployee(const char* nm) strcpy(m_name, nm); } virtual float computepay(); //----------------------------------------------- // class CWage : public CEmployee private : float m_wage; float m_hours; public : CWage(const char* nm) : CEmployee(nm) m_wage = 250.0; m_hours = 40.0; } void setwage(float wg) m_wage = wg; } void sethours(float hrs) m_hours = hrs; } virtual float computepay(); //------------------------------------------------- // class CSales : public CWage private : float m_comm; float m_sale; public : CSales(const char* nm) : CWage(nm) m_comm = m_sale = 0.0; } void setcommission(float comm) m_comm = comm; } void setsales(float sale) m_sale = sale; } virtualfloat computepay(); // 理 class CManager : public CEmployee private : float m_salary; public : CManager(const char* nm) : CEmployee(nm) m_salary = 15000.0; } void setsalary(float salary) m_salary = salary; } virtual float computepay(); 10

pemp 理 pemp->computepay 理 pemp pemp->computepay 數 參數 例 polyphmfun(cempolyee * pemp) pemp->computepay(); } CSales asales( 倫 "); CManager amanager( 劉 "); polymphmfun(&sales); polymphmfun(&amanager); 11

class CShape public: virtual void display() = 0; // "= 0" 不 了 類 了 類 類 不 instantiate 說 不 說 狀 'Shape' CShape obj1; 來 error : illegal attempt to instantiate abstract class. 類 abstract Class 類 不 不 "=0" 說 類 abstract Class 類 concrete class) 類 不 類 便 類 類 CCircle 了 CShape CShape CCircle 類 類 12

數 數 數 類, 若 類,, : PureDef.cpp ( ) //---- 類 Shape -------- class Shape private: int i; public: Shape(): i(7)} ~Shape()} virtual void Rotate() =0; virtual void Erase()=0; virtual void Center()=0; //--- 數 Center() ----------- void Shape::Center() cout << "Center point\n";} //---- 類 Circle-------- class Circle : public Shape private: int r; public: Circle(): r(5) } Circle(int N): r(n) } ~Circle() } void Rotate() cout<<" \n";} void Erase() cout<<" \n";} void Center()Shape::Center();} 13

14

15

CShape *pwin; pwin=&win2; pwin->show_area(); pwin=&win2; pwin->show_area(); 16

17

18

CShape *pwin; pwin=&win2; pwin->show_area(); pwin=&win2; pwin->show_area(); pwin=&win2; pwin->show_area(); 19

20

21

22

數 virtual destructor (dynamic memory allocation) 數 數 (virtual destructor) 利 數 行 類 類 數 類 數 數 數不 23