C++ : overload and override .. follow up

Posted on 2003-10-26
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.


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

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

void main()
      Base objBase;
      Derived objDerived;

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.


 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 ......
Question by:meow00
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 15

Accepted Solution

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.

Assisted Solution

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


The previous question is the following.


Assisted Solution

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)..

Author Comment

ID: 9627047

    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.

LVL 16

Assisted Solution

imladris earned 75 total points
ID: 9627697
No. Overriding non-virtual functions is done at compile time; i.e. "early-binding".

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

733 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