I am confused about the differences between redefining and overriding functions in derived classes.
I know that - In C++, redefined functions are statically bound and overridden functions are dynamically bound and that a a virtual function is overridden, and a non-virtual function is redefined.
When a derived class "redefines" a method in a base class its considered redefining. But when a derived class is virtual it no longer redefines but rather overrides. So i understand the logistics of the rules but i don't understand the bottom line.
In the below example the function SetScore is redefined. However if I make the setScore function in the base class virtual (by adding the word virtual to it) the setScore in the derived class will be overridden. I don't understand the bottom line - what is the difference. in setScore?
The base class:
class GradedActivity
{
protected:
char letter; // To hold the letter grade
double score; // To hold the numeric score
void determineGrade(); // Determines the letter grade
public:
// Default constructor
GradedActivity()
{ letter = ' '; score = 0.0; }
// Mutator function
void setScore(double s)
{ score = s;
determineGrade();}
// Accessor functions
double getScore() const
{ return score; }
char getLetterGrade() const
{ return letter; }
};
The derived class:
class CurvedActivity : public GradedActivity
{
protected:
double rawScore; // Unadjusted score
double percentage; // Curve percentage
public:
// Default constructor
CurvedActivity() : GradedActivity()
{ rawScore = 0.0; percentage = 0.0; }
// Mutator functions
void setScore(double s)
{ rawScore = s;
GradedActivity::setScore(rawScore * percentage); }
void setPercentage(double c)
{ percentage = c; }
// Accessor funtions
double getPercentage() const
{ return percentage; }
double getRawScore() const
{ return rawScore; }
};
This is main:
// Define a CurvedActivity object.
CurvedActivity exam;
...
// Send the values to the exam object.
exam.setPercentage(percentage);
exam.setScore(numericScore);