Solved

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

Posted on 1999-01-25
6
458 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Accepted Solution

by:
Seeker092397 earned 50 total points
Comment Utility
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
Comment Utility
>>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
Comment Utility
Thank you seeker.

You got rid of my ambiguity.

Thanks again.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now