- #1
yungman
- 5,738
- 290
I am studying polymorphism I just don't understand the purpose in this example from the book:
The whole thing started with line 93:
I simply change line 93 to
Why declare virtual function in line 22 and 52 JUST to defeat the normal logical progression of the program(that is wrong)? This is like sticking worms into your behind and then take anti parasite medication to get rid of the worm! I don't know a better way to express how I feel!
C++:
#include <iostream>
#include <iomanip>
using namespace std;
class GradedActivity
{protected: double score; // To hold the numeric score
public:
GradedActivity()
{ score = 0.0;
cout<<"\t GradedActivity, Default Constructor, score = "<<score<<"\n\n";
}
GradedActivity(double s)
{ score = s;
cout << "\t GradedActivity, Constructor1, score = " << score << "\n\n";
}
void setScore(double s)
{ score = s;
cout << "\t GradedActivity setscore, score = " << score << "\n\n";
}
double getScore() const
{ return score;
cout << "\t GradedActivity getscore, score= " << score << "\n\n";}
virtual char getLetterGrade() const//declare virtual
{ char letterGrade; // To hold the letter grade
if (score > 89)letterGrade = 'A';
else if (score > 79) letterGrade = 'B';
else if (score > 69) letterGrade = 'C';
else if (score > 59)letterGrade = 'D';
else letterGrade = 'F';
return letterGrade;
}
};
class PassFailActivity : public GradedActivity
{protected: double minPassingScore; // Minimum passing score.
public:
PassFailActivity() : GradedActivity()
{ minPassingScore = 0.0;
cout<<"\t PassFailActivity Default Con, minPassingScore= "<<minPassingScore<<"\n\n";
}
PassFailActivity(double mps) : GradedActivity()
{ minPassingScore = mps;
cout << "\t PassFailActivity Const1, minPassingScore= "<<minPassingScore<<"\n\n";
}
void setMinPassingScore(double mps)
{ minPassingScore = mps;
cout << "\t PassFailActivity setMinPassingScore, minPassingScroe= "<<minPassingScore<<"\n\n";
}
double getMinPassingScore() const
{ cout<<"\t PassFailActivity getMinPassingScore: "<<minPassingScore<<"\n\n";
return minPassingScore;
}
virtual char getLetterGrade() const//Declare virtual
{ char letterGrade;
if (score >= minPassingScore) letterGrade = 'P';
else letterGrade = 'F';
cout<<"\t PassFailActivity getLetterGrade, letterGrade= "<<letterGrade<<"\n\n";
return letterGrade;
}
};
class PassFailExam : public PassFailActivity
{
private: int numQuestions; double pointsEach; int numMissed;
public: PassFailExam() : PassFailActivity()
{ cout << "\t PassFailExam Default Constructor: \n\n";
numQuestions = 0; pointsEach = 0.0; numMissed = 0;
}
PassFailExam(int questions, int missed, double mps) :PassFailActivity(mps)
{ cout << "\t PassFailExam Constructor 3: \n\n";
set(questions, missed);}
void set(int questions, int missed)
{ double numericScore;
numQuestions = questions;
numMissed = missed;
pointsEach = 100.0 / numQuestions;
numericScore=100.0-(missed*pointsEach);
cout << "\t PassFailExam set, numQuestion= "<<numQuestions<<
", numMissed= "<<numMissed<<", pointsEach= "<<pointsEach<<"\n\n";
setScore(numericScore);
}
double getNumQuestions() const
{ cout << "\t PassFailExam getNumQuestions: " << numQuestions<<"\n\n";
return numQuestions;
}
double getPointsEach() const
{ cout << "\t PassFailExam getPointsEach: " << pointsEach << "\n\n";
return pointsEach;
}
int getNumMissed() const
{ cout << "\t PassFailExam getNumMissed: " << numMissed << "\n\n";
return numMissed;
}
};
void displayGrade(const GradedActivity& activity)//declare activity a GradeActivity object
{ cout<<setprecision(1)<<fixed<<" The activity's numeric score is "<<activity.getScore()<<
"\n The activity's letter grade is "<< activity.getLetterGrade() << "\n\n";}
int main()
{ PassFailActivity test(70);// Create a PassFailActivity object. Minimum passing score is 70.
test.setScore(72); //score=72.
displayGrade(test); return 0;//The letter grade should be 'P'.
}
The whole thing started with line 93:
void displayGrade(const GradedActivity& activity)
that declare object activity of GradedActivity class! Then it complain about it gives you a letter grade A, B, C... Instead of P or F in class PassFailActivity!I simply change line 93 to
void displayGrade(const PassFailActivity& activity)
and I get P or F already.Why declare virtual function in line 22 and 52 JUST to defeat the normal logical progression of the program(that is wrong)? This is like sticking worms into your behind and then take anti parasite medication to get rid of the worm! I don't know a better way to express how I feel!