I don't understand this Example

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 
Last edited on
The wording seems a bit off, but the point is that if r is an object of some class, and you call a class function of that class, then the parameters must match the function prototype.

In the wording, it makes sense if the following change is made:

This second call is an error even if r refers to an object of type GradCore. The type of r is a
reference to Core, and the version of regrade in Core takes one value of type double.


and this change as well (because it's not at all surprising):

What
may be more surprising
This is what happens if r is a reference to a Grad:


Which book is this?
Last edited on
Is Grad derived from Core??

What book is it by the way??

This second call is an error even if r refers to an object of type Grad.


I don't get this part because if r is of type Grad, then the second call should have worked.

Yes, Grad is derived from Core. And the book is Accelerated C++ 2000
Last edited on
Core is a class for undergraduates, it has the data members: vector<int> homework, int midterm, int final, string name.

The Grad class is for graduates, it is derived from Core but adds one data member which is: int thesis.
Yes, Grad is derived from Core.


Oh. Well then, forget I spoke :)

Is there more to these classes; are these class functions public or private, for example?
Last edited on
eNergizer wrote:
Yes, Grad is derived from Core.

In which case the issue you seeing is Name Hiding - your book should mention it somewhere.
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.



I don't get this part because it says later that it should work:

What may be more surprising This 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  

Thanks for clearing it up Moschops, I guess my book just said it wrongly.
I pass all credit to the guestgulkan :)
Topic archived. No new replies allowed.