您的位置:首页 > 编程语言 > C语言/C++

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.

#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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: