By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

Need clarification for virtual method and pure virtual function

P: n/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.
1is it correct for the above? (The base->func() will call the Child func)
2For the func of the Base class with virtual keyword, it still can have
the guts. Am I right?
3If 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?
Jun 27 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 27 Jun, 09:17, "a" <a...@mail.comwrote:
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.
1is it correct for the above? (The base->func() will call the Child func)
yes.
2For the func of the Base class with virtual keyword, it still can have
the guts. Am I right?
yes.
3If 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
Jun 27 '07 #2

P: n/a
On 27 Jun, 12:41, dasjotre <dasjo...@googlemail.comwrote:
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

Jun 27 '07 #3

P: n/a
On Jun 27, 1:41 pm, dasjotre <dasjo...@googlemail.comwrote:
On 27 Jun, 09:17, "a" <a...@mail.comwrote:
[...]
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:ja*********@gmail.com
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

Jun 28 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.