473,589 Members | 2,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I see no difference in an inheirited non-virtual method and an inheirited virtual method

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
{
public:
};

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

class Child : public Base
{
publc:
};

void main()
{
Child* child = new Child();
D_of_ABase* doab = new D_of_ABase();

child->filter();
doab->filter();
}
Jul 22 '05 #1
9 1865
jlopes wrote:
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
{
public:
};
One generally has no reason to inherit unless one overrides a virtual class.
class Base
{
public:
void filter(){ /* some code */ }
};

class Child : public Base
{
publc:
};

void main()
'void main' will make your 'nads drop off. Use 'int main'.
{
Child* child = new Child();
D_of_ABase* doab = new D_of_ABase();

child->filter();
doab->filter();
}


Suppose Child overrode filter() too. Now suppose you have this function:

int foo(Base & b)
{
b.filter();
}

Now you can pass any Base object into foo(), or any Child object, or any
other object that inherits Base. The behavior of filter() will change. This
allows callers to customize foo()'s behavior without changing its source.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces

Jul 22 '05 #2

"jlopes" <jl*******@COMC AST.NET> wrote in message
news:f1******** *************** ***@posting.goo gle.com...
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
{
public:
};

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

class Child : public Base
{
publc:
};

void main()
{
Child* child = new Child();
D_of_ABase* doab = new D_of_ABase();

child->filter();
doab->filter();
}


Even if you do not override the inherited method, you can still see
diferences if the following conditions are met:

- some class E_ derives from class D_of_ABase and E_ overrides filter()
- you use references or pointers to class D_of_ABase
- you do not initialize the reference or pointer based of objects you
create,
but based of objects you receive from some other module or library
- the other module or library knows or defines class E_ and actualy
provides you
with references/pointers to objects of class E_ (this would be
perfectly legal C++)

then when you would call filter() through your reference/pointer to
C_of_ABase, you
will have the surprise of getting yourself a call to some other override of
filter() then the one in ABase (namely the one form E_).

Should you have used classes Base and Child in my example, you would have no
such surprise.

I think you can still explicitly call (with no surprises) the inherited
version of filter() like this:

D_of_ABase &Dobj = InvokeDObjectFa ctory() //Get reference to D from
outside
Dobj.ABase::fil ter() // Explicitly call the base class
override, no matter
// what was provided with the
reference form outside

"Timothy Madden"
Romania
Jul 22 '05 #3

"jlopes" <jl*******@COMC AST.NET> wrote in message
news:f1******** *************** ***@posting.goo gle.com...
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
{
public:
};

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

class Child : public Base
{
publc:
};

void main()
{
Child* child = new Child();
D_of_ABase* doab = new D_of_ABase();

child->filter();
doab->filter();
}


Even if you do not override the inherited method, you can still see
diferences if the following conditions are met:

- some class E_ derives from class D_of_ABase and E_ overrides filter()
- you use references or pointers to class D_of_ABase
- you do not initialize the reference or pointer based of objects you
create,
but based of objects you receive from some other module or library
- the other module or library knows or defines class E_ and actualy
provides you
with references/pointers to objects of class E_ (this would be
perfectly legal C++)

then when you would call filter() through your reference/pointer to
C_of_ABase, you
will have the surprise of getting yourself a call to some other override of
filter() then the one in ABase (namely the one form E_).

Should you have used classes Base and Child in my example, you would have no
such surprise.

I think you can still explicitly call (with no surprises) the inherited
version of filter() like this:

D_of_ABase &Dobj = InvokeDObjectFa ctory() //Get reference to D from
outside
Dobj.ABase::fil ter() // Explicitly call the base class
override, no matter
// what was provided with the
reference form outside

"Timothy Madden"
Romania

Jul 22 '05 #4

"jlopes" <jl*******@COMC AST.NET> wrote in message
news:f1******** *************** ***@posting.goo gle.com...
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
{
public:
};

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

class Child : public Base
{
publc:
};

void main()
{
Child* child = new Child();
D_of_ABase* doab = new D_of_ABase();

child->filter();
doab->filter();
}


Even if you do not override the inherited method, you can still see
diferences if the following conditions are met:

