473,395 Members | 1,670 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Why specify a virtual function in a derived class?

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
9 2029
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kapil Khosla | last post by:
Dear all, I am trying to underlying implementation of virtual functions in C++. The way I understand polymorphism is class Base { public: virtual int func(); };
11
by: Josh Lessard | last post by:
Hi all. I'm maintaining a C++ program and I've come across a nasty piece of code that works, but I just don't understand why. I'm not actually this part of the program, but I really want to know...
24
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
2
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.