Link to home
Start Free TrialLog in
Avatar of meow00
meow00

asked on

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 ......
ASKER CERTIFIED SOLUTION
Avatar of jimmack
jimmack

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of meow00
meow00

ASKER

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.....
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial