473,763 Members | 8,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a basic virtual function question

Hi, all:

I have 3 class X, Y, Z

class Y is a subclass of class X;
class Z is a subclass of class Y;

i.e.
class Y : public class X
class Z : public class Y

X has a virtual function, for example, print(),
Y and Z both need to override this function.

My question is : Need I declare print as a virtual in class Y also?
I write a code and it seems that no need to declare virtual in class
Y's print method, but I don't know why. Can anyone help me a bit, thank
you.

------------------Code snippet------------------------------------

#include <iostream>
using namespace std;

class X
{
public:
virtual void print()
{
cout << "class X\n";
}
};

class Y :public X
{
public:
virual void print()
~~~~~~
{
cout << "class Y\n";
}
};

class Z :public Y
{
public:
void print()
{
cout << "class Z\n";
}
};

------------------------------------------------------------
Rick

Oct 25 '05 #1
5 1621

go***********@g mail.com wrote:
Hi, all:

I have 3 class X, Y, Z

class Y is a subclass of class X;
class Z is a subclass of class Y;

i.e.
class Y : public class X
class Z : public class Y

X has a virtual function, for example, print(),
Y and Z both need to override this function.

My question is : Need I declare print as a virtual in class Y also?
No. The virtual property of a function is, unfortunately, silently
inherited from bases all throughout the derived class hierarchy.

This means that if you introduce a new virtual function to a
widely-inherited base class, and its name and type signature happens to
match existing functions in some of the derived classes, you have
probably just introduced a bug, and one that may be hard to find. These
existing functions now become virtuals that override the ``new''
function, no matter how distant they are in the inheritance tree.
I write a code and it seems that no need to declare virtual in class
Y's print method, but I don't know why.


It's an inheritance rule.

Oct 25 '05 #2
If you want to override the print function of class X, you don't need
to use 'virtual' keyword explicitly in the derived class. Because the
overriding function defautly is a virtual function, no matter it has
'virtual' keyword or not. I think, 'virtual function' means that , the
address of the calling function can only be gotten during run-time, not
compile-time.

Oct 25 '05 #3
go***********@g mail.com wrote:
Hi, all:

I have 3 class X, Y, Z

class Y is a subclass of class X;
class Z is a subclass of class Y;

i.e.
class Y : public class X
class Z : public class Y

X has a virtual function, for example, print(),
Y and Z both need to override this function.

My question is : Need I declare print as a virtual in class Y also?
I write a code and it seems that no need to declare virtual in class
Y's print method, but I don't know why. Can anyone help me a bit,
thank you.

------------------Code snippet------------------------------------

#include <iostream>
using namespace std;

class X
{
public:
virtual void print()
You only need the 'virtual' keyword here.
{
cout << "class X\n";
}
};

class Y :public X
{
public:
virual void print()
~~~~~~
Here you can have:
virtual void print()
or:
void print()

Either way it is virtual because it overrides the virtual X::print.
{
cout << "class Y\n";
}
};

class Z :public Y
{
public:
void print()
The same goes here.
{
cout << "class Z\n";
}
};


All overrides of a virtual function are implicitly virtual as well, with or
without the keyword. This is probably because it does not make much sense to
allow an override of a virtual function to itself be non-virtual, so the
compiler automatically makes it virtual and doesn't make a fuss if you
didn't use the keyword. Some people prefer to use the keyword on all virtual
functions, simply so you don't have to look up the one at the top to find
out if it's virtual, but that's up to you.

DW
Oct 25 '05 #4
Kaz Kylheku wrote:
go***********@g mail.com wrote:
X has a virtual function, for example, print(),
Y and Z both need to override this function.

My question is : Need I declare print as a virtual in class Y also?


No. The virtual property of a function is, unfortunately, silently
inherited from bases all throughout the derived class hierarchy.

This means that if you introduce a new virtual function to a
widely-inherited base class, and its name and type signature happens
to match existing functions in some of the derived classes, you have
probably just introduced a bug, and one that may be hard to find.


Kind of an unlikely bug isn't it? Same name, same parameter types by
accident? And even if you do this without realizing it, isn't it virtually
(pun intended) certain that you'll want the most-derived function to be
called, should the call be made through a base-class reference or pointer?

DW
Oct 25 '05 #5
function once declared virtual is always virtual in its class
hierarchy. More interstingly even if the same function is made private
it can still be accessed as its virtual.
class X
{
public:
virtual void show()
{
cout<<"X::show( )"<<endl;
}
};

class Y : public X
{
//Make this function private
private:
void show()
{
cout<<"Y::show( )"<<endl;
}
};

void main()
{
X *pX = new Y;
pX->show();
}

Output:
----------
Y::show().

Oct 25 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3454
by: Wouter Moors | last post by:
Hi all, i got a question about virtual functions. Suppose class A has a virtual function doIt() and in my derived class B i do not declare it as virtual. Now, will that function still be virtual in a third class C which is derived from class B ? TAI, Wouter
15
10641
by: Dave Townsend | last post by:
Yo, I had a job interview today, the interviewing asked me about inline virtual functions, or what was my opinion on them. Hm, I've seen mention of these babies in the reference material, but I've never used one. ( I'm an experienced software developer and have used C++ for more than 10 years) Anybody found the use of one of these guys necessary or useful. Curious minds need to know for the next curve ball question coming my
3
1632
by: prashna | last post by:
Hi all, Is'nt a function invocation through a function pointer is dynamic binding? For example consider the following program 1 int main() 2 { 3 int (*fun_ptr)(); 4 int fun(){printf("HIIIIII\n");}
24
3304
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();
1
1243
by: Bob | last post by:
Try the following code class basic { public: virtual void one()=0; }; class derived1:public basic { public:
16
3756
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
3
11837
by: Jimmy | last post by:
I was browsing the C++ FAQ Lite the other day when I came accross question #23.4 (http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.4). The question was "When should someone use private virtuals?", and the author answered with "almost never, but there are reasons". So, I am asking, what are the reasons. It seems illogical to have one; the point of a virtual function is to be overriden in a derived class, and the...
4
2148
by: cwc5w | last post by:
I have two classes. One with a regular destructor and the other with a virtual destructor. e.g. class x { ~x(){} } vs
8
4401
by: JackC | last post by:
Hi, I am trying to get posix threads working from within an object, heres some code: int NConnection::TheadControl() { int thread_id; pthread_t new_connection; pthread_create(&new_connection, NULL, PriC, (void *)thread_id);
0
10148
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...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
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
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6643
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
5270
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...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2794
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.