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);
Here are some basic differences:
An overloaded function is a function that shares its name with one or more other functions, but which has a different parameter list. The compiler chooses which function is desired based upon the arguments used.
An overridden function is a method in a descendant class that has a different definition than a virtual function in an ancestor class. The compiler chooses which function is desired based upon the type of the object being used to call the function.
A redefined function is a method in a descendant class that has a different definition than a non-virtual function in an ancestor class. Don't do this. Since the method is not virtual, the compiler chooses which function to call based upon the static type of the object reference rather than the actual type of the object.
Static type checking means that type checking occurs at compile time. No type information is used at runtime in that case.
Dynamic type checking occurs when type information is used at runtime. C++ uses a mechanism called RTTI (runtime type information) to implement this. The most common example where RTTI is used is the dynamic_cast operator which allows downcasting of polymorphic types: