Things are told and standards are specified clearly in C++. The face is that
we are not aware of some of those at times..
First of all, as per C++ standard its not mandatory to implement the virtual
function in each derived class unless it is declared as pure virtual
function
Class SuperParent
{
virtual void WhoAmI()
{
cout << 'I am an object of class SuperParent';
}
};
Class A_Child : SuperParent
{
} ;
This is perfectly valid in C++. In this case if you call WhoAmI() on the
derived object, the base class version will be called.
Now coming to pure virtual functions
Class SuperParent
{
virtual void WhoAmI() = 0 ;
};
Class A_Child : SuperParent
{
virtual void WhoAmI()
{
cout << 'I am an object of class A_Child';
}
} ;
In this case your derived class has to implement the virtual function
WhoAmI(). If not your compiler will raise compile time error.
regarding your second question,
A class with virtual functions can very well be instantiated.
On the other hand, a class with pure virtual functions cannot be
instantiated in C++. We call these classes as abstract classes.
This kind of classes are used to implement interface kind of logic which is
directly supported in Java.
Hope this info helps you in getting more clarity.
-Thangaraj
On Thu, Aug 6, 2009 at 4:07 AM, Totally Freeenergy <
[email protected]> wrote:
>
>
> 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] <c-prog%40yahoogroups.com>, "Saurabh Jain"
> <hundredr...@...> wrote:
> >
> > --- In [email protected] <c-prog%40yahoogroups.com>, 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
> > >
> >
>
>
>
[Non-text portions of this message have been removed]
------------------------------------
To unsubscribe, send a blank message to
<mailto:[email protected]>.Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/c-prog/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/c-prog/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:[email protected]
mailto:[email protected]
<*> To unsubscribe from this group, send an email to:
[email protected]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/