C++: Virtual fnc Vs Pure fnc?

A

A

Hi,

1) Whats the difference from a virtual function and a pure virtual function?

2) When would you use one over the over?

3) What's the significance of the word virtual?


Regards,
A
 
O

osmium

A said:
1) Whats the difference from a virtual function and a pure virtual function?

2) When would you use one over the over?

3) What's the significance of the word virtual?

That's a question I would normally try to answer, but it sounds just too
much like a take-home test. Try google. If you answer 1 and 2 the answer
to 3 should almost be a side effect.
 
E

EventHelix.com

1) Whats the difference from a virtual function and a pure virtual function?

Virtual functions that must be overriden by the inheriting class are pure
virtual function.
2) When would you use one over the over?

Use virtual functions when the base class can provide a reasonable
default handling for the method. Use pure virtual when the base class
just defines the interface for the function and no default handling
is applicable.
3) What's the significance of the word virtual?

What's in a name? Virtual is a misleading keyword for overridable

Sandeep
 
R

Ron Natalie

EventHelix.com said:
Virtual functions that must be overriden by the inheriting class are pure
virtual function.

....or the derived class remains abstract.
Use virtual functions when the base class can provide a reasonable
default handling for the method. Use pure virtual when the base class
just defines the interface for the function and no default handling
is applicable.

Or when you wish to force the derived class to at least think about whether
the base class default is appropriate. There's no requirement that the base
class not implement a pure virtual.
What's in a name? Virtual is a misleading keyword for overridable

Virtual...by definition: in appearance but not in fact. While it appears
you are calling the base class function, you are in fact calling the one from
the derived class.
 
D

Deming He

Also, for a virtual function you need an implementation as a place holder,
but for pure virtual, you don't.
 
M

Mike Smith

EventHelix.com said:
What's in a name? Virtual is a misleading keyword for overridable

It's more than that. Non-virtual functions can be overridden, too. The
difference is *polymorphism*.
 
R

Ron Natalie

Mike Smith said:
It's more than that. Non-virtual functions can be overridden, too. The
difference is *polymorphism*.

Non-virtual functions are not overriden. The definition of override in C++
is based on virtual functions (10.3/2):

If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or
indirectly from Base, a member function vf with the same name and same parameter list as Base::vf is
declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::vf.
 
J

jeffc

Ron Natalie said:
There's no requirement that the base
class not implement a pure virtual.

For some reason, this is not intuitive for the vast majority of programmers.
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top