My understanding is that a 'Virtual' function will have to be implemented in
each derived class. So
Class SuperParent{
Function WhoAmI : Virtual (){
cout << 'I am an object of class SuperParent';
}
}
Class A_Child : SuperParent {
Function WhoAmI (){
cout << 'I am an object of class A_Child';
}
}
Class A_GrandChild : A_Child {
Function WhoAmI (){
cout << 'I am an object of class A_GrandChild;
}
}
If you action pointers to these three classes and action the function WhoAmI
for each, then you will get the following outcome
I am an object of class SuperParent
I am an object of class A_Child
I am an object of class A_GrandChild
I don't know if classes that have virtual functions declared in them can infact
be instantiated? Common sense says they should not be instantiatable. So many
things about C++ are so weird ...
TFE
http://totallyfreeenergy.zxq.net
--- In [email protected], "Saurabh Jain" <hundredr...@...> wrote:
>
> --- In [email protected], saurabh jain <hundredrabh@> wrote:
> >
> > Hi,
> > Can anyone tell what is supposed to be the behaviour for a function which
> > is declared as virtual in a base class and non-virtual in derived?
> > Specially pertaining to cases when you cast the derived object into a base
> > class object and call the function.
> >
> > For example:
> >
> > class Base {
> > public:
> > virtual void foo() { cout << " I am Base foo \n"; }
> > };
> >
> > class Derived : public Base {
> > public:
> > void foo() { cout << " I am Derived foo\n";}
> > };
> >
>
> For my first question this is what I understood from the standard:
> "If a virtual member function vf is declared in a class Base and in a class
> Derived, derived directly or
> indirectly from Base, a member function vf with the same name and same
> parameter list as Base::vf is
> declared, then Derived::vf is also virtual (whether or not it is so declared)
> and it overrides95)
> __________________
> 95) A function with the same name but a different parameter list (13) as a
> virtual function is not necessarily virtual and does not over10.3
> Virtual functions Derived classes 10 7
> Base::vf."
>
>
> > And to extend the question what if another class Derived2 is derived from
> > class "Derived" with the function foo being declared as non-virtual.
> >
> For the second question :
>
> "[Note: a virtual member function does not have to be visible to be
> overridden, for example,
> struct B {
> virtual void f();
> };
> struct D : B {
> void f(int);
> };
> struct D2 : D {
> void f();
> };
> the function f(int) in class D hides the virtual function f() in its base
> class B; D::f(int) is not a virtual
> function. However, f() declared in class D2 has the same name and the same
> parameter list as
> B::f(), and therefore is a virtual function that overrides the function
> B::f() even though B::f() is
> not visible in class D2. ]
> "
>
>
> > Thanks and Regards,
> > Saurabh
> >
> > $ make war
> > make: *** No rule to make target `war'. Try `peace' instead
> >
>