473,505 Members | 14,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple question about virtual function

Hi all:

Below is simple code:
class bs {
public:
virtual void handle() = 0;
};

class driv : public bs {
public:
void handle();
};

void driv::handle(){

cout << "hello " <<endl;
};

void funct(bs* b){
b -> handle();
};

In funct(), will b->handle() invoke the handle() of class driv or the
handle() class bs?
And why?

Thanks.

Jack
Jul 22 '05 #1
6 1291


C++fan wrote:
Hi all:

Below is simple code:
class bs {
public:
virtual void handle() = 0;
};

class driv : public bs {
public:
void handle();
};

void driv::handle(){

cout << "hello " <<endl;
};

void funct(bs* b){
b -> handle();
};

In funct(), will b->handle() invoke the handle() of class driv or the
handle() class bs?
And why?

Why not try to test it?... Calling yourself C++fan should require some
commitment. Btw, I guess you would not want 'handle' declared in the
class 'bs' to be invoked...

Some hint, a function declared as virtual in a base class makes virtual
all functions overriding it in the derived classes even if the keyword
virtual was not used there explicitly.

Regards,
Janusz

Jul 22 '05 #2
C++fan wrote:
Below is simple code:

class bs {
public:
virtual void handle(void) const = 0;
};

class driv: public bs {
public:
/* virtual */ void handle(void) const;
};

void driv::handle(void) const {
std::cout << "Hello!" << std::endl;
}

void funct(const bs* b){
b->handle();
}

In funct(const bs*), will b->handle() invoke driv::handle(void)?
Or bs::handle(void)?


You can't instantiate an object of type bs
so b->handle() must invoke member function handle(void)
defined by some class derived from bs.
The only class that you have derived from bs so far is driv
so, if you write:

const driv d;
funct(&d);

your program will write

Hello!

to standard output.

Jul 22 '05 #3

"C++fan" <jw****@excite.com> wrote in message
news:15**************************@posting.google.c om...
Hi all:

Below is simple code:
class bs {
public:
virtual void handle() = 0;
};

class driv : public bs {
public:
void handle();
};

void driv::handle(){

cout << "hello " <<endl;
};

void funct(bs* b){
b -> handle();
};

In funct(), will b->handle() invoke the handle() of class driv or the
handle() class bs?
And why?

Thanks.

Jack


The easiest way is to test this yourself. Anyway, as you're declaring the
function handle() purely virtual in the base class, there is no
implementation. Thus, there is simply no way to call it and as bs is a
purely virtual class there is no way of instantiating an object of type bs
either.

Cheers
Chris
Jul 22 '05 #4
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<40************@jpl.nasa.gov>...
C++fan wrote:
Below is simple code:

class bs {
public:
virtual void handle(void) const = 0;
};

class driv: public bs {
public:
/* virtual */ void handle(void) const;
};

void driv::handle(void) const {
std::cout << "Hello!" << std::endl;
}

void funct(const bs* b){
b->handle();
}

In funct(const bs*), will b->handle() invoke driv::handle(void)?
Or bs::handle(void)?


You can't instantiate an object of type bs
so b->handle() must invoke member function handle(void)
defined by some class derived from bs.
The only class that you have derived from bs so far is driv
so, if you write:

const driv d;
funct(&d);

your program will write

Hello!

to standard output.


Thanks. I have tested it. It is the same result.
Now I add two more derived classes as below:

class driv1: public bs {
public:
/* virtual */ void handle(void);
};

void driv1::handle(void) {
std::cout << "Hello! Hello!" << std::endl;
}
class driv2: public driv {
public:
/* virtual */ void handle(void);
};

void driv2::handle(void){
std::cout << "Hello! Hello! Hello!" << std::endl;
}

and

driv* a1 = new driv;
funct(a1);

driv1* a2 = new driv1;
funct(a2);

driv2* a3 = new driv2;
funct(a3);

What will be the respective result?

Thanks.

Jack
Jul 22 '05 #5

"C++fan" <jw****@excite.com> wrote in message
news:15**************************@posting.google.c om...

Thanks. I have tested it. It is the same result.
Now I add two more derived classes as below:

...

What will be the respective result?


So you were able to test it. But now you want him to answer *another*
question without trying it yourself. If you keep doing this it's going to
look like you actually aren't trying these things, but just trying to get
homework questions answered.
Jul 22 '05 #6
Chris Theis wrote:

"C++fan" <jw****@excite.com> wrote in message
news:15**************************@posting.google.c om...
Hi all:

Below is simple code:
class bs {
public:
virtual void handle() = 0;
};

class driv : public bs {
public:
void handle();
};

void driv::handle(){

cout << "hello " <<endl;
};

void funct(bs* b){
b -> handle();
};

In funct(), will b->handle() invoke the handle() of class driv or the
handle() class bs?
And why?

Thanks.

Jack


The easiest way is to test this yourself. Anyway, as you're declaring
the function handle() purely virtual in the base class, there is no
implementation.


This isn't necessary though. You can implement a pure virtual member
function. However, the class can still not be instantiated.

Jul 22 '05 #7

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

Similar topics

8
6075
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that...
7
1844
by: Ben | last post by:
Hi all, I'm not yet good at thinking the right way in c++ so although I could solve this problem, I'm not sure if they way I'm thinking of is the best way to do it. I need a data type or class...
6
2436
by: Jacek Dziedzic | last post by:
Hello! First of all please forgive me for not posting a compilable snippet, but rather a simplified piece of code with the unimportant details left out. Let's say I have two classes...
13
2330
by: LRW | last post by:
Having a problem getting a onSubmit function to work, to where it popsup a confirmation depending on which radiobutton is selected. Here's what I have: function checkdel() { if...
5
3009
by: dotNeter | last post by:
I'm studying the RTTI, and my current work is concern for how to get the self-defined type at runtime, that's exactly what the RTTI does. I mean, in my application, I built several self-defined...
10
1719
by: Ivan Vecerina | last post by:
Here's a relatively simple code snippet: #include <memory> class Base { public: Base(); virtual ~Base(); virtual void f(int a, char const* name);
4
2136
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
17
3510
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;"...
17
5788
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
7098
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
7303
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
7367
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...
1
7018
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
7471
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...
1
5028
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...
0
3187
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...
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
407
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...

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.