Solved

C++ : overload and override .. follow up

Posted on 2003-10-26
6
296 Views
Last Modified: 2013-11-25
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 ......
0
Comment
Question by:meow00
6 Comments
 
LVL 15

Accepted Solution

by:
jimmack earned 75 total points
ID: 9622884
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
 
LVL 9

Assisted Solution

by:tinchos
tinchos earned 75 total points
ID: 9623137
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
 
LVL 3

Assisted Solution

by:yasser_helmy
yasser_helmy earned 75 total points
ID: 9625731
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
 
LVL 1

Author Comment

by:meow00
ID: 9627047
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
 
LVL 16

Assisted Solution

by:imladris
imladris earned 75 total points
ID: 9627697
No. Overriding non-virtual functions is done at compile time; i.e. "early-binding".
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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

947 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

21 Experts available now in Live!

Get 1:1 Help Now