As I'm reading various books and online resources I'm seeing different terms used to describe what I think are the same thing, but I can't quite be sure due to my inexperience.
Thanks in advance for reading this, and my apologies if this is too long.
I was recently reading C++ Programming: Visual QuickStart Guide, by Larry Ullman and Andreas Signer when I came across this:
I tinkered with the code and tried to figure it out, and made a posting on Larry's forums asking for clarification where he shared this StackOverflow link.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.
From that link, I took away that I had the following understanding:
- 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.
I kept getting hung up on his use of the term overloaded in the quote above, particularly in the context of a derived class function hiding a base class function, so I want to make sure that I have a solid grasp of these terms before proceeding further:
My understanding of overloaded methods/functions is what C++ Pocket Reference sums up so well on page 104:
If I'm correct, a base class and derived class are separate scopes, so overloading will not occur between the two without explicitly "pulling them in" via a using statement.Overloading allows you to provide more than one definition for a function within the same scope.
Bjarne Stroustrup had this to say:
While I waited for Larry or another forum goer to find my question where I asked for clarification, I went back to chapter 9, Inheritance and Polymorphism from Ivor Horton's Beginning Visual C++ 2008 and finally arrived at this statement on page 539:In C++, there is no overloading across scopes - derived class scopes are not an exception to this general rule.
Ivor Horton said replaced, and not overridden, but I am thinking he was referring to the same thing:
In Thinking in C++, 2e, Volume 1 (pages 632:646), Bruce Eckel had something similar to say, but he did specifically say overriding:The class also contains the function ShowVolume(), which displays the volume of objects of derived classes. Because this is declared as virtual, it can be replaced in a derived class, but if it isnít, the base class version that you see here is called.
Follow that up with page 130 from C++ Programming in Easy Steps:The redefinition of a virtual function in a derived class is usually called overriding
The C++ FAQ Lite entry, "Should a derived class redefine ("override") a member function that is non-virtual in a base class?", recommends against redefining (overriding) a member function that is non-virtual in a base class.A method can be declared in a derived class to override a matching method in the base class - if both method declarations have the same name, arguments and return type.
Page 130 from C++ Programming in Easy Steps also mentions:
C++ Pocket Reference, 1e (page 93) has this to say:The technique of overriding base class methods must be used with caution however, to avoid unintentionally hiding overloaded methods - a single overriding method in a derived class will hide all overloaded methods of that name in the base class.
Thinking in C++, 2e, Volume 1 (pages 632:646) mentioned this:The parameter list and return type for the member function in the derived class must match those of the member function in the base class. Otherwise, the member function of the derived class hides the member function in the base class, and no polymorphic behavior will occur.
If you override one of the overloaded member functions in the base class, the other overloaded versions become hidden in the derived class.
- redefining, overriding and replacing a function occurs when you have a derived class function with the same function signature as a base class function.
- Doing so hides all of the other base-class versions of that function.
- It is not recommended to do this with non-virtual base class functions.