(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, âle, &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