Solved

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

Posted on 1999-01-25
6
463 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

862 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

24 Experts available now in Live!

Get 1:1 Help Now