My book gave me this example and I don't understand the wording of it at all. Any help on this?
It is important to realize that when a base- and derived-class function have the same name,
but they don't match exactly in number and types of parameters, they behave as if they were
completely unrelated functions. For example, we might add to our hierarchy an accessor
function that we could use to change a student's final exam-grade. For Core students, this
function should set only the final grade; for Grad students, the function should take two
parameters, the second one being used to set the thesis:
1 2
|
void Core::regrade(double d) { final = d; }
void Grad::regrade(double d1, double d2) { final = d1; thesis = d2; }
|
If r is a reference to a Core, then
1 2
|
r.regrade(100); // ok, call Core::regrade
r.regrade(100, 100); // compile error, Core::regrade takes a single argument
|
This second call is an error even if r refers to an object of type Grad. The type of r is a
reference to Core, and the version of regrade in Core takes one value of type double. What
may be more surprising is what happens if r is a reference to a Grad:
1 2
|
r.regrade(100); // compile error, Grad::regrade takes two arguments
r.regrade(100, 100); // ok, call Grad::regrade
|