Jump to content
Larry Ullman's Book Forums

Chapter 8, Pg 260, Overriding And Overloading Methods


Recommended Posts

First, thanks to both of you for writing the book and for how well the content is explained. However I'm having trouble understanding this statement on page 260:

 

Pay attention when overriding methods. If you don't use the exactly same parameters and return values, you'll end up with an overloaded method, and not an overridden one. Such mistakes are very hard to debug!

 

From what I can find via Google searches and tinkering with the pets4.cpp code, having a method in the derived class with the same name (different signature or not) as one in the base class hides the base class method, much like overriding a method seems to.

 

Can you maybe explain that in a different way so it makes more sense to me?

 

Thanks in advance!

Link to comment
Share on other sites

Let me know if it's still not clear.

 

Thanks for the reply Larry. I understand what was said on the thread about:

  • A derived class function with a different signature than a base class function will result in the base class function of the same name being hidden.
  • A derived class function with the same name and signature as the base class will override the base class function.

This is the part I've gotten fixated on:

 

you'll end up with an overloaded method

 

Please correct me, but with an overloaded function, wouldn't you generally use one of the functions and depending on what function signature you use, that determines what function gets called? In this case:

 

sleep(int hours)

 

 

since the base class sleep function is hidden, don't you end up with just one function available to the dog class? At least running the pets4.cpp file through a compiler with this new function:

 

void Dog::sleep(int hours)
{
std::cout << name << " sleeps " << hours << " hours";
}

 

and calling it like so:

 

dog.sleep();

 

results in this error:

 

Error 1 error C2660: 'Dog::sleep' : function does not take 0 arguments ch8\pets4\pets4.cpp 121
Link to comment
Share on other sites

Thanks for the reply. I took the provided pets4.cpp file for the book (peachpit website) and made the changes mentioned on page 260.

Note: I forgot to change tabs to spaces or vice verse for consistency.

 

Here is an inline diff if it's helpful:

 

--- pets4.orig.cpp	2008-04-12 15:41:08 -0500
+++ pets4.cpp	2012-02-27 06:34:06 -0600
@@ -38,6 +38,7 @@

 // Overloaded method:
 void bark(int count);
+	void sleep(int hours);

};

@@ -91,6 +92,11 @@
void Dog::bark(int count) {
 std::cout << name << " barks " << count << " times\n";
}
+
+void Dog::sleep(int hours)
+{
+	std::cout << name << " sleeps " << hours << " hours";
+}

// Start the main() function.
int main() {
@@ -104,8 +110,8 @@
 cat.eat();
 cat.climb();
 cat.play();
-		
-	dog.sleep();
+	
+	dog.sleep();
 dog.eat();
 dog.bark();
 dog.play();

Link to comment
Share on other sites

Please correct me, but with an overloaded function, wouldn't you generally use one of the functions and depending on what function signature you use, that determines what function gets called?

 

Larry et al,

 

At the time, I believe that made sense to me, but now it reads pretty poorly.

 

It was the use of the word overloaded in this text that I got hung up on:

 

Pay attention when overriding methods. If you don't use the exactly same parameters and return values, you'll end up with an overloaded method, and not an overridden one.

 

I've done more reading, and my understanding of overloaded methods/functions is what C++ Pocket Reference sums up so well on page 104:

 

Overloading allows you to provide more than one definition for a function within the same scope.

 

 

If I'm correct in my understanding, the base class and derived classes are separate scopes, so overloading will not occur.

 

Bjarne Stroustrup had this to say:

 

In C++, there is no overloading across scopes - derived class scopes are not an exception to this general rule.

 

Would the following be an equivalent to what you were saying?

 

Pay attention when overriding a method. If you don't use the exactly same parameters and return values, you'll end up hiding the base class method, and not overriding it.

 

Thanks.

Link to comment
Share on other sites

Sorry for the confusion and for the delay. I wanted to go back and look at my original notes to make sure I was telling you here what I meant to say in the book.

 

Your quote (which I assume is your rewrite of what I wrote) is equivalent to my intention, although I would merge that quote with my own, because the point I'm trying to make is that you can end up in a confusing situation, as you mention in hiding the base class method. In other words, if you *intend* to override method a(), but inadvertently overload the method, in both cases, the original definition of a() in the base class is no longer acceptable, but in the latter case, it's usage will have changed, too, which can be confusing. That's the point I was trying to make, I believe.

 

Does that make more sense?

Link to comment
Share on other sites

Sorry for the confusion and for the delay. I wanted to go back and look at my original notes to make sure I was telling you here what I meant to say in the book.

 

Thanks for the reply. Regarding the delay, you're a busy guy, and you're providing free support here on your own site so practicing patience won't hurt me.

 

 

Your quote (which I assume is your rewrite of what I wrote)

 

Yeah, I was trying to find another way to say it without using the word overload. Whenever I read that word, I think of having multiple functions with the same name in the same scope, so I find other uses of the word confusing. I'm not sure why I'm being dense about this, but I find this confusing too:

 

 

In other words, if you *intend* to override method a(), but inadvertently overload the method

 

Would the following be equivalent to what you wrote? I know it's not as well written, but I'm hoping to find an equivalent statement so I can get past this mental block. I really appreciate the help.

 

In other words, if you *intend* to override method a(), but inadvertently change the signature on the method you wanted to override method a() with
Link to comment
Share on other sites

Thanks for the patience and understanding. Much appreciated. As for your rewrite, I'm not quite following it. Perhaps you're overthinking all this?

 

All I'm really trying to say in that tip is that if you're trying to override a method, make sure that the signature is exactly the same or else things can get funky.

 

How's that for the same sentiment without using the other "O" word?

  • Upvote 1
Link to comment
Share on other sites

Perhaps you're overthinking all this?

 

I think you're right. I just need to move on and come back (much) later if it's still not clear.

 

All I'm really trying to say in that tip is that if you're trying to override a method, make sure that the signature is exactly the same or else things can get funky.

 

lol, I think that will work.

 

How's that for the same sentiment without using the other "O" word?

 

Thanks Larry, much appreciated.

 

On a completely unrelated note, I started off my serious programming efforts years ago with the 1E of your "PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide" book; I still have it on the shelf at work. :)

 

May you write many more!

Link to comment
Share on other sites

  • 3 years later...

Static methods can be overloaded, that means a class can have more than one static method of same name. But static methods cannot be overridden, even if you declare a same static method in derived class it has nothing to do with the same method of base class......Source

 

Dov

Link to comment
Share on other sites

 Share

×
×
  • Create New...