- some class E_ derives from class D_of_ABase and E_ overrides filter()
- you use references or pointers to class D_of_ABase
- you do not initialize the reference or pointer based of objects you
create,
but based of objects you receive from some other module or library
- the other module or library knows or defines class E_ and actualy
provides you
with references/pointers to objects of class E_ (this would be
perfectly legal C++)

then when you would call filter() through your reference/pointer to
C_of_ABase, you
will have the surprise of getting yourself a call to some other override of
filter() then the one in ABase (namely the one form E_).

Should you have used classes Base and Child in my example, you would have no
such surprise.

I think you can still explicitly call (with no surprises) the inherited
version of filter() like this:

D_of_ABase &Dobj = InvokeDObjectFa ctory() //Get reference to D from
outside
Dobj.ABase::fil ter() // Explicitly call the base class
override, no matter
// what was provided with the
reference form outside

"Timothy Madden"
Romania

Jul 22 '05 #5

"jlopes" <jl*******@COMC AST.NET> skrev i en meddelelse
news:f1******** *************** ***@posting.goo gle.com...
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 */ } When having a virtual function you sould alway include a virtual destructor:
virtual ~ABase() {} };

class D_of_ABase : public ABase
{
public:
};

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

class Child : public Base
{
publc:
};

void main() should be int main() {
Child* child = new Child();
D_of_ABase* doab = new D_of_ABase();

child->filter();
doab->filter();
}


There is no difference here as you do not override the virtual function, but
try doing that (print some statement in each function) and see what happens
here:

int main()
{
Base *base = new Child();
ABase *abase = new D_of_ABase();
base->filter();
abase->filter();
}

/Peter
Jul 22 '05 #6

"Phlip" <ph*******@yaho o.com> wrote in message
news:u3******** ***********@new ssvr17.news.pro digy.com...

One generally has no reason to inherit unless one overrides a virtual class.


He means to say, "unless one overrides a virtual function." If you don't
override the function, then there is no point to it.
Jul 22 '05 #7
"Phlip" <ph*******@yaho o.com> wrote in message news:<u3******* ************@ne wssvr17.news.pr odigy.com>...
jlopes wrote:
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
{
public:
};


One generally has no reason to inherit unless one overrides a virtual class.
class Base
{
public:
void filter(){ /* some code */ }
};

class Child : public Base
{
publc:
};

void main()


'void main' will make your 'nads drop off. Use 'int main'.
{
Child* child = new Child();
D_of_ABase* doab = new D_of_ABase();

child->filter();
doab->filter();
}


Suppose Child overrode filter() too. Now suppose you have this function:

int foo(Base & b)
{
b.filter();
}

Now you can pass any Base object into foo(), or any Child object, or any
other object that inherits Base. The behavior of filter() will change. This
allows callers to customize foo()'s behavior without changing its source.


Thanks Phil

Your thought the in general there is no need to inherit is an answer
closer to what I'm looking for. If one has a basic set of behavior for
a given set of objects, and the individual objects has distinct
behavior, they would inherit behavior from the common object
(Polymorphisim) . As for the "void main" heh just wanted the code to
compile, sorry the code was not politically correct. As for the foo()
method heh polymorphism I'm aware of.

My quesstion was centered round to virtual or not to virtual.
Jul 22 '05 #8
"Peter Koch Larsen" <pk*****@mailme .dk> wrote in message news:<kB******* *************@n ews000.worldonl ine.dk>...
"jlopes" <jl*******@COMC AST.NET> skrev i en meddelelse
news:f1******** *************** ***@posting.goo gle.com...
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 */ }

When having a virtual function you sould alway include a virtual destructor:
virtual ~ABase() {}
};

class D_of_ABase : public ABase
{
public:
};

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

class Child : public Base
{
publc:
};

void main()

should be int main()
{
Child* child = new Child();
D_of_ABase* doab = new D_of_ABase();

child->filter();
doab->filter();
}


There is no difference here as you do not override the virtual function, but
try doing that (print some statement in each function) and see what happens
here:

int main()
{
Base *base = new Child();
ABase *abase = new D_of_ABase();
base->filter();
abase->filter();
}

/Peter


Thanks Peter, I thought there wasn't a difference.

I deliberately left out the details to gen a debate on this subject.
The d_tor was not part of the subject. I left out the override to
limit the generating a discussion of polymorphism (it didn't work
but...heh).

