C++: Polymorphism ( 多态 )
2011-06-28 12:15
176 查看
● Meaning “many forms”, polymorphism in OO terminology means a single statement in code
that might do different things depending on the context in which it is
executed.
○ In this
case, rpt[i]->Display() is a polymorphic call (when Display() is a virtual function).
○ Depending
on what kind of object we send in at runtime, that line of code can either
Report’s Display() method, GradeReport’s Display() method, or OnlineGradeReport’s
Display() method.
● One of the great advantages from polymorphism is that old (existing)
code can can call new (being developed) code without modification.
○ If we
created another class in the Report hierarchy (as a subclass of Report or of
any subclass of Report), then we can write a new display() method for it, and ShowReport()
will call the new Display() method without modfication.
that might do different things depending on the context in which it is
executed.
○ In this
case, rpt[i]->Display() is a polymorphic call (when Display() is a virtual function).
○ Depending
on what kind of object we send in at runtime, that line of code can either
Report’s Display() method, GradeReport’s Display() method, or OnlineGradeReport’s
Display() method.
● One of the great advantages from polymorphism is that old (existing)
code can can call new (being developed) code without modification.
○ If we
created another class in the Report hierarchy (as a subclass of Report or of
any subclass of Report), then we can write a new display() method for it, and ShowReport()
will call the new Display() method without modfication.
#include <iostream> #include <string> using namespace std; #define DEBUGBUILD 0 #define DEBUG if (DEBUGBUILD) cout class Date{ private: int day; int month; int year; public: Date() { DEBUG << "Date: default ctor" << endl; } Date(int m, int d, int y) { DEBUG << "Date: 3 int ctor" << endl; day=d; month=m; year=y; } void Display(void) { cout << month << "/" << day << "/" << year << endl; } }; // end class Date class Time { private: int hour; int minute; int second; public: Time() { DEBUG << "Time: default ctor" << endl; } Time(int h, int m, int s) { DEBUG << "Time: 3 int ctor" << endl; hour=h; minute=m; second=s; } void Display() { cout << hour << ":" << minute << ":" << second << endl; } }; class Report { private: Date repDate; Time repTime; string repDesc; public: Report() : repDate(1,1,1970), repTime(0,0,0), repDesc("") { DEBUG << "Report: default ctor" << endl; repDesc = ""; } Report(Date d, Time t, string desc) : repDate(d), repTime(t){ DEBUG << "Report: date time string ctor" << endl; repDesc = desc; } virtual void Display() { cout << "Date: "; repDate.Display(); cout << "Time: "; repTime.Display(); cout << "Description: "; DisplayDesc(); } void DisplayDesc() { cout << repDesc << endl; } }; // end class Report class GradeReport : public Report { private: int StudentID; int CourseID; public: GradeReport() : StudentID(0), CourseID(0) { DEBUG << "GradeReport: default ctor" << endl; } GradeReport(Date d, Time t, string desc, int sid, int cid) : Report(d, t, desc), StudentID(sid), CourseID(cid) { DEBUG << "GradeReport: 5 parameters ctor" << endl; } GradeReport(const GradeReport& gr) : Report(gr), StudentID(gr.StudentID), CourseID(gr.CourseID) { DEBUG << "GradeReport: Copy ctor" << endl; } ~GradeReport() { DEBUG << "GradeReport: dtor" << endl; } virtual void Display() { DisplayID(); cout << "Course ID: " << CourseID << endl; Report::Display(); } void DisplayID() { cout << "Student ID: " << StudentID << endl; } }; // end class GradeReport class OnlineGradeReport : public GradeReport { public: enum ReportStatus {LOGGED, INFORMED, CONFIRMED}; private: string program; string deptDesc; ReportStatus repStatus; public: OnlineGradeReport() : program(""), deptDesc(""), repStatus(LOGGED) { DEBUG << "OnlineGradeReport: default ctor" << endl; } OnlineGradeReport(Date d, Time t, string desc, int sid, int cid, string pro, string dept, ReportStatus s) : GradeReport(d, t, desc, sid, cid), program(pro), deptDesc(dept), repStatus(s) { DEBUG << "OnlineGradeReport: 8 parameters ctor" << endl; } OnlineGradeReport(const OnlineGradeReport& or) : GradeReport(or), program(or.program), deptDesc(or.deptDesc), repStatus(or.repStatus) { DEBUG << "OnlineGradeReport: Copy ctor" << endl; } ~OnlineGradeReport() { DEBUG << "OnlineGradeReport: dtor" << endl; } virtual void Display() { DisplayStatus(); cout << "Program: " << program << endl; cout << "Department: " << deptDesc << endl; GradeReport::Display(); } void DisplayStatus() { switch(repStatus) { case LOGGED: cout << "The grade is logged by the professor." << endl; break; case INFORMED: cout << "The Records Office has informed the student of the grade." << endl; break; case CONFIRMED: cout << "The student has confirmed receiving the grade report." << endl; break; } // end switch } // end fun }; // end class void main() { Date today(6, 4, 2011); Time t(8, 0, 0); GradeReport gradeA(today, t, "CS360", 1234, 100); Date today2(7, 4, 2011); Time t2(18, 0, 0); Report status(today2, t2, "July Report"); Date today3(8, 4, 2011); Time t3(26, 5, 0); OnlineGradeReport or = OnlineGradeReport(today3, t3, "CS350", 1234, 100, "MSCS", "Computer Science", OnlineGradeReport::ReportStatus::CONFIRMED); Report *rpt[3]; rpt[0] = &gradeA; rpt[1] = &status; rpt[2] = ∨ for(int i = 0; i < 3; i++) { rpt[i]->Display(); cout << endl; } }
相关文章推荐
- C++ 面向对象(四)—— 多态 (Polymorphism)
- C++多态基础(polymorphism)
- c++多态 polymorphism
- java c++ 实现多态(polymorphism)的机制
- C++ 面向对象(四)—— 多态 (Polymorphism)
- 学习笔记之深入浅出MFC 第8章 C++重要性质----虚拟函数与多态(Polymorphism)
- C++ 多态(polymorphism)的例子
- c++面向对象学习(二)编译时多态
- C++和java多态的区别
- C++多态与虚函数
- Java与C++中多态的实现方法,并举例实现
- Polymorphism(多态)
- C++中的多态
- 学习C++中的多态
- C++学习------包含多态的例子
- C++ 类的多态(方法的重载与继承)
- [置顶] Java面向对向编程之多态(polymorphism)
- C++中类的多态与虚函数的使用
- C++多态为什么要定义虚析构函数
- 关于C++多态