You are here:

C++/Difference between Overriding and Overloading

Advertisement


Question
Eddie,

Thank you so much for a fast and detailed response. It helped me .

I have one more doubt. It may sound dumb to you ,but would appreciate if you would answer.

In the overriding example you made the DoSomething function as Virtual
so that the class variable can figure out which version of the method to call at runtime.

But If I dont make the base class function to be virtual , and use the scope resolution operator to call the function I need will that help ?

for eg. If I want to call the Base class function I will call

Base::DoSomething();

for Derived class function I will call
Derived ::DoSomething();

Is this way correct?


I understood from explanation that overiding means inheriting the function ( eg from base to derived ) and use the function per the functionality we require in the derived class while as overloading is using the same function name for 'n' different functions  with different no of parameters or diffrent type of parameters in same class for performing the required functionality. Please correct if I am wrong in understanding.

Thanks
BK



-------------------------
Followup To
Question -
Can you please explain with an example the diffrence between overriding and over loading in C++ ?

Answer -
Hello BK, thank you for the question.

In C++, overriding is a concept used in inheritance which involves a base class implementation of a method. Then in a subclass, you would make another implementation of the method. This is overriding. Here is a simple example.

class Base
{
public:
virtual void DoSomething() {x = x + 5;}
private:
int x;
};
class Derived : public Base
{
public:
virtual void DoSomething() { y = y + 5; Base::DoSomething(); }
private:
int y;
};

Here you can see that the derived class overrides the base class method DoSomething to have its own implementation where it adds to its variable, and then invokes the parent version of the function by calling Base::DoSomething() so that the x variable gets incremented as well. The virtual keyword is used to that the class variable can figure out which version of the method to call at runtime.

Overloading is when you make multiple versions of a function. The compiler figures out which function to call by either 1) The different parameters the function takes, or 2) the return type of the function. If you use the same function declaration, then you will get a compiler error because it will not know which function to use. Here is another simple example.

class SomeClass
{
public:
void SomeFunction(int &x) { x *= x; }
int SomeFunction(int x) { return x * x; }
};

// In main()
SomeClass s;
int x = 5;
x = SomeFunction(x);

The compiler knows to call the second implementation of the method because we are assigning the return value from the function to x.

If you need further explantion, please do not hesitate to ask.

I hope this information was helpful.

- Eddie

Answer
Yes, your thinking is correct. Think of it like this: Overriding, say you have a base class called WorldObject. It has a method called Update, which is called once per frame. You put all of the base code all WorldObjects would need to do in the update like collision testing, orienting, etc. Then you derive 2 classes from it. Player, and Computer. Then you make each of those classes have an Update function. The Computer Update function performs AI, and the Player Update processes keyboard input. At the end of each, you call the base class Update. Easy enough? Overloading is exactly what you said. As the far as the code example you gave, this is slightly off. You can only invoke a static function (those functions do not have an invoking object (ie a this pointer) by doing Class::Function().

Lets say you did this:

class Base
{
public:
void SomeFunction() {}
};

class Derived : public Base
{
void SomeFunction() {}
};

// In main
Base *baseptr = new Derived;
baseptr->SomeFunction();

This is not going to be correct because the function is not virtual. It is going to call the Base class method instead of the derived. However, if the function was declared virtual, then it would have no problems.

If you need any further help, please do not hesitate to ask.

I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.