473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 1887
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Jason | last post by:
Hey, I'm an experience programmer but new to Python. I'm doing a simple implementation of a field morphing techinique due to Beier and Neely (1992) and I have the simple case working in Python...
9
by: jlopes | last post by:
I'm looking at the differences between these to forms and see no difference in their use. When accessed through a derived class. class ABase { public: virtual void filter(){ /* some code */ }...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
5
by: Action | last post by:
does it works like ordinary virtual method?? coz I find that child class can't invoke the event of the parent class. class parent { public virtual event SomeDelegate SomeChanged; } class...
15
by: DBA | last post by:
Hi All, What is the diff. between a singleton class and a static class in C#?
3
by: Raghu Raman | last post by:
Hi I want to find the difference between 2 dates in terms of year.It is confusng in msdn.could u help me on that. for ex, int year=datediff("y",date1,date2)//of course it is in VB. ...
9
by: DrBonzo | last post by:
Is there any effective difference between doing something in the DragDrop event handler and doing it in the OnDragDrop(.) method of a control? I'm coming from a MFC background and am having a hard...
3
by: chandu | last post by:
hello, what is the difference to use the keyword virtual,abstract when we are overriding the methods.instead of abstract shall we use virtual everywhere when we need to override? i am little...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.