le ténébreux wrote:
jl*******@COMCAST.NET (jlopes) wrote:
So my qusetion:
If one removes the concept of polymorphism and pure virtual
methods. To inherit a method from a base class defined as virtual is
no different than a defined method in that base class.
I think the whole point of using virtual functions is
to allow the derived classes to define their own unique
behaviour for that function.
Say you want to call a function through a single pointer to
the base class which at runtime may arbitrarily point to any
derived object. If the function is virtual, it will use the
appropriate code for that derived class. Extremely useful.
There are different reasons why you have inherited and virtual
functions. At the minimum virtual methods are inherited by the derived
class and so are the same as inherited methods.
So, if you have some common functionality in your classes then you can
put it in the base class, and use it in all your derived classes as an
inherited method. So, this is inherited methods.
Now for virtual methods, in addition to being inherited they allow,
runtime linking. The vtable is used for linking to virtual functions.
For example you have some function in the base class say (area) this is
a property common to all objects(shapes), but each object has different
implementations of it. So, you can define the area method in the class
shape, then this class is inherited by everyone(square, rectangle,
circle) and then a different function can create an object of type shape
and find out its area without knowing whether it is square, rectangle.
I will recommend you the book "Structure and Interpretation of Computer
Programs". It is available online for free. It is a book on programming
and uses Scheme, but it will teach you the concepts of abstraction and
functional programming, and believe me it will make you a better
programmer and make you appreciate and not awed by the concepts of C++.
Goigng through stroustrup's book will be like piece of cake.
--
Surendra Singhi
www.public.asu.edu/~sksinghi