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??
"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.
struct A{ virtualint 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. 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.