473,657 Members | 2,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Documentatio n. 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::Interse ct, 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::Interse ct, 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 objektorientier ter Datenverarbeitu ng
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
8026
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
8093
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 how and why it works. I'll post a simplified version of it below and ask my questions afterwards: class Base { void *function_ptr;
24
3287
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
4349
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 it inline. Like this.
11
3424
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 exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered like mine. ...
10
4785
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 pure virtual function body
14
4193
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
17
3526
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;" instead? I think declaring a function as "=0" is the same
2
2404
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 static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a template class that interfaces to one of several derived classes through a pointer to a base class. The...
0
8302
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8820
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8499
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8601
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5630
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.