C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

pure virtual

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Sat Sep 27, 2003 5:13 pm    Post subject: pure virtual Reply with quote



I don't understand. What's the difference in practice between 2 next lines?
When I *must* use first line and when second?
-------------
virtual int a(int b) = 0;
virtual int a(int b);
-------------

thank you


Back to top
Siana
Guest





PostPosted: Sat Sep 27, 2003 5:39 pm    Post subject: Re: pure virtual Reply with quote



On Sat, 27 Sep 2003 20:13:31 +0300, "<- Chameleon ->"
<cham_gss (AT) hotmail (DOT) NOSPAM.com> wrote:

Quote:
I don't understand. What's the difference in practice between 2 next lines?
When I *must* use first line and when second?
-------------
virtual int a(int b) = 0;
virtual int a(int b);
-------------

thank you


The first line declares a function a in some class that has no
definition - it is there only to be overridden by derived classes for
the purposes of polymorphism. It also makes the class abstract (unable
to be instantiated). Derived classes must declare and implement this
function or else they too are abstract and un-instatiable. It is
useful in defining an interface.

The second line declares a function a in some class that must have an
implementation for that class. That implementation will be inherited
by derived classes but can still be overridden and perform
polymorphically.

S.

Back to top
Thomas Wintschel
Guest





PostPosted: Sat Sep 27, 2003 6:49 pm    Post subject: Re: pure virtual Reply with quote



"<- Chameleon ->" <cham_gss (AT) hotmail (DOT) NOSPAM.com> wrote

Quote:
I don't understand. What's the difference in practice between 2 next
lines?
When I *must* use first line and when second?
-------------
virtual int a(int b) = 0;
virtual int a(int b);
-------------

thank you



You may want to look up 'pure virtual'

The first one would be used when defining a pure virtual base class. This
is done in situations where code needs to call the method 'a' for objects of
types which derive from the class but where you need the derived class to
implement the actual method (derived class *must* implement the method). If
we define:

class C
{

Back to top
Kevin Goodsell
Guest





PostPosted: Sat Sep 27, 2003 7:03 pm    Post subject: Re: pure virtual Reply with quote

Siana wrote:

Quote:
On Sat, 27 Sep 2003 20:13:31 +0300, "<- Chameleon ->"
[email]cham_gss (AT) hotmail (DOT) NOSPAM.com[/email]> wrote:


I don't understand. What's the difference in practice between 2 next lines?
When I *must* use first line and when second?
-------------
virtual int a(int b) = 0;
virtual int a(int b);
-------------

thank you



The first line declares a function a in some class that has no
definition

That is not true in general. A pure virtual function certainly may have
a definition.

Quote:
- it is there only to be overridden by derived classes for
the purposes of polymorphism. It also makes the class abstract (unable
to be instantiated). Derived classes must declare and implement this
function or else they too are abstract and un-instatiable. It is
useful in defining an interface.


-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


Back to top
Ron Natalie
Guest





PostPosted: Mon Sep 29, 2003 2:00 pm    Post subject: Re: pure virtual Reply with quote


"Siana" <siana_canadaREMOVETHISTOSEND (AT) yahoo (DOT) ca> wrote


Quote:
The first line declares a function a in some class that has no
definition

Wrong answer. It is the designation of the function as pure virtual.
It does not preclude the function from having a definition. It makes
the class it is defined in abstract. An abstract class is one that can
not be instantiated. It serves only as a base class for others.

Quote:
- it is there only to be overridden by derived classes for
the purposes of polymorphism.

Not quite, it means that it must be overridden in dervied classes
(as well as all other pure virtual functions) in order to be concrete
(that is, one that can have instances created of it).

Quote:
The second line declares a function a in some class that must have an
implementation for that class. That implementation will be inherited
by derived classes but can still be overridden and perform
polymorphically.

No it just declares one that is not pure. It means that it doesn't make
the class abstract.

While it is the case that you can omit the implementation of a pure virtual
function, that is secondary to it's meaning.



Back to top
jeffc
Guest





PostPosted: Mon Sep 29, 2003 2:37 pm    Post subject: Re: pure virtual Reply with quote


"<- Chameleon ->" <cham_gss (AT) hotmail (DOT) NOSPAM.com> wrote

Quote:
I don't understand. What's the difference in practice between 2 next
lines?
When I *must* use first line and when second?
-------------
virtual int a(int b) = 0;
virtual int a(int b);
-------------

There is no time when you *must* use the first (pure virtual). It's a
design decision on your part. There are 2 reasons you might use it
a) you want to force any class that inherits from this class to define
function a
b) you want that class to be abstract (no one can make an instance of it -
you can only make instances of subclasses). This is a rather confusing
means of creating an abstract class, but that's the way it is.



Back to top
jeffc
Guest





PostPosted: Mon Sep 29, 2003 2:41 pm    Post subject: Re: pure virtual Reply with quote


"Thomas Wintschel" <thomwint (AT) telus (DOT) net> wrote

Quote:

The first one would be used when defining a pure virtual base class.

You mean an abstract class.



Back to top
Ron Natalie
Guest





PostPosted: Mon Sep 29, 2003 3:04 pm    Post subject: Re: pure virtual Reply with quote


"jeffc" <nobody (AT) nowhere (DOT) com> wrote

Quote:

"Thomas Wintschel" <thomwint (AT) telus (DOT) net> wrote in message
news:KYkdb.12037$o21.6208 (AT) edtnps84 (DOT) ..

The first one would be used when defining a pure virtual base class.

You mean an abstract class.

Now you're being pedantic Smile




Back to top
jeffc
Guest





PostPosted: Mon Sep 29, 2003 3:35 pm    Post subject: Re: pure virtual Reply with quote


"Ron Natalie" <ron (AT) sensor (DOT) com> wrote

Quote:

"jeffc" <nobody (AT) nowhere (DOT) com> wrote


"Thomas Wintschel" <thomwint (AT) telus (DOT) net> wrote in message
news:KYkdb.12037$o21.6208 (AT) edtnps84 (DOT) ..

The first one would be used when defining a pure virtual base class.

You mean an abstract class.

Now you're being pedantic Smile

I knew somone was going to say that Smile The point to my way of thinking is
that "pure virtual base class" is not an OO term, it's not a C++ term, it's
just not any term I've ever heard. I did not of course object to "method",
nor would I even have objected if he used the term "abstract method" for
pure virtual function. I'd be interested in knowing if that's what Thomas
really meant to write and where he heard it. Maybe I should have said "You
mean an abstract class, right?"



Back to top
Ron Natalie
Guest





PostPosted: Mon Sep 29, 2003 3:58 pm    Post subject: Re: pure virtual Reply with quote


"jeffc" <nobody (AT) nowhere (DOT) com> wrote

Quote:

"Ron Natalie" <ron (AT) sensor (DOT) com> wrote in message
news:3f7849ee$0$36943$9a6e19ea (AT) news (DOT) newshosting.com...

"jeffc" <nobody (AT) nowhere (DOT) com> wrote in message
news:3f784563_2 (AT) news1 (DOT) prserv.net...

"Thomas Wintschel" <thomwint (AT) telus (DOT) net> wrote in message
news:KYkdb.12037$o21.6208 (AT) edtnps84 (DOT) ..

The first one would be used when defining a pure virtual base class.

You mean an abstract class.

Now you're being pedantic :-)

I knew somone was going to say that Smile

I was just ribbing ya.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.