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

Why specify a virtual function in a derived class?

P: n/a
On this page:

http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html

Shape specify the virtual function:
virtual double Intersect( const Shape& s) = 0;

then the derived class Circle also specify:

virtual double Intersect( const Shape& s) = 0;
Why does Circle use the keyword "virtual"?

I have read that if a base class declares a function "fun" as virtual
all derived classes inherits this virtual function and does not need to
use the keyword "virtual" in their declarations.
May 10 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
I have read that if a base class declares a function "fun" as virtual
all derived classes inherits this virtual function and does not need to
use the keyword "virtual" in their declarations.
Yes thats true, it's just a lot of people add it in, so that a new
class was derived from Circle then we could specialise it's
implementation of the Intersect function.

In a similar way a lot of people create Base class destructors as
virtual even though it's only explicitly required in the derived
classes.
It's just an example of safe coding.

May 10 '07 #2

P: n/a
desktop wrote:
On this page:

http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html

Shape specify the virtual function:
virtual double Intersect( const Shape& s) = 0;

then the derived class Circle also specify:

virtual double Intersect( const Shape& s) = 0;
Why does Circle use the keyword "virtual"?

I have read that if a base class declares a function "fun" as virtual
all derived classes inherits this virtual function and does not need to
use the keyword "virtual" in their declarations.
You are correct that the derived class does not have to declare
'virtual' again. But by doing that, the code is self-documenting and
anyone who reads it can immediately pick it up.

Fei
May 10 '07 #3

P: n/a
desktop wrote:
On this page:

http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html

Shape specify the virtual function:
virtual double Intersect( const Shape& s) = 0;

then the derived class Circle also specify:

virtual double Intersect( const Shape& s) = 0;
Why does Circle use the keyword "virtual"?

I have read that if a base class declares a function "fun" as virtual
all derived classes inherits this virtual function and does not need to
use the keyword "virtual" in their declarations.
Documentation. Also, it's being declared explicitly pure in Circle.

May 10 '07 #4

P: n/a
red floyd wrote:
desktop wrote:
>On this page:

http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html

Shape specify the virtual function:
virtual double Intersect( const Shape& s) = 0;

then the derived class Circle also specify:

virtual double Intersect( const Shape& s) = 0;
Why does Circle use the keyword "virtual"?

I have read that if a base class declares a function "fun" as virtual
all derived classes inherits this virtual function and does not need
to use the keyword "virtual" in their declarations.

Documentation. Also, it's being declared explicitly pure in Circle.
its only pure in Shape right? ( virtual functions are pure only when the
initialiser = 0 is used.)
May 10 '07 #5

P: n/a
Keith Halligan wrote:
>I have read that if a base class declares a function "fun" as virtual
all derived classes inherits this virtual function and does not need to
use the keyword "virtual" in their declarations.

Yes thats true, it's just a lot of people add it in, so that a new
class was derived from Circle then we could specialise it's
implementation of the Intersect function.

In a similar way a lot of people create Base class destructors as
virtual even though it's only explicitly required in the derived
classes.
I wouldn't say this, base class destructor needs to be virtual otherwise
deletion through base class leads to UB.
It's just an example of safe coding.
May 10 '07 #6

P: n/a
Keith Halligan wrote:
>I have read that if a base class declares a function "fun" as virtual
all derived classes inherits this virtual function and does not need to
use the keyword "virtual" in their declarations.

Yes thats true, it's just a lot of people add it in, so that a new
class was derived from Circle then we could specialise it's
implementation of the Intersect function.

But a derived class from Circle would still inherit the virtual function
Intersect from Circle even though Circle does not specify it as
"virtual" right (and its "virtuality")
May 10 '07 #7

P: n/a
desktop wrote:
red floyd wrote:
>desktop wrote:
>>On this page:

http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html

Shape specify the virtual function:
virtual double Intersect( const Shape& s) = 0;

then the derived class Circle also specify:
You are pure virtual here.
>>virtual double Intersect( const Shape& s) = 0;
>>>

Why does Circle use the keyword "virtual"?

I have read that if a base class declares a function "fun" as virtual
all derived classes inherits this virtual function and does not need
to use the keyword "virtual" in their declarations.

Documentation. Also, it's being declared explicitly pure in Circle.

its only pure in Shape right? ( virtual functions are pure only when the
initialiser = 0 is used.)
No, given your description of Circle::Intersect, it's also pure in
Circle. See above.
May 10 '07 #8

P: n/a
red floyd wrote:
desktop wrote:
>red floyd wrote:
>>desktop wrote:
On this page:

http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html

Shape specify the virtual function:
virtual double Intersect( const Shape& s) = 0;

then the derived class Circle also specify:

You are pure virtual here.
>>>virtual double Intersect( const Shape& s) = 0;

>>>>

Why does Circle use the keyword "virtual"?

I have read that if a base class declares a function "fun" as
virtual all derived classes inherits this virtual function and does
not need to use the keyword "virtual" in their declarations.

Documentation. Also, it's being declared explicitly pure in Circle.

its only pure in Shape right? ( virtual functions are pure only when
the initialiser = 0 is used.)

No, given your description of Circle::Intersect, it's also pure in
Circle. See above.
whops my bad - a copy paste error, the link shows the right implementation.
May 10 '07 #9

P: n/a
On May 10, 8:36 pm, Keith Halligan <keith.halli...@gmail.comwrote:
I have read that if a base class declares a function "fun" as virtual
all derived classes inherits this virtual function and does not need to
use the keyword "virtual" in their declarations.
Yes thats true, it's just a lot of people add it in, so that a new
class was derived from Circle then we could specialise it's
implementation of the Intersect function.
As far as the language is concerned, there is absolutely no
difference whether the virtual is present in Circle or not. If
a function is virtual in a base class, it is virtual in all
derived classes, always. There's no way you can turn virtuality
off, once it's established.
In a similar way a lot of people create Base class destructors as
virtual even though it's only explicitly required in the derived
classes.
It's just an example of safe coding.
No. If you delete an object through a Base*, and the actual
type of the object is not B, it is undefined behavior if the
destructor is not virtual. Although the names of the
destructors in derived classes may not look the same, all
destructors are treated as the "same function", with regards to
virtuality. Virtual on the destructor of the base class implies
that all destructors of all derived classes are virtual as well.

To answer the original question: as far as the compiler is
concerned, the only time you need virtual in a derived class is
when that class introduces new virtual functions. Human beings,
however, read code differently than the compiler. When the
compiler sees the derived class definition, it has already read
the base class definition, and has all of the information in its
symbol table. When a human reads the code, he often has not
seen the base class definition, and doesn't really want to have
to go back to it to know what is going on. So every coding
guideline I've seen requires the use of virtual in the derived
class as well.

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

May 11 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.