Solved

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

Posted on 1999-01-25
6
475 Views
Last Modified: 2010-04-16
What's the difference between "override" and "overload"?
0
Comment
Question by:jhoon
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 clear a vector as well as how to detect empty vectors in C++.

828 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