Simple question about virtual function

C

C++fan

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
 
J

Janusz Szpilewski

C++fan said:
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
 
E

E. Robert Tisdale

C++fan said:
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.
 
C

Chris Theis

C++fan said:
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
 
C

C++fan

E. Robert Tisdale said:
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
 
J

jeffc

C++fan said:
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.
 
R

Rolf Magnus

Chris said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top