Re: What the diff between a virtual method and an inheirited method

From: Surendra Singhi (efuzzyone_at_netscape.net)
Date: 11/20/04


Date: Sat, 20 Nov 2004 02:00:01 -0700

le ténébreux wrote:
> jlopes151@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


Relevant Pages

  • Re: Abstract Class Theory
    ... >> there would be no need for abstract methods, and no abstract classes. ... >> the derived class will be overriding, or even overloading, the base class ... > class inherit from both. ... Actually you are confusing abstract classes and interfaces. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Abstract Class Theory
    ... > the derived class will be overriding, or even overloading, the base class ... If you read into the Patterns literature, you can see that the requirement ... In addition, if you define two abstract classes, you can have a concrete ... class inherit from both. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Visual Studio 2003, CollectionBase.RemoveAt Method
    ... You don't think of it as such because Microsoft also hid the ... The VB Collection class does not inherit System.Collections.CollectionBase. ... you must explicitly define a "RemoveAt" method in order to ... you can't hide anything in a derived class from a base class. ...
    (microsoft.public.vsnet.general)
  • Re: Dynamic class creation at runtime
    ... dinamically a new type that inherit directly from the base class. ... I have in my assembly the base class and 2 inherited classes: ... >> classes for each single algorithm. ... >> dynamic definition of Assemblies, but I do not want to create a new Assembly, ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Inheritance
    ... objects from it - which means I still get a 'cannot expose a Friend type ... that inherit from Y. Can this be done? ... >>I want to make a base class such that it can only be inheritted by derived ... >>classes (at least the derived classes) to be public. ...
    (microsoft.public.dotnet.general)