Need clarification for virtual method and pure virtual function

A

a

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?
 
U

ujjwal

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
 
D

dasjotre

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
 
D

dasjotre

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
 
D

dasjotre

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
 
J

James Kanze

On 27 Jun, 09:17, "a" <[email protected]> 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.
 
D

dasjotre

dasjotre a écrit :


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/infocenter/pseries/v5r3/topic/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"

regards

DS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top