473,699 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1904
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*******@nets cape.net> wrote in message news:<cn******* ***@news.asu.ed u>...
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*******@COMCA ST.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*******@COMCA ST.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.aboli e> wrote in message news:<41******@ duster.adelaide .on.net>...
jl*******@COMCA ST.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*******@COMCA ST.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
2581
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 2.3 - but it's REALLY slow. Basically, you specify two directed line segments in the coordinate system of a raster image and use the difference between those two lines to transform the image.
9
1873
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 */ } }; class D_of_ABase : public ABase
32
4512
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
9
6513
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 delta binary is pretty huge in size. I have 1 byte file and 2 bytes file, the delta should be 1 byte but somehow it turns out to be 249 bytes using binary formatter. I guess serialization has some other things added to the delta file.
5
5327
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 child : parent {
15
63977
by: DBA | last post by:
Hi All, What is the diff. between a singleton class and a static class in C#?
3
1707
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. -------------- COULD U PLS TELL ME HOW TO DO IT IN C#.Net
9
2368
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 time comprehending the difference. Thanks for your explanations . . .
3
38802
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 confusing to use these keywords. thaks..
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9172
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.