Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Need clarification for virtual method and pure virtual function

Reply
Thread Tools

Need clarification for virtual method and pure virtual function

 
 
a
Guest
Posts: n/a
 
      06-27-2007
Hi,
I need clarification for virtual method and pure virtual method.
e.g
Class Base{
virtual void func(){
----
}
}
Class Child : public Base{
void func()
{
----
}
}

Child* child = new Child();
Base * base = child;
base->func(); //it will call the Child func, because func is declared as
virtual in Base class.
1> is it correct for the above? (The base->func() will call the Child func)
2> For the func of the Base class with virtual keyword, it still can have
the guts. Am I right?
3> If the func of the Base class is declared as virtual void func()=0, then
there is no gut for it. The gut is defined by the Child. Am I right?


 
Reply With Quote
 
 
 
 
ujjwal
Guest
Posts: n/a
 
      06-27-2007
On Jun 27, 1:17 pm, "a" <(E-Mail Removed)> wrote:
> Hi,
> I need clarification for virtual method and pure virtual method.
> e.g
> Class Base{
> virtual void func(){
> ----}
> }
>
> Class Child : public Base{
> void func()
> {
> ----
>
> }
> }
>
> Child* child = new Child();
> Base * base = child;
> base->func(); //it will call the Child func, because func is declared as
> virtual in Base class.
> 1> is it correct for the above? (The base->func() will call the Child func)
> 2> For the func of the Base class with virtual keyword, it still can have
> the guts. Am I right?
> 3> If the func of the Base class is declared as virtual void func()=0, then
> there is no gut for it. The gut is defined by the Child. Am I right?


first of all the question is not clear, you should define the function
in public scope in order to access it directly from the object or
pointer to the object of that class.

1) This is absolutely correct as the pointer to a base class is
pointing to an object of derived class so it will call the method of
the derived class

2) Definitely as you can have a virtual function doing something
rather than doing nothing unless it is not a pure virtual function
because for a pure virtual function you should not do anything in that
class it only provides the interface not the method henceforth the
concept of an abstract class comes into picture

3) I think third is already answered you can't have the guts in the
pure virtual function declaration because it is intended of doing
nothing



 
Reply With Quote
 
 
 
 
dasjotre
Guest
Posts: n/a
 
      06-27-2007
On 27 Jun, 09:17, "a" <(E-Mail Removed)> wrote:
> Hi,
> I need clarification for virtual method and pure virtual method.
> e.g
> Class Base{
> virtual void func(){
> ----}
> }
>
> Class Child : public Base{
> void func()
> {
> ----
>
> }
> }
>
> Child* child = new Child();
> Base * base = child;
> base->func(); //it will call the Child func, because func is declared as
> virtual in Base class.
> 1> is it correct for the above? (The base->func() will call the Child func)


yes.

> 2> For the func of the Base class with virtual keyword, it still can have
> the guts. Am I right?


yes.

> 3> If the func of the Base class is declared as virtual void func()=0, then
> there is no gut for it. The gut is defined by the Child. Am I right?


no. pure virtual can have guts too. pure virtual only
means that you can not create an object of that class.

class Base
{
virtual void func() =0
{
// guts
}
};

class Child : public Base
{
void func()
{
// call base func
Base::func();
}
};

there is a natural limitation to where you can
call func() thou. since Base can not be instantiate
it has to be a base of some derived class. if you
call func() from Base constructor or destructor,
even thou it has guts, you will get run time
'pure virtual call' error since derived class is
either not constructed yet, or already destructed.

regards

DS


 
Reply With Quote
 
dasjotre
Guest
Posts: n/a
 
      06-27-2007
On 27 Jun, 11:39, ujjwal <(E-Mail Removed)> wrote:
> first of all the question is not clear, you should define the function
> in public scope in order to access it directly from the object or
> pointer to the object of that class.
>
> 1) This is absolutely correct as the pointer to a base class is
> pointing to an object of derived class so it will call the method of
> the derived class


only if the method is virtual of course.

> 2) Definitely as you can have a virtual function doing something
> rather than doing nothing unless it is not a pure virtual function
> because for a pure virtual function you should not do anything in that
> class it only provides the interface not the method henceforth the
> concept of an abstract class comes into picture
>
> 3) I think third is already answered you can't have the guts in the
> pure virtual function declaration because it is intended of doing
> nothing


completely wrong. not only you can provide implementation
for pure virtual functions but in case of pure virtual destructors
you HAVE to provide implementation.

regards

DS

 
Reply With Quote
 
dasjotre
Guest
Posts: n/a
 
      06-27-2007
On 27 Jun, 12:41, dasjotre <(E-Mail Removed)> wrote:
> no. pure virtual can have guts too. pure virtual only
> means that you can not create an object of that class.


and of course, pure virtual means that you don't have
to provide implementation with the declaration.

regards

DS

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-28-2007
On Jun 27, 1:41 pm, dasjotre <(E-Mail Removed)> wrote:
> On 27 Jun, 09:17, "a" <(E-Mail Removed)> wrote:


[...]
> no. pure virtual can have guts too. pure virtual only
> means that you can not create an object of that class.


It also means that if dynamic look-up of the function would find
the pure virtual instance, the behavior is undefined (most
compilers will abort the program with an error message), and
that unless the function is explicitly called (using a qualified
name), it isn't considered "used", so you don't have to provide
an implementation.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
jalina
Guest
Posts: n/a
 
      06-28-2007
dasjotre a écrit :

> class Base
> {
> virtual void func() =0
> {
> // guts
> }
> };


Is this really allowed ? On any compiler that I have in hand (gcc 4.1.2)
or have some documentation (e.g.
http://publib.boulder.ibm.com/infoce...ef/cplr142.htm)
it is not.


Jalina.
 
Reply With Quote
 
dasjotre
Guest
Posts: n/a
 
      06-28-2007
On 28 Jun, 11:43, jalina <(E-Mail Removed)> wrote:
> dasjotre a écrit :
>
> > class Base
> > {
> > virtual void func() =0
> > {
> > // guts
> > }
> > };

>
> Is this really allowed ? On any compiler that I have in hand (gcc 4.1.2)
> or have some documentation (e.g.http://publib.boulder.ibm.com/infoce...opic/com.ibm.x...)
> it is not.


you're right, you shouldn't be able to provide definition
with pure virtual declaration. but you can provide definition
outside.

struct Base
{
virtual void fun()=0;
};

void Base::fun() // this is ok
{
}

10.3.8.
"A virtual function declared in a class shall be defined,
or declared pure in that class, or both"

> Jalina.


regards

DS


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pure space directly inside div ignored, but pure space directlyinside span honored liketofindoutwhy@gmail.com HTML 4 03-29-2008 06:06 PM
virtual vs pure virtual member function sam_cit@yahoo.co.in C++ 7 05-02-2007 10:00 AM
private virtual functions and pure virtual functions with bodies John Goche C++ 10 12-08-2006 04:00 PM
Pure functions still pure after definition Todd Aspeotis C++ 3 05-30-2005 03:53 AM
virtual function and pure virtual function in the implementation of COM IK C++ 2 07-23-2004 02:55 PM



Advertisments