C++: Pure Virtual Functions

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, month, year; 11 public: 12     Date(){ DEBUG << "Date: default ctor" << endl;} 13     Date(int m, int d, int y) { 14         DEBUG << "Date: 3 int ctor" << endl; 15         day=d; 16         month=m; 17         year=y; 18     } 19     void Display(void) { cout << month << "/" << day << "/" << year << endl;} 20 }; 21  22 class Time { 23 private: 24     int hour, minute, second; 25 public: 26     Time(){    DEBUG << "Time: default ctor" << endl;} 27     Time(int h, int m, int s)    { 28         DEBUG << "Time: 3 int ctor" << endl; 29         hour=h; 30         minute=m; 31         second=s; 32     } 33     void Display(){    cout << hour << ":" << minute << ":" << second << endl; } 34 }; 35  36 class Document { // abstract class 37 public: 38     virtual void Display() = 0; // pure virtual function 39 }; 40  41 class Report : public Document { 42 private: 43     Date repDate; 44     Time repTime; 45     string repDesc; 46 public: 47     Report() : repDate(1,1,1970), repTime(0,0,0), repDesc("") 48     { 49         DEBUG << "Report: default ctor" << endl; 50         repDesc = ""; 51     } 52     Report(Date d, Time t, string desc) : repDate(d), repTime(t) 53     { 54         DEBUG << "Report: date time string ctor" << endl; 55         repDesc = desc; 56     } 57     virtual void Display() 58     { 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     virtual void Display() { 88         DisplayID(); 89         cout << "Course ID: " << CourseID << endl; 90         Report::Display(); 91     } 92     void DisplayID() { cout << "Student ID: " << StudentID << endl; } 93 }; 94  95 class Counselor : public Document { 96 private: 97     Date counseledDate; 98     string counselorName; 99     GradeReport* badGrade;100 public:101     Counselor() { DEBUG << "Counselor: default ctor" << endl; }102 103     ~Counselor() { DEBUG << "Counselor: dtor" << endl;    }104 105     Counselor(const Counselor &cr) : counseledDate(cr.counseledDate), 106         badGrade(cr.badGrade),107         counselorName(cr.counselorName)108     { DEBUG << "Counselor: copy ctor" << endl; }109 110     Counselor(Date d, string name, GradeReport *gptr) : counseledDate(d), badGrade(gptr),111         counselorName(name)112     {     DEBUG << "Counselor: Date, string, GradeReport* ctor" << endl; }113 114     virtual void Display() {115         cout << "Student ";116         badGrade->DisplayID();117         cout << "Counseled on: ";118         counseledDate.Display();119         cout << "Counselor's name: " << counselorName << endl;120     }121 };122 123 void ShowReport(Document* doc){124     cout << "*********************************" << endl;125     doc->Display();126     cout << "*********************************" << endl;127 }128 129 void main()130 {131     Date today(6, 4, 2011);132     Time t(8, 0, 0);133     GradeReport gradeA(today, t, "CS360", 1234, 100);134 135     Date today2(7, 4, 2011);136     Time t2(18, 0, 0);137     Report status(today2, t2, "July Report");138 139     Date counseled(10, 18, 2011);140     Counselor helper(counseled, "Paul Davis", &gradeA);141 142     Document *doc[3];143     doc[0] = &gradeA;144     doc[1] = &status;145     doc[2] = &helper;146 147     for(int i = 0; i < 3; i++)148         ShowReport(doc[i]);149 }


原文链接: https://www.cnblogs.com/ashu/archive/2011/06/28/2091952.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/27772

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午5:22
下一篇 2023年2月8日 上午5:22

相关推荐