473,583 Members | 3,413 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 1302


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(vo id) const {
std::cout << "Hello!" << std::endl;
}

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

In funct(const bs*), will b->handle() invoke driv::handle(vo id)?
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.goo gle.com...
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(vo id) const {
std::cout << "Hello!" << std::endl;
}

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

In funct(const bs*), will b->handle() invoke driv::handle(vo id)?
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(v oid) {
std::cout << "Hello! Hello!" << std::endl;
}
class driv2: public driv {
public:
/* virtual */ void handle(void);
};

void driv2::handle(v oid){
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.goo gle.com...

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.goo gle.com...
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
6081
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 uses message queues to pass pointers to Events between threads. In my app there are simple events that can be defined using an enum (for example an...
7
1854
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 or something that can hold either an int, or a float, knows which one it is holding, and will allow me to do comparisons with instances of it without...
6
2442
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 'box_shape' and 'cylinder_shape' that derive from a common base class 'shape', more or less like this: namespace Shapes {
13
2349
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 (document.getElementById"].value=='1') { confirm('Are you sure you want to delete this file?'); } } ......
5
3018
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 data types, so I have to implement the RTTI by myself. I need a simple and effective example to help me decide how to design. Can someon help me?
10
1732
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
2140
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
3514
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
17
5799
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: _________________________________________________________________ /* Simple Thread Object ______________________________________________________________*/ #include <pthread.h> extern "C" void* thread_entry(void*);
0
7894
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8172
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. ...
1
7929
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...
0
8190
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...
0
6577
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...
1
5697
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...
0
3841
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2328
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 we have to send another system
0
1152
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...

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.