我有一個叫做course的類。課程有一個指向名爲Assessment的基類的指針。C++多態/純虛函數
class Course{
char* courseName;
float fee;
public:
Assessment* assessment;
Course();
Course(Course&);
Course(char*, float, Assessment *);
~Course();
friend ostream& operator <<(ostream& os, Course&);
};
Strickly用於測試目的,在構造函數中我指定的評估指針的ExamAssessment(子類)
Course::Course(){
courseName = "something";
fee = 0;
assessment = &ExamAssessment(45);
}
我的司機的代碼如下所示
Course t = Course();
cout << *(t.assessment);
在評估中,報告是純虛擬的
virtual void Report() = 0;
問題是,當我清點評估,在評估重載運算符「< <」,我打電話報告()
ostream& operator<<(ostream& os, Assessment& assessment){
assessment.Report();
os << "Course Grade: "<< assessment.grade << endl;
return os;
}
但報告呼籲「評估」報告,而不是ExamAssessments報告。不知道在哪裏,我本打算錯誤的(我是相當新的C++繼承)
你怎麼知道這是調用'評估'報告? – wimh
編譯器告訴我'純虛函數被稱爲' –