473,385 Members | 1,379 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,385 software developers and data experts.

pure virtual

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
Jul 19 '05 #1
9 3600
On Sat, 27 Sep 2003 20:13:31 +0300, "<- Chameleon ->"
<ch******@hotmail.NOSPAM.com> 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 - 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.
Jul 19 '05 #2
"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:bl**********@nic.grnet.gr...
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
{
Jul 19 '05 #3
Siana wrote:
On Sat, 27 Sep 2003 20:13:31 +0300, "<- Chameleon ->"
<ch******@hotmail.NOSPAM.com> 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.
- 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.

Jul 19 '05 #4

"Siana" <si**************************@yahoo.ca> wrote in message news:vh********************************@4ax.com...
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.
- 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).
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.
Jul 19 '05 #5

"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:bl**********@nic.grnet.gr...
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.
Jul 19 '05 #6

"Thomas Wintschel" <th******@telus.net> wrote in message
news:KYkdb.12037$o21.6208@edtnps84...

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


You mean an abstract class.
Jul 19 '05 #7

"jeffc" <no****@nowhere.com> wrote in message news:3f********@news1.prserv.net...

"Thomas Wintschel" <th******@telus.net> wrote in message
news:KYkdb.12037$o21.6208@edtnps84...

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


You mean an abstract class.

Now you're being pedantic :-)
Jul 19 '05 #8

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"jeffc" <no****@nowhere.com> wrote in message

news:3f********@news1.prserv.net...

"Thomas Wintschel" <th******@telus.net> wrote in message
news:KYkdb.12037$o21.6208@edtnps84...

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 :-) 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?"
Jul 19 '05 #9

"jeffc" <no****@nowhere.com> wrote in message news:3f********@news1.prserv.net...

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"jeffc" <no****@nowhere.com> wrote in message

news:3f********@news1.prserv.net...

"Thomas Wintschel" <th******@telus.net> wrote in message
news:KYkdb.12037$o21.6208@edtnps84...
>
> 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 :-)


I was just ribbing ya.
Jul 19 '05 #10

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

Similar topics

11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
7
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to what is the exact difference between a virtual function and a pure virtual function? Thanks in advance!!!
3
by: keith | last post by:
Dear mentors and gurus, I noticed at the end of section 22.4 of the 'FAQ-Lite' it says "Note that it is possible to provide a definition for a pure virtual function, but this usually confuses...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
10
by: Rahul | last post by:
Hi, I tried to create a abstract class by having a non-virtual member function as pure, but i got a compilation error saying "only virtual member functions can be pure"... I'm trying to think...
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.