In trying to print something do you suspect the linker would mixup the
class/method signature? Hmm that woud be ugly, intresting.
Jul 22 '05 #9

"jlopes" <jl*******@COMC AST.NET> skrev i en meddelelse
news:f1******** *************** ***@posting.goo gle.com...
"Peter Koch Larsen" <pk*****@mailme .dk> wrote in message
news:<kB******* *************@n ews000.worldonl ine.dk>...
"jlopes" <jl*******@COMC AST.NET> skrev i en meddelelse
news:f1******** *************** ***@posting.goo gle.com...
> 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 */ }

When having a virtual function you sould alway include a virtual
destructor:
virtual ~ABase() {}
> };
>
> class D_of_ABase : public ABase
> {
> public:
> };
>
> class Base
> {
> public:
> void filter(){ /* some code */ }
> };
>
> class Child : public Base
> {
> publc:
> };
>
> void main()

should be int main()
> {
> Child* child = new Child();
> D_of_ABase* doab = new D_of_ABase();
>
> child->filter();
> doab->filter();
> }


There is no difference here as you do not override the virtual function,
but
try doing that (print some statement in each function) and see what
happens
here:

int main()
{
Base *base = new Child();
ABase *abase = new D_of_ABase();
base->filter();
abase->filter();
}

/Peter


Thanks Peter, I thought there wasn't a difference.

I deliberately left out the details to gen a debate on this subject.
The d_tor was not part of the subject. I left out the override to
limit the generating a discussion of polymorphism (it didn't work
but...heh).

In trying to print something do you suspect the linker would mixup the
class/method signature? Hmm that woud be ugly, intresting.


Ugly? Not at all - this is intended behaviour:
base->filter() will call the filter in base as base::filter is not virtual.
abase->filter() will call the filter in D_of_ABase. This is because
ABase::filter is virtual so the filter of the actual object, which here is
of type D_of_ABase, will be called.

/Peter
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
2140
by: Raj Dhrolia | last post by:
Hi Guys, It might seem to be a very easy question, but i am very much eager to know some good technical difference between C# and VB.NET. Are there anything that i can do in one language and cannot in other? And finally why C# seems to be a sort of standard in industry? Regards, Raj Dhrolia.
11
5702
by: Shea Martin | last post by:
I have been programming in C++ for over 4 years. I *think* I knew that a struct could have a constructor but I decided to dig into it a little more today, and found that there is very little difference between a struct and a class in C++. Both have inheritance, access modifiers, etc. The only diff I see is the default access level is public vs. private. I have always just used structs as a minimal class, as that is the way
16
16251
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
5
28033
by: QQ | last post by:
I know there are many functions that I can exit the program such as return 0, exit(0), exit(1),_EXIT(0) .... What are the difference between them? Thanks a lot!
9
3766
by: tkrogc | last post by:
I compiled the source below using the comeau compiler http://www.comeaucomputing.com/tryitout/ Why does the f funtion compile while the g function produces an lvalue error ? (I thought the two functions were identical except for a harmless syntax difference) struct A { void f(){}
6
2418
by: cdrsir | last post by:
we can use 1) // my comments a 2) /* my comments b */ when we want to add some comments. I know some compilers just support the 2nd syntax, but normally both of these 2 syntaxs are supported by most of the compilers. But what are the difference for those two syntaxs in the sense for the compiler, if it support both. Is one of them will be processed faster?
7
6681
by: Alex Vinokur | last post by:
What is the difference between an operator and a function in C++? Alex Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
4
7691
by: Academic | last post by:
I read the Help and some of the many Google hits I got but can't find out the difference between MyBase.Closing and MyBase.FormClosing Can anyone tell me? Thanks
9
5749
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just call it Express for simplicity). I have, to try to simplify things, put the exact same DB on two systems, one running MSDE and one running Express. Both have 2 Ghz processors (one Intel, one AMD), both have a decent amount of RAM (Intel system...
12
2705
by: Petronius | last post by:
Hallo, does anyone have an idea how to implement difference lists in Javascript? Thanks allot in advance
0
7865
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8233
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
8360
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7990
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8224
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
6637
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
3889
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2374
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1198
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.