C++: Virtual Functions - Static Binding

1 #include <iostream>
2 #include <string>
3 using namespace std;
4
5 #define DEBUGBUILD 0
6 #define DEBUG if (DEBUGBUILD) cout
7
8 class Date {
9 private:
10 int day;
11 int month;
12 int year;
13 public:
14 Date() { DEBUG << "Date: default ctor" << endl; }
15 Date(int m, int d, int y)
16 {
17 DEBUG << "Date: 3 int ctor" << endl;
18 day=d;
19 month=m;
20 year=y;
21 }
22 void Display(void) {
23 cout << month << "/" << day << "/" << year << endl;
24 }
25 };
26
27 class Time {
28 private:
29 int hour;
30 int minute;
31 int second;
32 public:
33 Time() { DEBUG << "Time: default ctor" << endl; }
34 Time(int h, int m, int s) {
35 DEBUG << "Time: 3 int ctor" << endl;
36 hour=h;
37 minute=m;
38 second=s;
39 }
40 void Display() { cout << hour << ":" << minute << ":" << second << endl; }
41 };
42
43 class Report {
44 private:
45 Date repDate;
46 Time repTime;
47 string repDesc;
48 public:
49 Report() : repDate(1,1,1970), repTime(0,0,0), repDesc("") {
50 DEBUG << "Report: default ctor" << endl;
51 repDesc = "";
52 }
53 Report(Date d, Time t, string desc) : repDate(d), repTime(t) {
54 DEBUG << "Report: date time string ctor" << endl;
55 repDesc = desc;
56 }
57
58 void Display() {
59 cout << "Date: ";
60 repDate.Display();
61 cout << "Time: ";
62 repTime.Display();
63 cout << "Description: ";
64 DisplayDesc();
65 }
66 void DisplayDesc() { cout << repDesc << endl; }
67 };
68
69 class GradeReport : public Report {
70 private:
71 int StudentID;
72 int CourseID;
73 public:
74 GradeReport() : StudentID(0), CourseID(0)
75 { DEBUG << "GradeReport: default ctor" << endl; }
76
77 GradeReport(Date d, Time t, string desc, int sid, int cid) :
78 Report(d, t, desc), StudentID(sid), CourseID(cid)
79 { DEBUG << "GradeReport: 5 parameters ctor" << endl; }
80
81 GradeReport(const GradeReport& gr) :
82 Report(gr), StudentID(gr.StudentID), CourseID(gr.CourseID)
83 { DEBUG << "GradeReport: Copy ctor" << endl; }
84
85 ~GradeReport() { DEBUG << "GradeReport: dtor" << endl; }
86
87 void Display() {
88 DisplayID();
89 cout << "Course ID: " << CourseID << endl;
90 Report::Display();
91 }
92
93 void DisplayID() { cout << "Student ID: " << StudentID << endl; }
94 };
95
96 class OnlineGradeReport : public GradeReport {
97 public:
98 enum ReportStatus { LOGGED, INFORMED, CONFIRMED };
99 private:
100 string program;
101 string deptDesc;
102 ReportStatus repStatus;
103 public:
104 OnlineGradeReport() : program(""), deptDesc(""), repStatus(LOGGED)
105 { DEBUG << "OnlineGradeReport: default ctor" << endl; }
106
107 OnlineGradeReport(Date d, Time t, string desc, int sid, int cid, string pro, string dept,
108 ReportStatus s) :
109 GradeReport(d, t, desc, sid, cid), program(pro), deptDesc(dept), repStatus(s)
110 { DEBUG << "OnlineGradeReport: 8 parameters ctor" << endl; }
111
112 OnlineGradeReport(const OnlineGradeReport& or) :
113 GradeReport(or), program(or.program), deptDesc(or.deptDesc), repStatus(or.repStatus)
114 { DEBUG << "OnlineGradeReport: Copy ctor" << endl; }
115
116 ~OnlineGradeReport()
117 { DEBUG << "OnlineGradeReport: dtor" << endl; }
118
119 void Display() {
120 DisplayStatus();
121 cout << "Program: " << program << endl;
122 cout << "Department: " << deptDesc << endl;
123 GradeReport::Display();
124 } // end fun Display()
125
126 void DisplayStatus() {
127 switch(repStatus)
128 {
129 case LOGGED:
130 cout << "The grade is logged by the professor." << endl;
131 break;
132 case INFORMED:
133 cout << "The Records Office has informed the student of the grade." <<
134 endl;
135 break;
136 case CONFIRMED:
137 cout << "The student has confirmed receiving the grade report." << endl;
138 break;
139 } // end switch
140 } // end fun DisplayStatus()
141 };
142
143 void main() {
144 Date today(6, 4, 2011);
145 Time t(8, 0, 0);
146 GradeReport gradeA(today, t, "CS360", 1234, 100);
147
148 Date today2(7, 4, 2011);
149 Time t2(18, 0, 0);
150 Report status(today2, t2, "July Report");
151
152 Date today3(8, 4, 2011);
153 Time t3(26, 5, 0);
154 OnlineGradeReport or = OnlineGradeReport(today3, t3, "CS350", 1234, 100, "MSCS",
155 "Computer Science", OnlineGradeReport::ReportStatus::CONFIRMED);
156
157 Report *rpt[3];
158 rpt[0] = &gradeA;
159 rpt[1] = &status;
160 rpt[2] = &or;
161
162 for(int i = 0; i < 3; i++)
163 rpt[i]->Display();
164 }
● The method that is called is determined at compile time.

● The method that is called is decided by the type of the pointer.

  ○ In main(), rpt is an array of Report pointer so Report’s Display() method will be called.

● This behavior is known as static binding.

posted @ 2011-06-28 12:08  Mayus  阅读(198)  评论(0)    收藏  举报