C++ : overload and override .. follow up

Posted on 2003-10-26
Medium Priority
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 300 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 300 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 300 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 300 total points
ID: 9627697
No. Overriding non-virtual functions is done at compile time; i.e. "early-binding".

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

777 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