472,353 Members | 1,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 1778
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*******@COMCAST.NET> wrote in message
news:f1**************************@posting.google.c om...
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 = InvokeDObjectFactory() //Get reference to D from
outside
Dobj.ABase::filter() // 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*******@COMCAST.NET> wrote in message
news:f1**************************@posting.google.c om...
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 = InvokeDObjectFactory() //Get reference to D from
outside
Dobj.ABase::filter() // 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*******@COMCAST.NET> wrote in message
news:f1**************************@posting.google.c om...
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 = InvokeDObjectFactory() //Get reference to D from
outside
Dobj.ABase::filter() // 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*******@COMCAST.NET> skrev i en meddelelse
news:f1**************************@posting.google.c om...
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*******@yahoo.com> wrote in message
news:u3*******************@newssvr17.news.prodigy. 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*******@yahoo.com> wrote in message news:<u3*******************@newssvr17.news.prodigy .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********************@news000.worldonline.d k>...
"jlopes" <jl*******@COMCAST.NET> skrev i en meddelelse
news:f1**************************@posting.google.c om...
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*******@COMCAST.NET> skrev i en meddelelse
news:f1**************************@posting.google.c om...
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:<kB********************@news000.worldonline.d k>...
"jlopes" <jl*******@COMCAST.NET> skrev i en meddelelse
news:f1**************************@posting.google.c om...
> 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
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...
11
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...
16
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...
5
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...
9
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...
6
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...
7
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...
4
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...
9
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...
12
by: Petronius | last post by:
Hallo, does anyone have an idea how to implement difference lists in Javascript? Thanks allot in advance
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.