469,898 Members | 2,349 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

What the diff between a virtual method and an inheirited method

There seems to bet no diff between a vitual method and an inheirited method.

class A_Base
{
public:
virtual void filter(){ /* some code */ }
};

class D_of_A_Base : public A_Base
{
public:
void filter(){ /* could have used the filter of the base class */ }
};

class AnotherBase
{
public:
void filter(){ /* some code */ }
};

class D_of_A_Base : public AnotherBase
{
public:
filter(){ /* could have used the filter of the base class */ }
};
Jul 22 '05 #1
9 1712
jlopes wrote:
There seems to bet no diff between a vitual method and an inheirited method.

class A_Base
{
public:
virtual void filter(){ /* some code */ }
};

class D_of_A_Base : public A_Base
{
public:
void filter(){ /* could have used the filter of the base class */ }
};

class AnotherBase
{
public:
void filter(){ /* some code */ }
};

class D_of_A_Base : public AnotherBase
{
public:
filter(){ /* could have used the filter of the base class */ }
};


I didn't get what you mean by no difference. Do you understand what are
virtual functions? Do you understand polymorphism?

As far as C++ is considered they are chalk and cheese.
I guess in Java they are the same i.e., inherited functions are also
virtual. Correct me on this if I am wrong.

I will advise you to read the chapters on virtual function in Stroustrup
or Lippman's book.

--
Surendra Singhi

www.public.asu.edu/~sksinghi
Jul 22 '05 #2
Another question pops up in my mind about virtual funtion.
Call to inline member function will be replaced by the function body
during compilation; and virtual function is reference by VTABLE. So,
what about virtual inline member function?

class Foo
{
public:
virtual static print(const std::string &str)
{
std::cout << str << std::endl;
}
}

Is call to print replaced by function body? or "inline" is ignored if
the function is virtual?


Surendra Singhi wrote:
jlopes wrote:
There seems to bet no diff between a vitual method and an inheirited
method.
class A_Base
{
public:
virtual void filter(){ /* some code */ }
};

class D_of_A_Base : public A_Base
{
public:
void filter(){ /* could have used the filter of the base class */ }
};

class AnotherBase
{
public:
void filter(){ /* some code */ }
};

class D_of_A_Base : public AnotherBase
{
public:
filter(){ /* could have used the filter of the base class */ }
};

I didn't get what you mean by no difference. Do you understand what are
virtual functions? Do you understand polymorphism?

As far as C++ is considered they are chalk and cheese.
I guess in Java they are the same i.e., inherited functions are also
virtual. Correct me on this if I am wrong.

I will advise you to read the chapters on virtual function in Stroustrup
or Lippman's book.

Jul 22 '05 #3
> Another question pops up in my mind about virtual funtion.
Call to inline member function will be replaced by the function body
during compilation; and virtual function is reference by VTABLE. So,
what about virtual inline member function?

class Foo
{
public:
virtual static print(const std::string &str)
{
std::cout << str << std::endl;
}
}

Is call to print replaced by function body? or "inline" is ignored if
the function is virtual?


I guess you have made a mistake in the function above and written static
instead of inline.
However if the compiler knows where the object originates from, then it
might be sure that the virtual function is not overridden and then it can
make the function inline. If the origin is unsure (object is a parameter to
a function), then it must call the function via VTABLE.

Niels Dybdahl
Jul 22 '05 #4
Surendra Singhi <ef*******@netscape.net> wrote in message news:<cn**********@news.asu.edu>...
jlopes wrote:
There seems to bet no diff between a vitual method and an inheirited method.

class A_Base
{
public:
virtual void filter(){ /* some code */ }
};

class D_of_A_Base : public A_Base
{
public:
void filter(){ /* could have used the filter of the base class */ }
};

class AnotherBase
{
public:
void filter(){ /* some code */ }
};

class D_of_A_Base : public AnotherBase
{
public:
filter(){ /* could have used the filter of the base class */ }
};


I didn't get what you mean by no difference. Do you understand what are
virtual functions? Do you understand polymorphism?

As far as C++ is considered they are chalk and cheese.
I guess in Java they are the same i.e., inherited functions are also
virtual. Correct me on this if I am wrong.

I will advise you to read the chapters on virtual function in Stroustrup
or Lippman's book.


Let me refine my question. Given a set of objects, each with common
behavior. I then pull out that common behavior into a single object.
That becomes the base of the set of objects. The set need not use
polymorphism, I could make the common methods virtual or not. Since
the common methods are inherited I need only use them. Having read
Stroustrup 3rd ed, he presents vitual methods in their pure form. To
say the a Base class method is virtual and its children in herit that
method is no different than simplly defining the method.

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.
Jul 22 '05 #5
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.
Jul 22 '05 #6
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
Jul 22 '05 #7
"le ténébreux" <pr****************@tour.abolie> wrote in message news:<41******@duster.adelaide.on.net>...
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.


But I can override a method of the base class in derived class with out
defining the base method as virtual.
Jul 22 '05 #8
jl*******@COMCAST.NET (jlopes) wrote:
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.


But I can override a method of the base class in derived class
with out defining the base method as virtual.


And then if you try what I described above (using a single base
class pointer, because your program may not know until runtime
what sort of object it will have to deal with) you'd find that
the base class's version of that function would be called, which
may not be appropriate for the derived object.

Jul 22 '05 #9
A good explanation is found in Effective C++ by Scott Meyers, 2nd
Edition. In item 36 he explains how a function should be declared
in a base class by distinguishing between it's inteface and it's
implementation.

There are three types of functions:

1. Non-Virtual - any derived class will inherit the function's
interface and implementation. It's the same function for all
derived classes.

2. Non-Pure or Simple Virtual - any derived class will inherit
the function's interface and a default implementation. If a
subclass doesn't implement the function it uses the base class's
implementation.

3. Pure Virtual - the derived class will inherit the function's
interface only. Each subclass needs to supply it's own
implementation of the function.

In your example, I think you are referring to non-virtual functions,
and yes their interface and implementation are indeed available to a
derived class.
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Jason | last post: by
32 posts views Thread by Adrian Herscu | last post: by
9 posts views Thread by Ching-Lung | last post: by
3 posts views Thread by Raghu Raman | last post: by
3 posts views Thread by chandu | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.