C++ : overload and override .. follow up

Hi C++ Experts :

  Sorry ... I thought I understood it ... but when I thought it again .... I got confused .....

  Based on Tincho's and Mustak's answer, I have a few questions :
-----------------------------------------------------------

In this particular code, you would say that Derived::read_a function does not override Base::read_a because read_a is not a virtual function as you said.

In fact, you would say that it hides Base::read_a.

Conceptually you would say override you mean "the read_a function that in the base class does this thing, in this one does this other thing".

In this case you mean something like "the read_a function in the base class does this thing, and the read_a function in the derived class does this other thing". There is no link between both functions, they could be considered completely different functions, but you can only access the one implemented in the derived class, as when the compiler looks for the method to run, it first look in the methods implemented in the derived class, and as it finds it there, it will never go to look for it in the function virtual table, so it will never be able to call the other one.

I hope I've been clear enoughj, otherwise, let me know and I'll try to explain it a little more.

Tincho
------------------------------------------------------------
meow,

definitly this is a function overriding.
let me prove it.
class Base{
  public:
    Base(){}
    ~Base(){}
    int read_a(){
             cout<<"Base read_a executed"<<endl;
             return 1;
    }
  private:
    int a ;
};

class Derived:public Base{
   public:
     Derived(){}
     ~Derived(){}
     //float read_a(){}   comment it for time being
   private:
     float a ;
} ;

void main()
{
      Base objBase;
      Derived objDerived;
     objBase.read_a();
     objDerived.read_a();
}

when you execute this code, for obvious reason read_a() function
will get executed twice.
now uncomment read_a() in derived class as below
float read_a()
      {
           cout<<"Derived read_a executed"<<endl;
           return 1;
      }
but this time read_a function from Base and Derived get executed,
which mean read_a() has been overridden.

Now coming to the second part of your problem
when you make this function as a virtual one, it will give you following
error at compile time:
'Derived::read_a' : overriding virtual function differs from 'Base::read_a' only by return type or calling convention

in this scenarion it is doing strict datatype checking.
so in case of virtual function your function signature should match completely.

Regards,
Mustak_Shaikh
-------------------------------

 1. How is "overriding" defined ? Do they "have to" be
     virtual functions ? can we override a non-virtual
     function ? (i.e. if both base and derived class have
     "int read_a()" today, and the d.read_a() executes
     the one in  the derived class ? is this  a
     "overriding" or just a " function hidden" ?)

     Thanks a lot !

meow ......
LVL 1
meow00Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimmackCommented:
I missed the question to which your are following up, so forgive me if I'm repeating something you've already been told.

Overriding is when you change or extend the method of a superclass.  It doesn't really matter if the subclass method completely replaces the superclass method or not (ie. "hides" the method).  The fact that the method has the same name means that the functionality has been overridden.

Overloading is when you have multiple methods in a class with the same name that do different things, based on the method signature.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tinchosCommented:
Hi meow........

As far as I know, I have always refered to overriding only to virtual functions, and that's the reason for my answer.

I still believe that if there's no virtual involved there will not be an overriding involved.

But I realize that maybe it was something I was misleaded to believe.

This is what I suggest.........

My believe is still the same.

If I were you I would post this question with the link I'm including to the original question inside the C++ area (we're talking about C++), and I believe that this question will get more opinions from experts and this will then lead you to the right answer.

Hope this helps

Tincho

The previous question is the following.

http://www.experts-exchange.com/Programming/Q_20778125.html#9621989

0
yasser_helmyCommented:
Overridding is telling the compiler to use the derived class function instead of the one in the base class.. the function in the base class still exists and objects of base class (and other derived classes) can use it.. you only override the derived class version of the function..
>>Do they "have to" be virtual functions ?
No, they don't.. virtual means that the derived class "must" override the function.. but you can override any other function if you wish (but not a must)..
0
meow00Author Commented:
Hi,

    We know the overriding in the virtual function is "late-binding".
Then if the overriding is not a virtual function, is it still a "late-binding" ? or it decides to use the function in the derived class in the compile time ? Thanks.

meow.....
0
imladrisCommented:
No. Overriding non-virtual functions is done at compile time; i.e. "early-binding".
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.