Solved

What's the difference between "override" and "overload"?

Posted on 1999-01-25
6
492 Views
Last Modified: 2010-04-16
What's the difference between "override" and "overload"?
0
Comment
Question by:jhoon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 1

Expert Comment

by:Seeker092397
ID: 1185072
The difference i think is in following:

Override is about inheritence: Overriden function is the function defined in both base and derived classes in exactly the same way, it lets you change the bahaviour of base class.

Overload is about the number of functions with the same name: This way you can define a number of functions in one class with the same name but different parameter types.

Hope this helps.
Best regards.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1185073
That's it.    Why don't you answer, seeker?

The way I think about it is that a derived class can provide a finction that overides (changes the behaviour) of a base class's function..  And that two functions with the same and different parameters are overloaded with posibilities.  


0
 

Author Comment

by:jhoon
ID: 1185074
Thank you so much.

Now I know "overload" which enable a program to have polymorphism.

but I'm not clear about "override".

Would you tell me more about "override" including a simple sample code?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Accepted Solution

by:
Seeker092397 earned 50 total points
ID: 1185075
Override example:

class A
{
public:
  A()
  {
    m_nVal = 0;
  }
  void Foo()
  {
    m_nVal = 1;
  }

  int m_nVal;
};

class B : public A
{
public:
  B() : A()
  {}
  void Foo()
  {
    m_nVal = 2;
  }
};

Class B overrides function Foo(). So now calling
B b;
b.Foo();
will set m_mVal to 2 instead of 1.

You do overriding only if you need to. I mean, if your B class is pretty happy with the way A handles Foo() it shouldn't override it(shouldn't define Foo in its class definition) and still you will be able to call b.Foo() but in this case m_nVal will get value 1.

Hope this helps.

Best regards.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1185076
>>Now I know "overload" which enable a program to have polymorphism
No, it is "overide" that provides polymorphism.  Consider a class heirarchy for shapes.  You may have a base class called "Shape", add derive three classes from it for "Circle" "Rectangle", and "Triangle".  Shape may provide a virtual procedure called "Draw()" that is supposed to draw the shape.  (But in the shape class the procedure does nothing, since shape is an abstract class and there is no shape associated with it.)  Each of the derived classes OVERRIDE this procedure to draw their own shapes.   This is polymorphism.  The Shape() function has polymorphic behavior--it acts differently for different classes--because it was overidden.
0
 

Author Comment

by:jhoon
ID: 1185077
Thank you seeker.

You got rid of my ambiguity.

Thanks again.
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question