What does Virtual in the derived class signify?

Hi All,

I have a base class A which is overriddedn by B, C and D. Now, I have a virtual method something() in A. And this has been declared as virtual in B, C and D too.
So, my question will be what is the use of doing so? What will we achieve through it.

does, it simply state that this method can be overridden by anyone who inherits from B, C and D??

Please suggest.

Thanks,
Pavan.

This is only for readability.
"does, it simply state that this method can be overridden by anyone who inherits from B, C and D??"

I believe so. Assuming B, C & D are derived from A, if we only wanted to override A, then we would not need to place virtual in the class definitions for B, C, and D. Since it did, it is stating they can be overridden as well.
does, it simply state that this method can be overridden by anyone who inherits from B, C and D??
No.
1
2
3
4
5
6
7
8
9
10
struct A{ virtual int foo(){ return 0; } };//a base class

struct B : A{ int foo(){ return 1; } };//no 'virtual' here.

struct C : B{ int foo(){ return 2; } };//no 'virtual' here either

int main(){
   A* a = new C;
   return a->foo();//returns 2, meaning that C::foo is a perfectly fine virtual function.
}
This is only for readability.

This. Once a function is virtual it is virtual in all subclasses. If the classes are in separate files, it's nice to see that it's virtual in the file that you're looking at.
Good advice. That is nice to know.
Topic archived. No new replies allowed.