LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-01-2015, 07:24 AM   #1
findnerd2
LQ Newbie
 
Registered: May 2015
Location: India
Posts: 11

Rep: Reputation: 0
What are pure virtual functions and How do I create it in C & C++?


I was wondering to know about virtual function, its uses and how to create a pure virtual function in c and c++ programming language. Being programmer I am looking to collect more and more c programming questions and answers to enhance my skills in C language. Can anyone answers my query.

Looking forward for positive replies.
 
Old 07-01-2015, 07:49 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
Honestly, the best way to find that is to Google it ... or DuckDuckGo. Search for c++ "pure virtual function".

There's an entire site devoted to programmer interview questions. The answer to this one is at http://www.programmerinterview.com/i...tual-function/.

And, as you'll quickly see from actually reading that page ... you'll quickly see that the C++ implementation of that idea is rather hosed-up. A function is shown as "assigned the value of zero," without explanation. This is to indicate that the function "has no body or definition," except, as the author goes on to say, it actually can. Other languages (and implementations of C++) deal with that problem by formally introducing the keyword, abstract. (See also http://www.programmerinterview.com/i...tract-keyword/.)

And, with all that said, I shan't go [much...] into a discussion here of what I think about "programmer interviews." I think that you can readily guess what I would say.

Anytime I taught a programming class in community college, I would let the students bring a "cheat sheet." One 8-1/2x11" piece of paper, filled on both sides with whatever they wanted, provided only if it was done by them. (They'd staple their sheets to the tests when they turned them in.) One enterprising student condensed 8 pages' worth of material and brought a magnifying glass with him ... which certainly attracted my interest, as did my observation that he never looked at it during the entire test, for which he received an "A."

I don't need to know if a programmer candidate has memorized the never-read pages of a programming language user-manual. I need to know if he/she has good social skills, can work well with others as a team, can delve into existing piles of execrable stinking crap legacy source-code, and can "think like a programmer." For the rest of it, I've got plenty of well-thumbed books on the shelf.

Last edited by sundialsvcs; 07-01-2015 at 07:56 AM.
 
Old 07-01-2015, 07:51 AM   #3
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
in C I would say it's just a function pointer, and if you use it you have to provide a function

in C++ and other OPP languages a pure virtual function is a function of a base class that has no implementation and derivated classes have to provide the implementation

where you use it you might not care about the concrete type because you know it has a base class with a certain function which you can call
 
Old 07-01-2015, 07:53 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
                            C            C++           Java
                           ---          -----         ------
function:                   +            +             -
virtual function:           -            -             -
non-virtual method:         -            +             -
virtual method:             -            +             +
pure virtual class:         -            +             +
 
Old 07-01-2015, 08:28 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by sundialsvcs View Post
A function is shown as "assigned the value of zero," without explanation.
What more explanation would you expect? If you know anything at all about C++, you know the designers of the language were very very stingy about introducing new keywords. So syntax using existing symbols is used wherever possible to indicate things you might have otherwise expected to be indicated by a new keyword ("pure" or "abstract" or whatever).

So you have the clearly documented syntax "=0" for specifying the clearly documented "pure virtual" behavior.

I do have to admit I didn't know about the ability to provide a definition for a function matching the pure virtual function's signature. At first glance, I'm not happy with that language feature, because my mind immediately leaps to all the obscure cases that seem to have no good solutions. I understand it would be a convenient place to put the base portion of the definition. It is very common for a virtual function definition to do part of the job and then hand off the rest to a base definition and I understand why it might (in some pretty obscure cases) be annoying to lose that ability in a pure virtual function. Maybe there is an answer I haven't thought of for the secondary issues it creates. But it seems like a language feature that is messier than it is worth. Even if that detail is messed up, I still disagree with you that pure virtual itself is "hosed up" in the C++ language design. The rest of it is not particularly impacted (except as an interview question) by that one obscurity, and otherwise the design makes perfect sense.

Last edited by johnsfine; 07-01-2015 at 08:46 AM.
 
  


Reply

Tags
function, virtual



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C++: Does it make sense to *define* the pure virtual functions in the *abstract base* class itself? Aquarius_Girl Programming 14 11-21-2011 06:51 PM
Pure virtual functions Doubt naveenisback Programming 1 10-30-2009 03:22 AM
I cannot use virtual users in pure-ftpd. kutlu Debian 0 01-05-2009 01:38 AM
LXer: This week at LWN: Implications of pure and constant functions LXer Syndicated Linux News 0 06-19-2008 08:30 PM
Pure Virtual LuderForChrist Programming 3 07-07-2004 01:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration