面向对象程序设计-第16次课.ppt
文本预览下载声明
2004-12-16 National Model School of Software Technology,YunNan University * lijin Class Hierarchy : Employee Member Function : computePay() How to get Polymorphism? A pointer of a base class A pointer to base class A derived object pointer The same name functions How can we do that? Polymorphism and Virtual Function Polymorphism: the ability of a base class pointer or reference to refer transparently to any of its derived class objects. Polymorphism allows us manipulate objects of these classes in a type-independent manner. Virtual Function supports us to implement the Polymorphism. Conclusions Class hierarchy: num_sequence Defining an Abstract base class Defining an Abstract base class * 单击此处编辑母版标题样式 单击此处编辑母版副标题样式 Instructor: Li Jin Email: bluecube@ School of Software Technology YunNan University 2005.2 ~ 2005.7 Object-Oriented Programming Chapter 6 Object-Oriented Programming Section 6.2 Virtual Function // private data of CEmployee char m_name[30]; // private data of CWage float m_wage; float m_hours; // private data of CSales float m_comm; float m_sale; void setWage(float wg); void setHours(float hrs); void setCommission(float comm); void setSale(float sales); void computePay(); float computePay( return CWage::computePay() + m_comm*m_sale; ); float computePay( return m_salary; ); float computePay( return m_wage*m_hours; ); CEmpolyee* pEmp; While(pEmp = anIter.getnext()){ pEmp-EmployeeName(); pEmp-computePay(); } Polymorphism ? CSales aSales(“SSSSSSSSS); CWage aWager(“WWWWWWWW); CWage* pWager; CSales* pSales; pWager = aSales; cout pWager-computePay() endl; pSales = aSales; cout pSales-computePay() endl; Original type a pointer or reference of a base class only can manipulate the actual base class objects We need Virtual Function ! See the example: Employee.cpp See the example: VEmployee.cpp Section 6.3 Defining an Abstract base class num_sequence Fibonacci Triangular … … Pell Lucas Pentagonal Class
显示全部